乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Java實(shí)現(xiàn)簡單定時(shí)任務(wù)

       aaie_ 2016-04-12

      了解了一下Java實(shí)現(xiàn)簡單定時(shí)任務(wù)的三種方式,分別做了個(gè)例子。

      自己寫篇以記錄、備忘。

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

      方式一:

      1. package test.timertask;  
      2.   
      3. /** 
      4.  * 使用Thread.sleep的方式來實(shí)現(xiàn) 
      5.  * 這種方式完全是自己實(shí)現(xiàn)的,該控制的地方控制,怎么寫都可以 
      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.                     // 執(zhí)行任務(wù)  
      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.         //這里直接調(diào)runnable的run方法了,這樣是由main方法的執(zhí)行線程直接轉(zhuǎn)去執(zhí)行這個(gè)runnable了(同一個(gè)線程)  
      31.         //當(dāng)然也可new一個(gè)Thread,將runnable扔進(jìn)去后調(diào)用其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來實(shí)現(xiàn),jdk自己實(shí)現(xiàn)且封裝了其操作, 
      10.  * TimerTask實(shí)現(xiàn)Runnable接口,但是其實(shí)現(xiàn)正是留給用戶制定任務(wù)的接口 
      11.  * 然后通過Timer來執(zhí)行即可,Timer另啟線程來執(zhí)行任務(wù),且實(shí)現(xiàn)了線程同步的,所示線程安全的 
      12.  * Timer維持一個(gè)任務(wù)隊(duì)列,可以調(diào)度多個(gè)任務(wù) 
      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.         //制定一個(gè)任務(wù)  
      22.         TimerTask task = new TimerTask() {  
      23.             @Override  
      24.             public void run() {  
      25.                 System.out.println("執(zhí)行任務(wù)ing");  
      26.             }  
      27.         };  
      28.           
      29. //      task - 所要安排的任務(wù)。  
      30. //      delay - 執(zhí)行任務(wù)前的延遲時(shí)間,單位是毫秒。  
      31. //      period - 執(zhí)行各后續(xù)任務(wù)之間的時(shí)間間隔,單位是毫秒。   
      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提供了支持并發(fā)的定時(shí)任務(wù)處理工具ScheduledExecutorService 
      12.  * 1.以線程池的方式來執(zhí)行任務(wù),效率高了 
      13.  * 2.可以設(shè)置開始延遲時(shí)間和任務(wù)取消時(shí)間 
      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.         //定義一個(gè)任務(wù)  
      26.         final Runnable task = new Runnable() {  
      27.             public void run() {  
      28.                 System.out.println("執(zhí)行任務(wù)");  
      29.             }  
      30.         };  
      31.           
      32. //      scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit):  
      33. //      通過ScheduledExecutorService的scheduleAtFixedRate來執(zhí)行任務(wù)  
      34. //      參數(shù)  
      35. //      command - 要執(zhí)行的任務(wù)  
      36. //      initialDelay - 首次執(zhí)行的延遲時(shí)間  
      37. //      period - 連續(xù)執(zhí)行之間的周期  
      38. //      unit - initialDelay 和 period 參數(shù)的時(shí)間單位  
      39.         final ScheduledFuture<?> taskHandle = scheduler.scheduleAtFixedRate(task, 11, TimeUnit.SECONDS);  
      40.           
      41.           
      42. //      schedule(Runnable command,long delay,TimeUnit unit)創(chuàng)建并執(zhí)行在給定延遲后啟用的一次性操作。 (取消任務(wù)本身也是一個(gè)任務(wù),一次去下就OK)  
      43. //      參數(shù):  
      44. //      command - 要執(zhí)行的任務(wù)  
      45. //      delay - 從現(xiàn)在開始延遲執(zhí)行的時(shí)間  
      46. //      unit - 延遲參數(shù)的時(shí)間單位   
      47.         scheduler.schedule(new Runnable() {  
      48.             public void run() {  
      49.                 taskHandle.cancel(true);    //取消任務(wù)  
      50.             }  
      51.         }, 10, TimeUnit.SECONDS);   //在10個(gè)TimeUnit.SECONDS時(shí)間單位后  
      52.     }  
      53.   
      54. }  



        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多