Java concurrency線(xiàn)程池之線(xiàn)程池原理(四)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
拒絕策略介紹
線(xiàn)程池的拒絕策略,是指當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕,而采取的處理措施。
當(dāng)任務(wù)添加到線(xiàn)程池中之所以被拒絕,可能是由于:第一,線(xiàn)程池異常關(guān)閉。第二,任務(wù)數(shù)量超過(guò)線(xiàn)程池的最大限制。
線(xiàn)程池共包括4種拒絕策略,它們分別是:AbortPolicy, CallerRunsPolicy, DiscardOldestPolicy和DiscardPolicy。
- AbortPolicy -- 當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕時(shí),它將拋出 RejectedExecutionException 異常。
- CallerRunsPolicy -- 當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕時(shí),會(huì)在線(xiàn)程池當(dāng)前正在運(yùn)行的Thread線(xiàn)程池中處理被拒絕的任務(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ù)。
線(xiàn)程池默認(rèn)的處理策略是AbortPolicy!
拒絕策略對(duì)比和示例
下面通過(guò)示例,分別演示線(xiàn)程池的4種拒絕策略。
1. DiscardPolicy 示例
import java.lang.reflect.Field; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy; public class DiscardPolicyDemo { private static final int THREADS_SIZE = 1; private static final int CAPACITY = 1; public static void main(String[] args) throws Exception { // 創(chuàng)建線(xiàn)程池。線(xiàn)程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線(xiàn)程池"的阻塞隊(duì)列容量為1(CAPACITY)。 ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(CAPACITY)); // 設(shè)置線(xiàn)程池的拒絕策略為"丟棄" pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); // 新建10個(gè)任務(wù),并將它們添加到線(xiàn)程池中。 for (int i = 0; i < 10; i++) { Runnable myrun = new MyRunnable("task-"+i); pool.execute(myrun); } // 關(guān)閉線(xiàn)程池 pool.shutdown(); } } class MyRunnable implements Runnable { private String name; public MyRunnable(String name) { this.name = name; } @Override public void run() { try { System.out.println(this.name + " is running."); Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } }
運(yùn)行結(jié)果:
task-0 is running. task-1 is running.
結(jié)果說(shuō)明:線(xiàn)程池pool的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),這意味著"線(xiàn)程池能同時(shí)運(yùn)行的任務(wù)數(shù)量最大只能是1"。
線(xiàn)程池pool的阻塞隊(duì)列是ArrayBlockingQueue,ArrayBlockingQueue是一個(gè)有界的阻塞隊(duì)列,ArrayBlockingQueue的容量為1。這也意味著線(xiàn)程池的阻塞隊(duì)列只能有一個(gè)線(xiàn)程池阻塞等待。
根據(jù)""中分析的execute()代碼可知:線(xiàn)程池中共運(yùn)行了2個(gè)任務(wù)。第1個(gè)任務(wù)直接放到Worker中,通過(guò)線(xiàn)程去執(zhí)行;第2個(gè)任務(wù)放到阻塞隊(duì)列中等待。其他的任務(wù)都被丟棄了!
2. DiscardOldestPolicy 示例
import java.lang.reflect.Field; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy; public class DiscardOldestPolicyDemo { private static final int THREADS_SIZE = 1; private static final int CAPACITY = 1; public static void main(String[] args) throws Exception { // 創(chuàng)建線(xiàn)程池。線(xiàn)程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線(xiàn)程池"的阻塞隊(duì)列容量為1(CAPACITY)。 ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(CAPACITY)); // 設(shè)置線(xiàn)程池的拒絕策略為"DiscardOldestPolicy" pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy()); // 新建10個(gè)任務(wù),并將它們添加到線(xiàn)程池中。 for (int i = 0; i < 10; i++) { Runnable myrun = new MyRunnable("task-"+i); pool.execute(myrun); } // 關(guān)閉線(xiàn)程池 pool.shutdown(); } } class MyRunnable implements Runnable { private String name; public MyRunnable(String name) { this.name = name; } @Override public void run() { try { System.out.println(this.name + " is running."); Thread.sleep(200); } catch (Exception e) { e.printStackTrace(); } } }
運(yùn)行結(jié)果:
task-0 is running. task-9 is running.
結(jié)果說(shuō)明:將"線(xiàn)程池的拒絕策略"由DiscardPolicy修改為DiscardOldestPolicy之后,當(dāng)有任務(wù)添加到線(xiàn)程池被拒絕時(shí),線(xiàn)程池會(huì)丟棄阻塞隊(duì)列中末尾的任務(wù),然后將被拒絕的任務(wù)添加到末尾。
3. AbortPolicy 示例
import java.lang.reflect.Field; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.ThreadPoolExecutor.AbortPolicy; import java.util.concurrent.RejectedExecutionException; public class AbortPolicyDemo { private static final int THREADS_SIZE = 1; private static final int CAPACITY = 1; public static void main(String[] args) throws Exception { // 創(chuàng)建線(xiàn)程池。線(xiàn)程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線(xiàn)程池"的阻塞隊(duì)列容量為1(CAPACITY)。 ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(CAPACITY)); // 設(shè)置線(xiàn)程池的拒絕策略為"拋出異常" pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); try { // 新建10個(gè)任務(wù),并將它們添加到線(xiàn)程池中。 for (int i = 0; i < 10; i++) { Runnable myrun = new MyRunnable("task-"+i); pool.execute(myrun); } } catch (RejectedExecutionException e) { e.printStackTrace(); // 關(guān)閉線(xiàn)程池 pool.shutdown(); } } } class MyRunnable implements Runnable { private String name; public MyRunnable(String name) { this.name = name; } @Override public void run() { try { System.out.println(this.name + " is running."); Thread.sleep(200); } catch (Exception e) { e.printStackTrace(); } } }
(某一次)運(yùn)行結(jié)果:
java.util.concurrent.RejectedExecutionException at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1774) at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:768) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:656) at AbortPolicyDemo.main(AbortPolicyDemo.java:27) task-0 is running. task-1 is running.
結(jié)果說(shuō)明:將"線(xiàn)程池的拒絕策略"由DiscardPolicy修改為AbortPolicy之后,當(dāng)有任務(wù)添加到線(xiàn)程池被拒絕時(shí),會(huì)拋出RejectedExecutionException。
4. CallerRunsPolicy 示例
import java.lang.reflect.Field; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; public class CallerRunsPolicyDemo { private static final int THREADS_SIZE = 1; private static final int CAPACITY = 1; public static void main(String[] args) throws Exception { // 創(chuàng)建線(xiàn)程池。線(xiàn)程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線(xiàn)程池"的阻塞隊(duì)列容量為1(CAPACITY)。 ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(CAPACITY)); // 設(shè)置線(xiàn)程池的拒絕策略為"CallerRunsPolicy" pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 新建10個(gè)任務(wù),并將它們添加到線(xiàn)程池中。 for (int i = 0; i < 10; i++) { Runnable myrun = new MyRunnable("task-"+i); pool.execute(myrun); } // 關(guān)閉線(xiàn)程池 pool.shutdown(); } } class MyRunnable implements Runnable { private String name; public MyRunnable(String name) { this.name = name; } @Override public void run() { try { System.out.println(this.name + " is running."); Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } }
(某一次)運(yùn)行結(jié)果:
task-2 is running. task-3 is running. task-4 is running. task-5 is running. task-6 is running. task-7 is running. task-8 is running. task-9 is running. task-0 is running. task-1 is running.
結(jié)果說(shuō)明:將"線(xiàn)程池的拒絕策略"由DiscardPolicy修改為CallerRunsPolicy之后,當(dāng)有任務(wù)添加到線(xiàn)程池被拒絕時(shí),線(xiàn)程池會(huì)將被拒絕的任務(wù)添加到"線(xiàn)程池正在運(yùn)行的線(xiàn)程"中取運(yùn)行
相關(guān)文章
Java實(shí)現(xiàn)各種文件類(lèi)型轉(zhuǎn)換方式(收藏)
這篇文章主要介紹了Java?各種文件類(lèi)型轉(zhuǎn)換的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03MyBatis-Plus通用枚舉自動(dòng)關(guān)聯(lián)注入的實(shí)現(xiàn)
本文主要介紹了MyBatis-Plus通用枚舉自動(dòng)關(guān)聯(lián)注入的實(shí)現(xiàn),解決了繁瑣的配置,讓 mybatis 優(yōu)雅的使用枚舉屬性,感興趣的可以一起來(lái)了解一下2021-06-06使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯(cuò)誤的解決
這篇文章主要介紹了使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯(cuò)誤的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08java使用單向鏈表解決數(shù)據(jù)存儲(chǔ)自定義排序問(wèn)題
本文主要介紹了java使用單向鏈表解決數(shù)據(jù)存儲(chǔ)自定義排序問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Java之?dāng)?shù)組在指定位置插入元素實(shí)現(xiàn)
本文主要介紹了Java之?dāng)?shù)組在指定位置插入元素實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Java中的有限狀態(tài)機(jī)(設(shè)計(jì)模式——狀態(tài)模式)
這篇文章主要介紹了Java中的有限狀態(tài)機(jī)(設(shè)計(jì)模式——狀態(tài)模式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08JDBC連接MySQL數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)過(guò)程詳解
這篇文章主要介紹了JDBC連接MySQL數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Java技巧分享之利用RxJava打造可觀測(cè)數(shù)據(jù)RxLiveData
這篇文章主要來(lái)和大家分享一個(gè)Java技巧,那就是利用RxJava打造可觀測(cè)數(shù)據(jù)RxLiveData,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-06-06