這篇文章主要介紹基于ScheduledExecutorService的方法有哪些,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)是一家專注于做網(wǎng)站、網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),鄆城網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:鄆城等地區(qū)。鄆城做網(wǎng)站價(jià)格咨詢:18980820575
開(kāi)發(fā)中,往往遇到另起線程執(zhí)行其他代碼的情況,用java定時(shí)任務(wù)接口ScheduledExecutorService來(lái)實(shí)現(xiàn)。
ScheduledExecutorService是基于線程池設(shè)計(jì)的定時(shí)任務(wù)類,每個(gè)調(diào)度任務(wù)都會(huì)分配到線程池中的一個(gè)線程去執(zhí)行,也就是說(shuō),任務(wù)是并發(fā)執(zhí)行,互不影響。
注意,只有當(dāng)調(diào)度任務(wù)來(lái)的時(shí)候,ScheduledExecutorService才會(huì)真正啟動(dòng)一個(gè)線程,其余時(shí)間ScheduledExecutorService都是處于輪詢?nèi)蝿?wù)的狀態(tài)。
1.scheduleAtFixedRate方法
例子:
import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class ScheduleAtFixedRateDemo { public static void main(String[] args) { ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置日期格式 executorService.scheduleAtFixedRate(new Runnable(){ @Override public void run() { System.out.println("++++++++++++++++++++thread:" + df.format(new Date())); } }, 2, 3, TimeUnit.SECONDS); System.out.println("++++++++++++++++++++main:" + df.format(new Date())); } }
運(yùn)行結(jié)果:
++++++++++++++++++++main:2017-10-20 15:20:52 ++++++++++++++++++++thread:2017-10-20 15:20:54 ++++++++++++++++++++thread:2017-10-20 15:20:57 ++++++++++++++++++++thread:2017-10-20 15:21:00 ++++++++++++++++++++thread:2017-10-20 15:21:03 ++++++++++++++++++++thread:2017-10-20 15:21:06
可以看出來(lái),在2s后,子線程開(kāi)始執(zhí)行,并且每過(guò)3s輪詢執(zhí)行一次。
2.scheduleWithFixedDelay方法
例子:
import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /** * ScheduleWithFixedDelay的用法 * @author Administrator * */ public class ScheduleWithFixedDelayDemo { public static void main(String[] args) { ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置日期格式 executorService.scheduleWithFixedDelay(new Runnable(){ @Override public void run() { System.out.println("++++++++++++++++++++thread:" + df.format(new Date())); } }, 2, 3, TimeUnit.SECONDS); System.out.println("++++++++++++++++++++main:" + df.format(new Date())); } }
運(yùn)行結(jié)果:
++++++++++++++++++++main:2017-10-20 15:24:32 ++++++++++++++++++++thread:2017-10-20 15:24:34 ++++++++++++++++++++thread:2017-10-20 15:24:37 ++++++++++++++++++++thread:2017-10-20 15:24:40 ++++++++++++++++++++thread:2017-10-20 15:24:43
3.兩個(gè)區(qū)別
ScheduleAtFixedRate每次執(zhí)行時(shí)間為上一次任務(wù)開(kāi)始起向后推一個(gè)時(shí)間間隔,即每次執(zhí)行時(shí)間為initialDelay,initialDelay+period,initialDelay+2*period。。。。。
ScheduleWithFixedDelay每次執(zhí)行時(shí)間為上一次任務(wù)結(jié)束起向后推一個(gè)時(shí)間間隔,即每次執(zhí)行時(shí)間為:initialDelay,initialDelay+executeTime+delay,initialDelay+2*executeTime+2*delay。。。。。
由此可見(jiàn),ScheduleAtFixedRate是基于固定時(shí)間間隔進(jìn)行任務(wù)調(diào)度,ScheduleWithFixedDelay取決于每次任務(wù)執(zhí)行的時(shí)間長(zhǎng)短,是基于不固定時(shí)間間隔進(jìn)行任務(wù)調(diào)度。
以上是“基于ScheduledExecutorService的方法有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!