ScheduledExecutorService执行定时任务

来源:互联网 发布:xtream path1.6 mac 编辑:程序博客网 时间:2024/06/13 22:33

ScheduledExecutorService执行定时任务

    博客分类: 
  • Java/J2se
 

ScheduledExecutorService

 

作者:赵磊

博客:http://elf8848.iteye.com

 

java.util.concurrent.Executors可创建一个线程池,它可给定延迟后定期地执行任务。
Executors.newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)


其中的scheduleAtFixedRate 和 scheduleWithFixedDelay 方法可以创建并执行任务,这些任务一直执行 ,并且是可以取消的任务。

 

 

Java代码  收藏代码
  1. import java.text.SimpleDateFormat;  
  2. import java.util.Date;  
  3. import java.util.concurrent.Executors;  
  4. import java.util.concurrent.ScheduledExecutorService;  
  5. import java.util.concurrent.ScheduledFuture;  
  6. import java.util.concurrent.TimeUnit;  
  7. import java.util.logging.SimpleFormatter;  
  8.   
  9. /** 
  10.  * ScheduledExecutorService 测试 
  11.  *  
  12.  * @author zhaolei 2012-5-23 
  13.  */  
  14. public class ScheduledThreadPoolTest {  
  15.   
  16.     private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);  
  17.     static SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd dd:mm:ss");  
  18.   
  19.     /** 
  20.      * beepForTime 
  21.      */  
  22.     public static void beepForTime() {  
  23.           
  24.         /** 
  25.          * 要执行的任务 
  26.          */  
  27.         final Runnable beeper = new Runnable() {  
  28.             int i=0;  
  29.             public void run() {  
  30.                 System.out.println("beep:"+(i++)+"----"+sf.format(new Date(System.currentTimeMillis())));  
  31.                 //throw new RuntimeException();  
  32.                   
  33.                 //任务耗时2秒  
  34.                 try {  
  35.                     Thread.sleep(2000);  
  36.                 } catch (InterruptedException e) {  
  37.                     e.printStackTrace();  
  38.                 }  
  39.             }  
  40.         };  
  41.           
  42.   
  43.   
  44.         int startTime=3;  
  45.         int each=5;  
  46.         System.out.println("任务将于"+startTime+"秒后开始,每"+each+"秒执行1次");  
  47.           
  48.         /** 
  49.          * 每each秒执行一次任务,两次任务的执行间隔是each秒(前面定义的变更) 
  50.          *  
  51.          *  
  52.          * 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期; 
  53.          * 也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行, 
  54.          * 接着在 initialDelay + 2 * period 后执行,依此类推。如果任务的任何一个执行遇到异常, 
  55.          * 则后续执行都会被取消。否则,只能通过执行程序的取消或终止方法来终止该任务。 
  56.          * 如果此任务的任何一个执行要花费比其周期更长的时间,则将推迟后续执行,但不会同时执行。 
  57.          * 参数: 
  58.          * command - 要执行的任务 
  59.          * initialDelay - 首次执行的延迟时间 
  60.          * period - 连续执行之间的周期 
  61.          * unit - initialDelay 和 period 参数的时间单位  
  62.          *  
  63.          */  
  64.         //final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, startTime, each, TimeUnit.SECONDS);  
  65.           
  66.           
  67.         /** 
  68.          *  
  69.          * 每each秒执行一次任务,两次任务的执行间隔是each秒(前面定义的变更)+任务执行用时 
  70.          *  
  71.          * 创建并执行一个在给定初始延迟后首次启用的定期操作, 
  72.          * 随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。 
  73.          * 如果任务的任一执行遇到异常,就会取消后续执行。 
  74.          * 否则,只能通过执行程序的取消或终止方法来终止该任务。  
  75.          * 参数: 
  76.          * command - 要执行的任务 
  77.          * initialDelay - 首次执行的延迟时间 
  78.          * period - 连续执行之间的周期 
  79.          * unit - initialDelay 和 period 参数的时间单位  
  80.          *  
  81.          */  
  82.         final ScheduledFuture<?> beeperHandle = scheduler.scheduleWithFixedDelay(beeper, startTime, each, TimeUnit.SECONDS);  
  83.           
  84.         /** 
  85.          * 创建并执行在给定延迟后启用的一次性操作。 
  86.          *  
  87.          * 这里用于在N时间后取消任务 
  88.          */  
  89.         scheduler.schedule(new Runnable() {  
  90.             public void run() {  
  91.                 System.out.println("取消任务");  
  92.                 beeperHandle.cancel(true);  
  93.             }  
  94.         }, 60, TimeUnit.SECONDS);  
  95.     }  
  96.   
  97.     /** 
  98.      * main 
  99.      *  
  100.      * @param args 
  101.      */  
  102.     public static void main(String[] args) {  
  103.         beepForTime();  
  104.     }  
  105. }  
 

 

0 0
原创粉丝点击