线程池饱和策略
2019獨角獸企業重金招聘Python工程師標準>>>
1.Abort策略:默認策略,新任務提交時直接拋出未檢查的異常RejectedExecutionException,該異常可由調用者捕獲。
new ThreadPoolExecutor.AbortPolicy()2.CallerRuns策略:為調節機制,既不拋棄任務也不拋出異常,而是將某些任務回退到調用者。不會在線程池的線程中執行新的任務,而是在調用exector的線程中運行新的任務。
new ThreadPoolExecutor.CallerRunsPolicy()3.Discard策略:新提交的任務被拋棄。
new ThreadPoolExecutor.DiscardPolicy()4.DiscardOldest策略:隊列的是“隊頭”的任務,然后嘗試提交新的任務。對頭任務被丟棄(不適合工作隊列為優先隊列場景)
new ThreadPoolExecutor.DiscardOldestPolicy()5.自定義飽和處理策略
import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor;/*** 循環,當隊列有空位時,該任務進入隊列,等待線程池處理*/ public class TestRejectedExecutionHandler implements RejectedExecutionHandler {public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {try {executor.getQueue().put(r);} catch (InterruptedException e) {e.printStackTrace();}}}?
轉載于:https://my.oschina.net/u/943316/blog/1601595
總結
- 上一篇: PHP百杂
- 下一篇: 为什么说dubbo的声明式缓存不好用!!