免费A级毛片无码专区网站-成人国产精品视频一区二区-啊 日出水了 用力乖乖在线-国产黑色丝袜在线观看下-天天操美女夜夜操美女-日韩网站在线观看中文字幕-AV高清hd片XXX国产-亚洲av中文字字幕乱码综合-搬开女人下面使劲插视频

補(bǔ)充部分---ScheduledThreadPoolExecutor類分析 線程池底層原理詳解與源碼分析

【1】前言
本篇幅是對(duì) 線程池底層原理詳解與源碼分析  的補(bǔ)充,默認(rèn)你已經(jīng)看完了上一篇對(duì)ThreadPoolExecutor類有了足夠的了解 。
【2】ScheduledThreadPoolExecutor的介紹
1.ScheduledThreadPoolExecutor繼承自ThreadPoolExecutor 。它主要用來(lái)在給定的延遲之后運(yùn)行任務(wù),或者定期執(zhí)行任務(wù) 。ScheduledThreadPoolExecutor可以在構(gòu)造函數(shù)中指定多個(gè)對(duì)應(yīng)的后臺(tái)線程數(shù) 。
2.構(gòu)造函數(shù)展示
public ScheduledThreadPoolExecutor(int corePoolSize) {super(corePoolSize, Integer.MAX_VALUE,DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,new DelayedWorkQueue());}public ScheduledThreadPoolExecutor(int corePoolSize,ThreadFactory threadFactory) {super(corePoolSize, Integer.MAX_VALUE,DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,new DelayedWorkQueue(), threadFactory);}public ScheduledThreadPoolExecutor(int corePoolSize,RejectedExecutionHandler handler) {super(corePoolSize, Integer.MAX_VALUE,DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,new DelayedWorkQueue(), handler);}public ScheduledThreadPoolExecutor(int corePoolSize,ThreadFactory threadFactory,RejectedExecutionHandler handler) {super(corePoolSize, Integer.MAX_VALUE,DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,new DelayedWorkQueue(), threadFactory, handler);}3.通過(guò)構(gòu)造函數(shù)我們可以看到,它的線程池本身就是調(diào)用ThreadPoolExecutor類的構(gòu)造方法,因此也繼承了ThreadPoolExecutor類所存在的隱患:
允許的請(qǐng)求隊(duì)列長(zhǎng)度為 Integer.MAX_VALUE,可能會(huì)堆積大量的請(qǐng)求,從而導(dǎo)致 OOM 。允許的創(chuàng)建線程數(shù)量為 Integer.MAX_VALUE,可能會(huì)創(chuàng)建大量的線程,從而導(dǎo)致 OOM 。(且CPU會(huì)變成100%)
4.PS:既然隱患這么嚴(yán)重,使用原生的不太合適 。正所謂,人無(wú)橫財(cái)不富,馬無(wú)夜草不肥,打不過(guò)就加入 。ScheduledThreadPoolExecutor繼承自ThreadPoolExecutor,那就寫個(gè)類繼承它然后調(diào)用ThreadPoolExecutor的構(gòu)造方法區(qū)解決掉創(chuàng)建線程數(shù)被寫死為最大值的情況,然后了解一下DelayedWorkQueue(這個(gè)本質(zhì)上也是優(yōu)先級(jí)隊(duì)列),繼承一下也改寫吧 。畢竟自己的最合適不是嗎 ?!井吘刮矣X得這些都是大佬們留給菜雞的底版,如拒絕策略不也是四個(gè)默認(rèn)都沒人用嗎,都是要你根據(jù)自己的場(chǎng)景改】(畢竟我這猜測(cè)的原因是因?yàn)橛辛藷o(wú)盡隊(duì)列,其實(shí)線程數(shù)設(shè)置為Integer.MAX_VALUE已經(jīng)沒有意義了)
【3】ScheduledThreadPoolExecutor的使用
1)schedule(Runnable command, long delay, TimeUnit unit)
方法說(shuō)明:無(wú)返回值的延遲任務(wù),有個(gè)嚴(yán)重的問題,就是沒有辦法獲知task的執(zhí)行結(jié)果2)schedule(Callable callable, long delay, TimeUnit unit)
方法說(shuō)明:有返回值的延遲任務(wù) :接收的是Callable實(shí)例,會(huì)返回一個(gè)ScheduleFuture對(duì)象,通過(guò)ScheduleFuture可以取消一個(gè)未執(zhí)行的task,也可以獲得這個(gè)task的執(zhí)行結(jié)果3)scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
方法說(shuō)明: 固定頻率周期任務(wù):第一次執(zhí)行的延遲根據(jù)initialDelay參數(shù)確定,以后每一次執(zhí)行都間隔period時(shí)長(zhǎng)4)scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
方法說(shuō)明: 固定延遲周期任務(wù) :scheduleWithFixedDelay的參數(shù)和scheduleAtFixedRate參數(shù)完全一致,它們的不同之處在于對(duì)period調(diào)度周期的解釋 。在scheduleAtFixedRate中,period指的兩個(gè)任務(wù)開始執(zhí)行的時(shí)間間隔,也就是當(dāng)前任務(wù)的開始執(zhí)行時(shí)間和下個(gè)任務(wù)的開始執(zhí)行時(shí)間之間的間隔 。而在scheduleWithFixedDelay中,period指的當(dāng)前任務(wù)的結(jié)束執(zhí)行時(shí)間到下個(gè)任務(wù)的開始執(zhí)行時(shí)間 。
【4】任務(wù)ScheduledFutureTask類源碼分析
1.構(gòu)造方法展示
代碼展示
private class ScheduledFutureTask<V> extends FutureTask<V> implements RunnableScheduledFuture<V> {...ScheduledFutureTask(Runnable r, V result, long triggerTime, long sequenceNumber) {super(r, result);this.time = triggerTime; //表示這個(gè)任務(wù)將要被執(zhí)行的具體時(shí)間this.period = 0;//表示任務(wù)執(zhí)行的間隔周期this.sequenceNumber = sequenceNumber;//表示這個(gè)任務(wù)被添加到ScheduledThreadPoolExecutor中的序號(hào)(采用AtomicLong原子類累加當(dāng)做序號(hào))}ScheduledFutureTask(Runnable r, V result, long triggerTime, long period, long sequenceNumber) {super(r, result);this.time = triggerTime;this.period = period;this.sequenceNumber = sequenceNumber;}ScheduledFutureTask(Callable<V> callable, long triggerTime, long sequenceNumber) {super(callable);this.time = triggerTime;this.period = 0;this.sequenceNumber = sequenceNumber;} ...}

經(jīng)驗(yàn)總結(jié)擴(kuò)展閱讀