zoukankan      html  css  js  c++  java
  • 多线程_几种定时器的写法

      1  :固定时间后执行一次任务:1000毫秒后执行任务(只执行一次)
      2 
      3 2: 5000毫秒后,执行任务,以后每隔1000毫秒再执行一次任务(无限执行)
      4 
      5 3:交替再生:任务2秒4秒交替的执行(无限执行),
      6 
      7 4: 创建两个循环交替任务:2秒后,A任务执行。 A任务里面创建一个B任务4秒后执行,B任务里面又创建一个A任务2秒后执行,如此往复。
      8 
      9 
     10 1,2,3代码:
     11 
     12 [java] view plain copy
     13 
     14     import java.util.Date;  
     15     import java.util.Timer;  
     16     import java.util.TimerTask;  
     17     /** 
     18      * @author Administrator @zsw 2012-7-19 下午04:37:19 
     19      */  
     20     public class TraditionalTimer {  
     21         public static void main(String[] args) {  
     22             //1:  
     23     //      test1();  
     24               
     25             //2:  
     26     //      test2();  
     27               
     28             //3:  
     29             test3();  
     30             //用于打印时间秒数  
     31             while (true) {  
     32                 System.out.println(new Date().getSeconds());  
     33                 try {  
     34                     Thread.sleep(1000);  
     35                 } catch (InterruptedException e) {  
     36                     // TODO Auto-generated catch block  
     37                     e.printStackTrace();  
     38                 }  
     39             }  
     40               
     41               
     42         }  
     43       
     44         //1:固定时间后执行一次任务:1000毫秒后执行任务(只执行一次)  
     45         public static void test1() {  
     46             new Timer().schedule(new TimerTask() {  
     47                 @Override  
     48                 public void run() {  
     49                     // TODO Auto-generated method stub  
     50                     System.out.println("bombing!");  
     51                 }  
     52             }, 1000);  
     53         }  
     54       
     55         // 2:5000毫秒后,执行任务,以后每隔1000毫秒再执行一次任务(无限执行)  
     56         public static void test2() {  
     57             new Timer().schedule(new TimerTask() {  
     58                 @Override  
     59                 public void run() {  
     60                     // TODO Auto-generated method stub  
     61                     System.out.println("bombing!");  
     62                 }  
     63             }, 5000, 1000);  
     64         }  
     65       
     66           
     67         //3:交替再生:任务2秒4秒交替的执行(无限执行),  
     68         static int count = 0;  
     69         public static void test3() {  
     70       
     71             class MyTimerTask extends TimerTask {  
     72                 @Override  
     73                 public void run() {  
     74                     count = (count + 1) % 2;  
     75                     System.out.println("bombing!");  
     76                     new Timer().schedule(new MyTimerTask(), 2000 + count * 2000);  
     77                 }  
     78             }  
     79             new Timer().schedule(new MyTimerTask(), 2000);  
     80         }  
     81       
     82     }  
     83 
     84 
     85  
     86 
     87            4代码
     88 
     89  
     90 [java] view plain copy
     91 
     92     import java.util.Date;  
     93     import java.util.Timer;  
     94     import java.util.TimerTask;  
     95       
     96     /** 
     97      * @author Administrator @zsw 2012-7-20 下午08:08:42 
     98      */  
     99     public class TraditionalTime2 {  
    100           
    101         /* 
    102          * 创建两个循环交替任务:2秒后,A任务执行。 
    103          * A任务里面创建一个B任务4秒后执行,B任务里面又创建一个A任务2秒后执行,,如此往复。 
    104          *  
    105          */  
    106         public static void main(String[] args) {  
    107             TraditionalTime2 t2=new TraditionalTime2();  
    108             new Timer().schedule(t2.new A(), 2000);  
    109               
    110              //用于打印时间秒数  
    111             while (true) {  
    112                 System.out.println(new Date().getSeconds());  
    113                 try {  
    114                     Thread.sleep(1000);  
    115                 } catch (InterruptedException e) {  
    116                     // TODO Auto-generated catch block  
    117                     e.printStackTrace();  
    118                 }  
    119             }  
    120         }  
    121         class A extends TimerTask {  
    122             @Override  
    123             public void run() {  
    124                 System.out.println("A bombing!");  
    125                 new Timer().schedule(new B(), 4000);  
    126       
    127             }  
    128       
    129         }  
    130       
    131         class B extends TimerTask {  
    132             @Override  
    133             public void run() {  
    134                 System.out.println("B bombing!");  
    135                 new Timer().schedule(new A(), 2000);  
    136       
    137             }  
    138         }  
    139     }  
  • 相关阅读:
    oracle 数据库服务名怎么查
    vmware vsphere 6.5
    vSphere虚拟化之ESXi的安装及部署
    ArcMap中无法添加ArcGIS Online底图的诊断方法
    ArcGIS中字段计算器(高级计算VBScript、Python)
    Bad habits : Putting NOLOCK everywhere
    Understanding the Impact of NOLOCK and WITH NOLOCK Table Hints in SQL Server
    with(nolock) or (nolock)
    What is “with (nolock)” in SQL Server?
    Changing SQL Server Collation After Installation
  • 原文地址:https://www.cnblogs.com/stsinghua/p/6418775.html
Copyright © 2011-2022 走看看