ScheduledExecutorService执行周期任务

来源:互联网 发布:python 计算 macd 编辑:程序博客网 时间:2024/06/06 01:54

鉴于 Timer 的上述缺陷,Java5 推出了基于线程池设计的 ScheduledExecutor。其设计思想是,每一个被调度的任务都会由线程池中一个线程去执行,因此任务是并发执行的,相互之间不会受到干扰。需要注意的是,只有当任务的执行时间到来时,ScheduedExecutor 才会真正启动一个线程,其余时间 ScheduledExecutor 都是在轮询任务的状态


项目中用到了cheduledExecutorService中的一个方法,就顺便把其中几个关重要的方法学习下:
1.schedule 
     schedule(Runnable command, long delay, TimeUnit unit),schedule方法被用来延迟指定时间后执行某个指定任务。

a.代码如下:

Java代码  收藏代码
  1. public class Job implements Runnable {  
  2.     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
  3.   
  4.     public void run() {  
  5.         try {  
  6.             Thread.sleep(5000);  
  7.         } catch (InterruptedException ex) {  
  8.             ex.printStackTrace();  
  9.         }  
  10.         System.out.println("do something  at:" + sdf.format(new Date()));  
  11.     }  
  12. }  
  13.   
  14. public class ScheduledExecutorServiceTest {  
  15.   
  16.     public static void main(String[] args) {  
  17.         ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5);  
  18.         final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
  19.         System.out.println(" begin to do something at:" + sdf.format(new Date()));  
  20.         schedule.schedule(new Job(),1, TimeUnit.SECONDS);  
  21.     }  
  22. }  

  
b.输出如下:

Java代码  收藏代码
  1. begin to do something at:2012-08-03 09:31:36  
  2. do something  at:2012-08-03 09:31:42  
 


2.scheduleWithFixedDelay 
         scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,TimeUnit unit)
         创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟,如果任务的执行时间超过了廷迟时间(delay),下一个任务则会在
(当前任务执行所需时间+delay)后执行。
    a.代码如下:

Java代码  收藏代码
  1. public class ScheduledExecutorServiceTest {  
  2.         public static void main(String[] args) {  
  3.             ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5);  
  4.             final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
  5.             System.out.println(" begin to do something at:" + sdf.format(new Date()));  
  6.             schedule.scheduleWithFixedDelay(new Job(), 12, TimeUnit.SECONDS);  
  7.         }  
  8.     }  
 

    b.输出如下:

Java代码  收藏代码
  1. begin to do something at:2012-08-03 09:36:53  
  2. do something at:2012-08-03 09:36:59  
  3. do something at:2012-08-03 09:37:06  
  4. do something at:2012-08-03 09:37:13  
 

3.scheduleAtFixedRate 
         scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnitunit)
         创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。
如果任务的执行时间小于period,将会按上述规律执行。否则,则会按 任务的实际执行时间进行周期执行。
    a.代码如下:

Java代码  收藏代码
  1. public class ScheduledExecutorServiceTest {  
  2.   
  3.     public static void main(String[] args) {  
  4.         ScheduledExecutorService schedule = Executors.newScheduledThreadPool(2);  
  5.         final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
  6.         System.out.println(" begin to do something at:" + sdf.format(new Date()));  
  7.         schedule.scheduleAtFixedRate(new Job(), 1,2, TimeUnit.SECONDS);  
  8.     }  

 

    b.结果输出:

Java代码  收藏代码
  1. begin to do something at:2012-08-04 08:53:30  
  2. do something at:2012-08-04 08:53:36  
  3. do something at:2012-08-04 08:53:41  
  4. do something at:2012-08-04 08:53:46  
  5. do something at:2012-08-04 08:53:51  
 
0 0
原创粉丝点击