分享

Java实现简单定时任务

 aaie_ 2016-04-12

了解了一下Java实现简单定时任务的三种方式,分别做了个例子。

自己写篇以记录、备忘。

注释都清楚了,不另外解释。

方式一:

  1. package test.timertask;  
  2.   
  3. /** 
  4.  * 使用Thread.sleep的方式来实现 
  5.  * 这种方式完全是自己实现的,该控制的地方控制,怎么写都可以 
  6.  * @author wz 
  7.  */  
  8. public class MyTimerTask {  
  9.     public static void main(String[] args) {  
  10.         MyTimerTask mt  = new MyTimerTask();  
  11.         mt.task();  
  12.     }  
  13.       
  14.     public void task(){  
  15.           
  16.         final long timeInterval = 1000;  
  17.         (new Runnable() {  
  18.             public void run() {  
  19.                 while (true) {  
  20.                     // 执行任务  
  21.                     System.out.println("do the task……");  
  22.                     try {  
  23.                         Thread.sleep(timeInterval);  
  24.                     } catch (InterruptedException e) {  
  25.                         e.printStackTrace();  
  26.                     }  
  27.                 }  
  28.             }  
  29.         }).run();  
  30.         //这里直接调runnable的run方法了,这样是由main方法的执行线程直接转去执行这个runnable了(同一个线程)  
  31.         //当然也可new一个Thread,将runnable扔进去后调用其start方法(另启一线程)  
  32.     }  
  33. }  

方式二:
  1. package test.timertask;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. /** 
  7.  *  
  8.  * @author wz 
  9.  * 通过Timer和TimerTask来实现,jdk自己实现且封装了其操作, 
  10.  * TimerTask实现Runnable接口,但是其实现正是留给用户制定任务的接口 
  11.  * 然后通过Timer来执行即可,Timer另启线程来执行任务,且实现了线程同步的,所示线程安全的 
  12.  * Timer维持一个任务队列,可以调度多个任务 
  13.  *  
  14.  */  
  15. public class MyTimerTask2 {  
  16.     public static void main(String[] args) {  
  17.         MyTimerTask2 mt2  =new MyTimerTask2();  
  18.         mt2.task();  
  19.     }  
  20.     public void task(){  
  21.         //制定一个任务  
  22.         TimerTask task = new TimerTask() {  
  23.             @Override  
  24.             public void run() {  
  25.                 System.out.println("执行任务ing");  
  26.             }  
  27.         };  
  28.           
  29. //      task - 所要安排的任务。  
  30. //      delay - 执行任务前的延迟时间,单位是毫秒。  
  31. //      period - 执行各后续任务之间的时间间隔,单位是毫秒。   
  32.         Timer timer = new Timer();  
  33.         long delay = 0;  
  34.         long intevalPeriod = 1 * 1000;  
  35.         timer.scheduleAtFixedRate(task, delay, intevalPeriod);  
  36.     }  
  37. }  

方式三:
  1. package test.timertask;  
  2.   
  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.   
  8. /** 
  9.  *  
  10.  * @author wz 
  11.  * jdk1.5提供了支持并发的定时任务处理工具ScheduledExecutorService 
  12.  * 1.以线程池的方式来执行任务,效率高了 
  13.  * 2.可以设置开始延迟时间和任务取消时间 
  14.  * 
  15.  */  
  16. public class MyTimerTask3 {  
  17.     public static void main(String[] args) {  
  18.         MyTimerTask3 mtt = new MyTimerTask3();  
  19.         mtt.taskForAPeriod();  
  20.     }  
  21.   
  22.     private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);  
  23.   
  24.     public void taskForAPeriod() {  
  25.         //定义一个任务  
  26.         final Runnable task = new Runnable() {  
  27.             public void run() {  
  28.                 System.out.println("执行任务");  
  29.             }  
  30.         };  
  31.           
  32. //      scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit):  
  33. //      通过ScheduledExecutorService的scheduleAtFixedRate来执行任务  
  34. //      参数  
  35. //      command - 要执行的任务  
  36. //      initialDelay - 首次执行的延迟时间  
  37. //      period - 连续执行之间的周期  
  38. //      unit - initialDelay 和 period 参数的时间单位  
  39.         final ScheduledFuture<?> taskHandle = scheduler.scheduleAtFixedRate(task, 11, TimeUnit.SECONDS);  
  40.           
  41.           
  42. //      schedule(Runnable command,long delay,TimeUnit unit)创建并执行在给定延迟后启用的一次性操作。 (取消任务本身也是一个任务,一次去下就OK)  
  43. //      参数:  
  44. //      command - 要执行的任务  
  45. //      delay - 从现在开始延迟执行的时间  
  46. //      unit - 延迟参数的时间单位   
  47.         scheduler.schedule(new Runnable() {  
  48.             public void run() {  
  49.                 taskHandle.cancel(true);    //取消任务  
  50.             }  
  51.         }, 10, TimeUnit.SECONDS);   //在10个TimeUnit.SECONDS时间单位后  
  52.     }  
  53.   
  54. }  



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多