Java線(xiàn)程池使用AbortPolicy策略
線(xiàn)程池ThreadPoolExecutor的拒絕策略
線(xiàn)程池中的線(xiàn)程資源全部被占用時(shí),對(duì)新添加的Task任務(wù)有不同的處理策略,在默認(rèn)的情況下,
ThreadPoolExecutor類(lèi)中有4種不同的處理方式:
- AbortPolicy:當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕時(shí),它將拋出RejectExecutionException異常。
- CallerRunsPolicy:當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕時(shí),會(huì)使用調(diào)用線(xiàn)程池的Thread線(xiàn)程對(duì)象處理被拒絕的任務(wù)。
- DiscardOldestPolicy: 當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕時(shí),線(xiàn)程池會(huì)放棄等待隊(duì)列中最舊的未處理任務(wù),然后將被拒絕的任務(wù)添加到等待隊(duì)列中。
- DiscardPolicy:當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕時(shí),線(xiàn)程池將丟棄被拒絕的任務(wù)。
AbortPolicy策略
AbortPolicy策略是當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕時(shí),它將拋出RejectedExecutionException異常。
線(xiàn)程執(zhí)行代碼如下:
public class FirstRunnable implements Runnable { @Override public void run() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss"); try { System.out.println(Thread.currentThread().getName() +" 開(kāi)始時(shí)間:"+simpleDateFormat.format(new Date())); Thread.sleep(1000); System.out.println(Thread.currentThread().getName() +" 結(jié)束時(shí)間:"+simpleDateFormat.format(new Date())); } catch (InterruptedException e) { e.printStackTrace(); } } }
運(yùn)行類(lèi)代碼如下:
public class AbortPolicyRun { public static void main(String[] args) { FirstRunnable firstRunnable = new FirstRunnable(); ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2), new ThreadPoolExecutor.AbortPolicy()); for (int i = 0; i < 7 ; i++) { threadPoolExecutor.execute(firstRunnable); } } }
運(yùn)行結(jié)果如下:
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.ozx.concurrentprogram.executor.service.FirstRunnable@6c629d6e rejected from java.util.concurrent.ThreadPoolExecutor@5f5a92bb[Running, pool size = 3, active threads = 3, queued tasks = 2, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at com.ozx.concurrentprogram.executor.controller.AbortPolicyRun.main(AbortPolicyRun.java:19)
pool-1-thread-3 開(kāi)始時(shí)間:16:20:27
pool-1-thread-1 開(kāi)始時(shí)間:16:20:27
pool-1-thread-2 開(kāi)始時(shí)間:16:20:27
pool-1-thread-2 結(jié)束時(shí)間:16:20:28
pool-1-thread-2 開(kāi)始時(shí)間:16:20:28
pool-1-thread-1 結(jié)束時(shí)間:16:20:28
pool-1-thread-1 開(kāi)始時(shí)間:16:20:28
pool-1-thread-3 結(jié)束時(shí)間:16:20:28
pool-1-thread-1 結(jié)束時(shí)間:16:20:29
pool-1-thread-2 結(jié)束時(shí)間:16:20:29
使用AbortPolicy策略后,線(xiàn)程任務(wù)數(shù)量超出線(xiàn)程池最大線(xiàn)程數(shù)時(shí),線(xiàn)程池將拋出java.util.concurrent.RejectedExecutionException異常。
到此這篇關(guān)于 Java線(xiàn)程池使用AbortPolicy策略的文章就介紹到這了,更多相關(guān)Java AbortPolicy內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)房屋出租系統(tǒng)詳解
這篇文章主要介紹了實(shí)現(xiàn)Java房屋出租系統(tǒng)的實(shí)現(xiàn)過(guò)程,文章條理清晰,在實(shí)現(xiàn)過(guò)程中加深了對(duì)相關(guān)概念的理解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10解決新版 Idea 中 SpringBoot 熱部署不生效的問(wèn)題
這篇文章主要介紹了解決新版 Idea 中 SpringBoot 熱部署不生效的問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08SpringMVC使用hibernate-validator進(jìn)行參數(shù)校驗(yàn)最佳實(shí)踐記錄
這篇文章主要介紹了SpringMVC使用hibernate-validator進(jìn)行參數(shù)校驗(yàn)最佳實(shí)踐,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05Java中的鎖與鎖的狀態(tài)升級(jí)詳細(xì)解讀
這篇文章主要介紹了Java中的鎖與鎖的狀態(tài)升級(jí)詳細(xì)解讀,Java 1.6以后官方針對(duì)鎖的優(yōu)化,主要是增加了兩種新的鎖:偏向鎖和輕量級(jí)鎖,再加上本身重量級(jí)鎖,那么鎖基本上可以大致分為這三種,它們之間的區(qū)別主要是體現(xiàn)在等待時(shí)間上面,需要的朋友可以參考下2024-01-01flowable動(dòng)態(tài)創(chuàng)建多級(jí)流程模板實(shí)現(xiàn)demo
這篇文章主要為大家介紹了flowable動(dòng)態(tài)創(chuàng)建多級(jí)流程模板實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Java有趣好玩的圖形界面開(kāi)發(fā)八個(gè)案例實(shí)現(xiàn)
今天使用GUI技術(shù)寫(xiě)了幾個(gè)練習(xí)的Demo,希望對(duì)大家學(xué)習(xí)圖形用戶(hù)界面有所幫助,感興趣的同學(xué)來(lái)看看吧,動(dòng)手敲一遍理解更通透2022-05-05Java詳解多線(xiàn)程協(xié)作作業(yè)之信號(hào)同步
信號(hào)量同步是指在不同線(xiàn)程之間,通過(guò)傳遞同步信號(hào)量來(lái)協(xié)調(diào)線(xiàn)程執(zhí)行的先后次序。CountDownLatch是基于時(shí)間維度的Semaphore則是基于信號(hào)維度的2022-05-05SpringBoot?項(xiàng)目的創(chuàng)建與啟動(dòng)步驟詳解
這篇文章主要介紹了SpringBoot?項(xiàng)目的創(chuàng)建與啟動(dòng),本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03