1,第一种:每隔多少时间爆炸一次,只爆炸一次,以后就不爆炸了。
import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class TraditionalTimerTest{ public static void main(String[] main){ new Timer().schedule(new TimerTask(){ @Override public void run() { System.out.println("bombing"); // 相当于一个炸弹。多少时间炸呢,就看 自己定义的时间了。 } }, 10000); // 相隔 10秒 爆炸一次 就再也炸了 while(true){ System.out.println(new Date().getSeconds()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
2,第二种:时间到后就爆炸一次,然后 再到多长时间再炸,以后就每隔这个就炸。
package com.thread; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class TimerTwo { public static void main(String[] main){ new Timer().schedule(new TimerTask(){ @Override public void run() { System.out.println("bombing"); // 相当于一个炸弹。多少时间炸呢,就看 自己定义的时间了。 } }, 10000,3000); // 相隔 10秒 爆炸一次 就再也炸了 // 3000表明:以后每隔3秒就会炸。 while(true){ System.out.println(new Date().getSeconds()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
3,循环炸的思路
(1)在定时器 内部再装一个炸弹。
package com.thread; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class Time { @SuppressWarnings("deprecation") public static void main(String[] main){ // new TimerTask(){// 可以用 this代替。但是:炸弹已经扎完了 就会在炸了。必须用新的炸弹。 // 必须 用新的炸弹。 // 所以:必须把这个 匿名类 改成有 有名字的。 new Timer().schedule(new TimerTask(){// 可以用 this代替。但是:炸弹已经扎完了 就会在炸了。必须用新的炸弹。 @Override public void run() { System.out.println("bombing"); // 在装个 定时器。 new Timer().schedule( new TimerTask(){ @Override public void run() { System.out.println("bombing"); } }, /*this,*/2000); } }, 2000);// 两秒以后炸。 while(true){ int seconds = new Date().getSeconds(); System.out.println(seconds); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
结果:在没有用this代替时,炸到第二次就不再炸了,若想达到这样的结果:必须再装一个炸弹,而我们这时就可以用this代替,注释里面已经阐述说明,就不再重复了。
显然:用this是不可以的。
4,我们来 new 一个类 用这个类来设计 1千,一万个对象,就可以了。匿名类只能new 一个对象 new 完之后这个类就找不回来了。所以:要改成 有名字的。
new TimerTask() 可以用 this代替。但是:炸弹已经扎完了 就不会在炸了。必须用新的炸弹。 // 必须 用新的炸弹。 // 所以:必须把这个 匿名类 改成有 有名字的。
这样做就可以了:
package com.itm.thread; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class TraditionalTimerTest { /** * @param args */ public static void main(String[] args) { class MyTimerTask extends TimerTask { @Override public void run() { System.out.println("bombing"); new Timer().schedule(/*new TimerTask(){ @Override public void run() { System.out.println("bombing"); } }*/new MyTimerTask(), 2000); } } new Timer().schedule(/*new TimerTask(){ @Override public void run() { System.out.println("bombing"); new Timer().schedule( //new TimerTask(){ //@Override //public void run() { //System.out.println("bombing"); //} //}, this,2000); } }*/new MyTimerTask(), 2000); while(true){ System.out.println(new Date().getSeconds()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
package com.itm.thread; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class TraditionalTimerTest { /** * * new 对象里面 肯定会有方法。运行,对象里面特定的方法代码。 * * * @param args */ public static void main(String[] args) { class MyTimerTask extends TimerTask { @Override public void run() { System.out.println("bombing"); new Timer().schedule(new MyTimerTask(), 2000); } } new Timer().schedule(new MyTimerTask(), 2000); while(true){ System.out.println(new Date().getSeconds()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
5,每隔不同的时间,来进行循环炸,2秒炸一次,四秒再炸一次,如此循环。
别炸啦,快哭了。
package com.itm.thread; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class TraditionalTimerChangeTest { private static int count = 0; 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){ System.out.println(new Date().getSeconds()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
这里面有:jdk1.5 的定时器:http://blog.csdn.net/itm_hadf/article/details/7599814
http://iamzhongyong.iteye.com/blog/1404149