Java多線程4種拒絕策略小結(jié)
一、簡介
在Java多線程編程中,我們通常使用線程池來管理和調(diào)度任務(wù)。線程池由一組預(yù)先創(chuàng)建的線程組成,可以重復(fù)利用這些線程來執(zhí)行多個任務(wù),避免頻繁地創(chuàng)建和銷毀線程而帶來的性能開銷。
當(dāng)線程池中的任務(wù)隊(duì)列已滿且無法再接受新的任務(wù)時,就需要采取拒絕策略來處理這種情況。拒絕策略定義了當(dāng)無法再接受新的任務(wù)時如何處理這些被拒絕的任務(wù)。
Java提供了四種常見的拒絕策略:
AbortPolicy(拋出異常):默認(rèn)的拒絕策略。當(dāng)任務(wù)無法被提交給線程池時,會直接拋出RejectedExecutionException異常。
CallerRunsPolicy(調(diào)用者運(yùn)行):當(dāng)任務(wù)無法被提交給線程池時,會由提交任務(wù)的線程自己執(zhí)行該任務(wù)。
DiscardPolicy(直接丟棄):當(dāng)任務(wù)無法被提交給線程池時,直接丟棄該任務(wù),沒有任何提示或處理。
DiscardOldestPolicy(丟棄最舊任務(wù)):當(dāng)任務(wù)無法被提交給線程池時,會丟棄隊(duì)列中最早的一個任務(wù),然后嘗試再次提交當(dāng)前任務(wù)。
二、AbortPolicy拒絕策略
A. 概述
AbortPolicy是ThreadPoolExecutor的默認(rèn)拒絕策略,當(dāng)任務(wù)無法被提交給線程池時,會直接拋出RejectedExecutionException異常。
B. 拒絕策略實(shí)現(xiàn)原理
實(shí)現(xiàn)RejectedExecutionHandler接口,在rejectedExecution方法中拋出異常。
public class AbortPolicy implements RejectedExecutionHandler { public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString()); } }
C. 應(yīng)用場景
適用于對任務(wù)提交失敗要求敏感的場景,需要明確知道任務(wù)是否被接受并執(zhí)行。
D. 使用示例
當(dāng)線程池的任務(wù)隊(duì)列和線程隊(duì)列都已滿的情況下執(zhí)行決絕策略
public class Task implements Runnable { private final int index; public Task(int index) { this.index = index; } @Override public void run() { System.out.println(Thread.currentThread().getName() + ":" + index); } } public class Main { public static void main(String[] args) { // 創(chuàng)建線程池 ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1), new ThreadPoolExecutor.AbortPolicy()); try { // 提交任務(wù) threadPool.execute(new Task(1)); threadPool.execute(new Task(2)); threadPool.execute(new Task(3)); } catch (RejectedExecutionException e) { e.printStackTrace(); } finally { // 關(guān)閉線程池 threadPool.shutdown(); } } }
三、CallerRunsPolicy拒絕策略
A. 概述
CallerRunsPolicy是一種簡單的拒絕策略,當(dāng)任務(wù)無法被提交給線程池時,會由提交任務(wù)的線程自己執(zhí)行該任務(wù)。
B. 拒絕策略實(shí)現(xiàn)原理
實(shí)現(xiàn)RejectedExecutionHandler接口,在rejectedExecution方法中使用提交任務(wù)的線程來執(zhí)行任務(wù)。
public class CallerRunsPolicy implements RejectedExecutionHandler { public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { r.run(); } } }
C. 應(yīng)用場景
適用于對任務(wù)提交失敗要求較低的場景,通過調(diào)用線程來執(zhí)行任務(wù),避免任務(wù)丟失。
D. 使用示例
public class Task implements Runnable { private final int index; public Task(int index) { this.index = index; } @Override public void run() { System.out.println(Thread.currentThread().getName() + ":" + index); } }
public class Main { public static void main(String[] args) { // 創(chuàng)建線程池 ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1), new ThreadPoolExecutor.CallerRunsPolicy()); try { // 提交任務(wù) threadPool.execute(new Task(1)); threadPool.execute(new Task(2)); threadPool.execute(new Task(3)); } catch (RejectedExecutionException e) { e.printStackTrace(); } finally { // 關(guān)閉線程池 threadPool.shutdown(); } } }
四、DiscardPolicy拒絕策略
A. 概述
DiscardPolicy是一種簡單的拒絕策略,當(dāng)任務(wù)無法被提交給線程池時,會直接丟棄該任務(wù),沒有任何提示或處理。
B. 拒絕策略實(shí)現(xiàn)原理
實(shí)現(xiàn)RejectedExecutionHandler接口,在rejectedExecution方法中不做任何操作,即丟棄任務(wù)。
public class DiscardPolicy implements RejectedExecutionHandler { public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { // Do nothing, discard the task } }
C. 應(yīng)用場景
適用于對任務(wù)提交失敗不敏感的場景,對任務(wù)丟失沒有特殊要求。
D. 使用示例
public class Task implements Runnable { private final int index; public Task(int index) { this.index = index; } @Override public void run() { System.out.println(Thread.currentThread().getName() + ":" + index); } }
public class Main { public static void main(String[] args) { // 創(chuàng)建線程池 ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1), new ThreadPoolExecutor.DiscardOldestPolicy()); try { // 提交任務(wù) threadPool.execute(new Task(1)); threadPool.execute(new Task(2)); threadPool.execute(new Task(3)); } catch (RejectedExecutionException e) { e.printStackTrace(); } finally { // 關(guān)閉線程池 threadPool.shutdown(); } } }
五、DiscardOldestPolicy拒絕策略
A. 概述
DiscardOldestPolicy是一種拒絕策略,當(dāng)任務(wù)無法被提交給線程池時,會丟棄最早的一個任務(wù),然后嘗試再次提交。
B. 拒絕策略實(shí)現(xiàn)原理
實(shí)現(xiàn)RejectedExecutionHandler接口,在rejectedExecution方法中從隊(duì)列中獲取最早的任務(wù)并丟棄,再次提交當(dāng)前任務(wù)。
public class DiscardOldestPolicy implements RejectedExecutionHandler { public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { e.getQueue().poll(); e.execute(r); } } }
C. 應(yīng)用場景
適用于對新任務(wù)優(yōu)先級比較高的場景,可以丟棄舊的任務(wù)以保證及時處理新任務(wù)。
D. 使用示例
public class Task implements Runnable { private final int index; public Task(int index) { this.index = index; } @Override public void run() { System.out.println(Thread.currentThread().getName() + ":" + index); } }
public class Main { public static void main(String[] args) { // 創(chuàng)建線程池 ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(2), new ThreadPoolExecutor.DiscardOldestPolicy()); try { // 提交任務(wù) threadPool.execute(new Task(1)); threadPool.execute(new Task(2)); threadPool.execute(new Task(3)); threadPool.execute(new Task(4)); } catch (RejectedExecutionException e) { e.printStackTrace(); } finally { // 關(guān)閉線程池 threadPool.shutdown(); } } }
六、總結(jié)
各種拒絕策略的特點(diǎn)和適用場景
- AbortPolicy:對任務(wù)提交失敗要求敏感,需要明確知道任務(wù)是否被接受并執(zhí)行。
- CallerRunsPolicy:對任務(wù)提交失敗要求較低,通過調(diào)用線程來執(zhí)行任務(wù),避免任務(wù)丟失。
- DiscardPolicy:對任務(wù)提交失敗不敏感,對任務(wù)丟失沒有特殊要求。
- DiscardOldestPolicy:適用于新任務(wù)優(yōu)先級高,丟棄舊任務(wù)以保證及時處理新任務(wù)。
到此這篇關(guān)于Java多線程4種拒絕策略小姐的文章就介紹到這了,更多相關(guān)Java多線程拒絕策略內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)的xml格式化實(shí)現(xiàn)代碼
這篇文章主要介紹了java實(shí)現(xiàn)的xml格式化實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-11-11Java SpringBoot啟動指定profile的8種方式詳解
這篇文章主要介紹了spring boot 如何指定profile啟動的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Spring Aop之AspectJ注解配置實(shí)現(xiàn)日志管理的方法
下面小編就為大家分享一篇Spring Aop之AspectJ注解配置實(shí)現(xiàn)日志管理的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Springboot 整合 Java DL4J 打造文本摘要生成系統(tǒng)
本文介紹了如何使用SpringBoot整合JavaDeeplearning4j構(gòu)建文本摘要生成系統(tǒng),該系統(tǒng)能夠自動從長篇文本中提取關(guān)鍵信息,生成簡潔的摘要,幫助用戶快速了解文本的主要內(nèi)容,技術(shù)實(shí)現(xiàn)包括使用LSTM神經(jīng)網(wǎng)絡(luò)進(jìn)行模型構(gòu)建和訓(xùn)練,并通過SpringBoot集成RESTfulAPI接口2024-11-11java Callable接口和Future接口創(chuàng)建線程示例詳解
這篇文章主要為大家介紹了java Callable接口和Future接口創(chuàng)建線程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11