序
備忘下線程池RejectedExecutionHandler的源碼。
CallerRunsPolicy/** * 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(); } } }AbortPolicy
/** * 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()); } }DiscardPolicy
/** * 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) { } }DiscardOldestPolicy
/** * 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); } } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/65779.html
摘要:將線程池狀態(tài)置為并不會立即停止,停止接收外部的任務,內(nèi)部正在跑的任務和隊列里等待的任務,會執(zhí)行完,才真正停止。將線程池狀態(tài)置為。 在Java中,我們經(jīng)常使用的線程池就是ThreadPoolExecutor,此外還有定時的線程池ScheduledExecutorService(),但是需要注意的是Executors.newCachedThreadPool()的線程是沒有上屆的,在使用時,...
摘要:當活動線程核心線程非核心線程達到這個數(shù)值后,后續(xù)任務將會根據(jù)來進行拒絕策略處理。線程池工作原則當線程池中線程數(shù)量小于則創(chuàng)建線程,并處理請求。當線程池中的數(shù)量等于最大線程數(shù)時默默丟棄不能執(zhí)行的新加任務,不報任何異常。 spring-cache使用記錄 spring-cache的使用記錄,坑點記錄以及采用的解決方案 深入分析 java 線程池的實現(xiàn)原理 在這篇文章中,作者有條不紊的將 ja...
摘要:創(chuàng)建方法最大線程數(shù)即源碼單線程化的線程池有且僅有一個工作線程執(zhí)行任務所有任務按照指定順序執(zhí)行,即遵循隊列的入隊出隊規(guī)則創(chuàng)建方法源碼還有一個結(jié)合了和,就不介紹了,基本不用。 *本篇文章已授權(quán)微信公眾號 guolin_blog (郭霖)獨家發(fā)布 為什么用線程池 創(chuàng)建/銷毀線程伴隨著系統(tǒng)開銷,過于頻繁的創(chuàng)建/銷毀線程,會很大程度上影響處理效率 >例如: > >記創(chuàng)建線程消耗時間T1,執(zhí)行...
摘要:任務性質(zhì)不同的任務可以用不同規(guī)模的線程池分開處理。線程池在運行過程中已完成的任務數(shù)量。如等于線程池的最大大小,則表示線程池曾經(jīng)滿了。線程池的線程數(shù)量。獲取活動的線程數(shù)。通過擴展線程池進行監(jiān)控??蚣馨ň€程池,,,,,,等。 Java線程池 [toc] 什么是線程池 線程池就是有N個子線程共同在運行的線程組合。 舉個容易理解的例子:有個線程組合(即線程池,咱可以比喻為一個公司),里面有3...
摘要:介紹線程池一般包含三個主要部分調(diào)度器決定由哪個線程來執(zhí)行任務執(zhí)行任務所能夠的最大耗時等線程隊列存放并管理著一系列線程這些線程都處于阻塞狀態(tài)或休眠狀態(tài)任務隊列存放著用戶提交的需要被執(zhí)行的任務一般任務的執(zhí)行的即先提交的任務先被執(zhí)行調(diào)度器并非是必 介紹 線程池一般包含三個主要部分: 調(diào)度器: 決定由哪個線程來執(zhí)行任務, 執(zhí)行任務所能夠的最大耗時等 線程隊列: 存放并管理著一系列線程, 這些...
閱讀 3805·2023-01-11 11:02
閱讀 4308·2023-01-11 11:02
閱讀 3132·2023-01-11 11:02
閱讀 5240·2023-01-11 11:02
閱讀 4804·2023-01-11 11:02
閱讀 5578·2023-01-11 11:02
閱讀 5384·2023-01-11 11:02
閱讀 4084·2023-01-11 11:02