定义两个炸弹: public class SerialBomings { public static void main(String[] args) { new Timer().schedule(new MyTimerTask(), 2000); new Timer().schedule(new MyTimerTask2(), 6000); while(true){ System.out.println(new Date().getSeconds()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } class MyTimerTask extends TimerTask{ @Override public void run() { System.out.println("bombing2"); new Timer().schedule(new MyTimerTask2(), 6000); } } class MyTimerTask2 extends TimerTask{ @Override public void run() { System.out.println("bombing"); new Timer().schedule(new MyTimerTask(), 6000); } }
定义一个炸弹 public class SerialBomings { public static void main(String[] args) { new Timer().schedule(new MyTimerTask(), 2000); //new Timer().schedule(new MyTimerTask2(), 6000); while(true){ System.out.println(new Date().getSeconds()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } class MyTimerTask extends TimerTask{ //静态变量 static int count = 0; @Override public void run() { count = (count+1)%2; System.out.println("bombing"); new Timer().schedule(new MyTimerTask(), 2000 + count*2000); } }
时间: 2024-10-13 23:44:59