文章目錄
- 線程池的拒絕策略
- AbortPolicy拒絕策略:
- CallerRunsPolicy拒絕策略:
- DiscardOldestPolicy拒絕策略:
- DiscardPolicy拒絕策略:
線程池的拒絕策略
若在線程池當中的核心線程數已被用完且阻塞隊列已排滿,則此時線程池的線程資源已耗盡,線程池沒有足夠的線程資源執行新的任務。
所以為了保證操作系統的安全性,線程池將通過拒絕策略來處理新添加的線程任務。
JDK 中內置的拒絕策略有 AbortPolicy,CallerRunsPolicy、DiscardOldestPolicy、DiscardPolicy 這4種,默認的拒絕策略在 ThreadPoolExecutor 中作為內部類來進行提供的,在默認的拒絕策略都不能滿足應用的需求時,也可以自定義拒絕策略。
AbortPolicy拒絕策略:
該策略會直接拋出異常,阻止系統正常工作。
jdk源碼:
/*** A handler for rejected tasks that throws a* {@code RejectedExecutionException}.*/public static class AbortPolicy implements RejectedExecutionHandler {/*** Creates an {@code AbortPolicy}.*/public AbortPolicy() { }/*** Always throws RejectedExecutionException.** @param r the runnable task requested to be executed* @param e the executor attempting to execute this task* @throws RejectedExecutionException always*/public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {throw new RejectedExecutionException("Task " + r.toString() +" rejected from " +e.toString());}}
CallerRunsPolicy拒絕策略:
如果線程池的線程數量達到上限,該策略會把任務隊列中的任務放在調用者線程(如main函數)當中運行。
jdk源碼:
/*** A handler for rejected tasks that runs the rejected task* directly in the calling thread of the {@code execute} method,* unless the executor has been shut down, in which case the task* is discarded.*/public static class CallerRunsPolicy implements RejectedExecutionHandler {/*** Creates a {@code CallerRunsPolicy}.*/public CallerRunsPolicy() { }/*** Executes task r in the caller's thread, unless the executor* has been shut down, in which case the task is discarded.** @param r the runnable task requested to be executed* @param e the executor attempting to execute this task*/public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {if (!e.isShutdown()) {r.run();}}}
DiscardOldestPolicy拒絕策略:
該策略將移除最早的一個請求,也就是即將被執 行的任務,然后并嘗試再次提交當前的任務。
jdk源碼:
/*** A handler for rejected tasks that discards the oldest unhandled* request and then retries {@code execute}, unless the executor* is shut down, in which case the task is discarded.*/public static class DiscardOldestPolicy implements RejectedExecutionHandler {/*** Creates a {@code DiscardOldestPolicy} for the given executor.*/public DiscardOldestPolicy() { }/*** Obtains and ignores the next task that the executor* would otherwise execute, if one is immediately available,* and then retries execution of task r, unless the executor* is shut down, in which case task r is instead discarded.** @param r the runnable task requested to be executed* @param e the executor attempting to execute this task*/public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {if (!e.isShutdown()) {e.getQueue().poll();e.execute(r);}}}
DiscardPolicy拒絕策略:
丟棄當前線程任務而不做任何處理。如果系統允許在資源不足的情況下丟棄部分任務,則這將是保障系統安全,穩定的一種很好的方案。
jdk源碼:
/*** A handler for rejected tasks that silently discards the* rejected task.*/public static class DiscardPolicy implements RejectedExecutionHandler {/*** Creates a {@code DiscardPolicy}.*/public DiscardPolicy() { }/*** Does nothing, which has the effect of discarding task r.** @param r the runnable task requested to be executed* @param e the executor attempting to execute this task*/public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {}}
以上4種拒絕策略均是實現的 RejectedExecutionHandler 接口,來實現拒絕策略,若無法滿足實際需要,則用戶就可以自己自定義來實現拒絕策略。