import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class TraditionalTimerTest { static int count; public static void main(String[] args) { class MyTimerTask extends TimerTask { @Override public void run() { count = (count + 1) % 2; System.out.println("bombing!"); new Timer().schedule(new MyTimerTask(), 2000 + 2000 * count); } } new Timer().schedule(new MyTimerTask(), 2000); while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(new Date().getSeconds()); } } }
两秒炸一次,四秒炸一次,循环往复。。。
时间: 2024-10-22 23:40:56