网站建设需要的费用,科技为了上大学上交可控核聚变,wordpress 重置,wordpress 模板 橱窗文章目录 线程池的拒绝策略AbortPolicy拒绝策略#xff1a;CallerRunsPolicy拒绝策略#xff1a;DiscardOldestPolicy拒绝策略#xff1a;DiscardPolicy拒绝策略#xff1a; 线程池的拒绝策略
若在线程池当中的核心线程数已被用完且阻塞队列已排满#xff0c;则此时线程池… 文章目录 线程池的拒绝策略AbortPolicy拒绝策略CallerRunsPolicy拒绝策略DiscardOldestPolicy拒绝策略DiscardPolicy拒绝策略 线程池的拒绝策略
若在线程池当中的核心线程数已被用完且阻塞队列已排满则此时线程池的线程资源已耗尽线程池没有足够的线程资源执行新的任务。
所以为了保证操作系统的安全性线程池将通过拒绝策略来处理新添加的线程任务。
JDK 中内置的拒绝策略有 AbortPolicyCallerRunsPolicy、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 callers 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 接口来实现拒绝策略若无法满足实际需要则用户就可以自己自定义来实现拒绝策略。