Java線程池使用AbortPolicy策略
線程池ThreadPoolExecutor的拒絕策略
線程池中的線程資源全部被占用時,對新添加的Task任務(wù)有不同的處理策略,在默認的情況下,
ThreadPoolExecutor類中有4種不同的處理方式:
- AbortPolicy:當任務(wù)添加到線程池中被拒絕時,它將拋出RejectExecutionException異常。
- CallerRunsPolicy:當任務(wù)添加到線程池中被拒絕時,會使用調(diào)用線程池的Thread線程對象處理被拒絕的任務(wù)。
- DiscardOldestPolicy: 當任務(wù)添加到線程池中被拒絕時,線程池會放棄等待隊列中最舊的未處理任務(wù),然后將被拒絕的任務(wù)添加到等待隊列中。
- DiscardPolicy:當任務(wù)添加到線程池中被拒絕時,線程池將丟棄被拒絕的任務(wù)。
AbortPolicy策略
AbortPolicy策略是當任務(wù)添加到線程池中被拒絕時,它將拋出RejectedExecutionException異常。
線程執(zhí)行代碼如下:
public class FirstRunnable implements Runnable {
@Override
public void run() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
try {
System.out.println(Thread.currentThread().getName() +" 開始時間:"+simpleDateFormat.format(new Date()));
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() +" 結(jié)束時間:"+simpleDateFormat.format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}運行類代碼如下:
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);
}
}
}運行結(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 開始時間:16:20:27
pool-1-thread-1 開始時間:16:20:27
pool-1-thread-2 開始時間:16:20:27
pool-1-thread-2 結(jié)束時間:16:20:28
pool-1-thread-2 開始時間:16:20:28
pool-1-thread-1 結(jié)束時間:16:20:28
pool-1-thread-1 開始時間:16:20:28
pool-1-thread-3 結(jié)束時間:16:20:28
pool-1-thread-1 結(jié)束時間:16:20:29
pool-1-thread-2 結(jié)束時間:16:20:29
使用AbortPolicy策略后,線程任務(wù)數(shù)量超出線程池最大線程數(shù)時,線程池將拋出java.util.concurrent.RejectedExecutionException異常。
到此這篇關(guān)于 Java線程池使用AbortPolicy策略的文章就介紹到這了,更多相關(guān)Java AbortPolicy內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
BufferedInputStream(緩沖輸入流)詳解_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細介紹了BufferedInputStream緩沖輸入流的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Spring-cloud Config Server的3種配置方式
這篇文章主要介紹了Spring-cloud Config Server的3種配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
詳解IDEA使用Maven項目不能加入本地Jar包的解決方法
這篇文章主要介紹了詳解IDEA使用Maven項目不能加入本地Jar包的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
SpringCloud集成zookeeper實現(xiàn)服務(wù)注冊并訪問功能
zookeeper和eureka一樣,是用于充當服務(wù)注冊功能服務(wù)器的一個springcloud插件,這篇文章主要介紹了SpringCloud集成zookeeper實現(xiàn)服務(wù)注冊并訪問,需要的朋友可以參考下2022-06-06

