多线程_几种定时器的写法

来源:互联网 发布:php异步请求$.post 编辑:程序博客网 时间:2024/05/29 11:47

1 :固定时间后执行一次任务:1000毫秒后执行任务(只执行一次)

2: 5000毫秒后,执行任务,以后每隔1000毫秒再执行一次任务(无限执行)

3:交替再生:任务24秒交替的执行(无限执行)

4: 创建两个循环交替任务:2秒后,A任务执行。 A任务里面创建一个B任务4秒后执行,B任务里面又创建一个A任务2秒后执行,如此往复。


                1,2,3代码:

[java] view plain copy
  1. import java.util.Date;  
  2. import java.util.Timer;  
  3. import java.util.TimerTask;  
  4. /** 
  5.  * @author Administrator @zsw 2012-7-19 下午04:37:19 
  6.  */  
  7. public class TraditionalTimer {  
  8.     public static void main(String[] args) {  
  9.         //1:  
  10. //      test1();  
  11.           
  12.         //2:  
  13. //      test2();  
  14.           
  15.         //3:  
  16.         test3();  
  17.         //用于打印时间秒数  
  18.         while (true) {  
  19.             System.out.println(new Date().getSeconds());  
  20.             try {  
  21.                 Thread.sleep(1000);  
  22.             } catch (InterruptedException e) {  
  23.                 // TODO Auto-generated catch block  
  24.                 e.printStackTrace();  
  25.             }  
  26.         }  
  27.           
  28.           
  29.     }  
  30.   
  31.     //1:固定时间后执行一次任务:1000毫秒后执行任务(只执行一次)  
  32.     public static void test1() {  
  33.         new Timer().schedule(new TimerTask() {  
  34.             @Override  
  35.             public void run() {  
  36.                 // TODO Auto-generated method stub  
  37.                 System.out.println("bombing!");  
  38.             }  
  39.         }, 1000);  
  40.     }  
  41.   
  42.     // 2:5000毫秒后,执行任务,以后每隔1000毫秒再执行一次任务(无限执行)  
  43.     public static void test2() {  
  44.         new Timer().schedule(new TimerTask() {  
  45.             @Override  
  46.             public void run() {  
  47.                 // TODO Auto-generated method stub  
  48.                 System.out.println("bombing!");  
  49.             }  
  50.         }, 50001000);  
  51.     }  
  52.   
  53.       
  54.     //3:交替再生:任务2秒4秒交替的执行(无限执行),  
  55.     static int count = 0;  
  56.     public static void test3() {  
  57.   
  58.         class MyTimerTask extends TimerTask {  
  59.             @Override  
  60.             public void run() {  
  61.                 count = (count + 1) % 2;  
  62.                 System.out.println("bombing!");  
  63.                 new Timer().schedule(new MyTimerTask(), 2000 + count * 2000);  
  64.             }  
  65.         }  
  66.         new Timer().schedule(new MyTimerTask(), 2000);  
  67.     }  
  68.   
  69. }  

 

           4代码

 

[java] view plain copy
  1. import java.util.Date;  
  2. import java.util.Timer;  
  3. import java.util.TimerTask;  
  4.   
  5. /** 
  6.  * @author Administrator @zsw 2012-7-20 下午08:08:42 
  7.  */  
  8. public class TraditionalTime2 {  
  9.       
  10.     /* 
  11.      * 创建两个循环交替任务:2秒后,A任务执行。 
  12.      * A任务里面创建一个B任务4秒后执行,B任务里面又创建一个A任务2秒后执行,,如此往复。 
  13.      *  
  14.      */  
  15.     public static void main(String[] args) {  
  16.         TraditionalTime2 t2=new TraditionalTime2();  
  17.         new Timer().schedule(t2.new A(), 2000);  
  18.           
  19.          //用于打印时间秒数  
  20.         while (true) {  
  21.             System.out.println(new Date().getSeconds());  
  22.             try {  
  23.                 Thread.sleep(1000);  
  24.             } catch (InterruptedException e) {  
  25.                 // TODO Auto-generated catch block  
  26.                 e.printStackTrace();  
  27.             }  
  28.         }  
  29.     }  
  30.     class A extends TimerTask {  
  31.         @Override  
  32.         public void run() {  
  33.             System.out.println("A bombing!");  
  34.             new Timer().schedule(new B(), 4000);  
  35.   
  36.         }  
  37.   
  38.     }  
  39.   
  40.     class B extends TimerTask {  
  41.         @Override  
  42.         public void run() {  
  43.             System.out.println("B bombing!");  
  44.             new Timer().schedule(new A(), 2000);  
  45.   
  46.         }  
  47.     }  
  48. }  

0 0