溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java循環(huán)調(diào)用多個(gè)timer實(shí)現(xiàn)定時(shí)任務(wù)的方法

發(fā)布時(shí)間:2020-07-30 10:31:35 來源:億速云 閱讀:375 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要講解了Java循環(huán)調(diào)用多個(gè)timer實(shí)現(xiàn)定時(shí)任務(wù)的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

通常在使用java實(shí)現(xiàn)定時(shí)任務(wù)時(shí),有兩種方法,一種是spring中的schedule(cron = " */5 * * * "),另一種就是java中的timer,

timer+TimerTask配合實(shí)現(xiàn),這里附上Timer對(duì)象的一些常用api

Timer()                   創(chuàng)建-個(gè)新計(jì)時(shí)器。
Timer(boolean isDaemon)         創(chuàng)建一個(gè)新計(jì)時(shí)器, 可以指定其相關(guān)的線程作為守護(hù)程序運(yùn)行。
Timer(String, name)            創(chuàng)建一個(gè)新計(jì)時(shí)器,其相關(guān)的線程具有指定的名稱。
Timer(String, name, boolean isDaemon) 創(chuàng)建一個(gè)新計(jì)時(shí)器, 可以指定其相關(guān)的線程作為守護(hù)程序運(yùn)行。
cancel()  終止此計(jì)時(shí)器,丟棄所有當(dāng)前已安排的任務(wù)。.
purge ()  從此計(jì)時(shí)器的任務(wù)隊(duì)列中移除所有己取消的任務(wù)。
schedule(TimerTask, task, Date time)                  安排在指定的時(shí)間執(zhí)行指定的任務(wù)。
schedule (TimerTask task, Date firstTime, long period)       安排指定的任務(wù)在指定的時(shí)間開始進(jìn)行重復(fù)的固定延遲執(zhí)行。
schedule (TimerTask task, long, delay)                 安排在指定延遲后執(zhí)行指定的任務(wù)。
schedule (TimerTask task, 1ong. delay, long period)          安排指定的任務(wù)從指定的延遲后開始進(jìn)行重復(fù)的固定延遲執(zhí)行。
scheduleAtFixedRate (TimerTask task,Date firstTime, long period) 安排指定的任務(wù)在指定的時(shí)間開始進(jìn)行重復(fù)的固定速率執(zhí)行。
scheduleAtFixedRate (TimerTask task, long delay, long period)   安排指定的任務(wù)在指定的延遲后開始進(jìn)行重復(fù)的固定速率執(zhí)行。

下面列舉一個(gè)循環(huán)創(chuàng)建TimerTask的實(shí)例:

 public static void main(String[] args) throws ParseException { 
     ArrayList<TimeClass> list = new ArrayList<>();  //TimeClass是自己寫的一個(gè)類,只有兩個(gè)字段,private String startTime和private String endTime;     list.add(new TimeClass("2020-07-23 20:08:00 ","2020-07-23 20:08:15"));     list.add(new TimeClass("2020-07-23 20:08:30 ","2020-07-23 20:08:00"));      DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     //一個(gè)對(duì)象,在指定的開始時(shí)間和結(jié)束時(shí)間,分別進(jìn)行不同的操作,如商品的定時(shí)上架、下架。
    Timer timer = new Timer();
     for(int i = 0; i < list.size(); i++){
       Date startTime = df.parse(list.get(i).getStartTime());
       Date endTime = df.parse(list.get(i).getEndTime());
       int a=i;
       timer.schedule(new TimerTask() { //新建一個(gè)定時(shí)任務(wù)
         @Override
         public void run() {
           System.out.println("任務(wù)開始了"+a);
         }
       },startTime);       
       timer.schedule(new TimerTask() {
         @Override
         public void run() {
           System.out.println("任務(wù)結(jié)束了"+a);         }
       },endTime); 
     }
     //timer的過程中不會(huì)阻塞,不影響下面代碼的執(zhí)行
     for (int i=0;i<=20;i++) {
       System.out.println("=====" + i);
     }
   }

看完上述內(nèi)容,是不是對(duì)Java循環(huán)調(diào)用多個(gè)timer實(shí)現(xiàn)定時(shí)任務(wù)的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI