标签:定时 ng2 new t public dex schedule date() [] ext
定义两个炸弹: 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); } }
定时器连环炸弹:先2s炸一下,再4s炸一下,再2s炸一下,再4s炸一下如此循环下去。
标签:定时 ng2 new t public dex schedule date() [] ext
原文地址:http://www.cnblogs.com/ysloong/p/6368278.html