当前位置: 首页 > news >正文

线程池的拒绝策略

文章目录

    • 线程池的拒绝策略
      • 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 接口,来实现拒绝策略,若无法满足实际需要,则用户就可以自己自定义来实现拒绝策略。

http://www.lryc.cn/news/252109.html

相关文章:

  • Redis7--基础篇5(管道、发布订阅)
  • Unity中Shader指令优化(编译后指令解析)
  • 单个 Zip 文件体积超过 40GB
  • pandas 基础操作3
  • 开发知识点-Maven包管理工具
  • 104. 二叉树的最大深度
  • JAVA毕业设计113—基于Java+Springboot+Vue的体育馆预约系统(源代码+数据库+12000字论文)
  • 【自动化测试】pytest 用例执行中print日志实时输出
  • 【深度学习】KMeans中自动K值的确认方法
  • github问题解决(持续更新中)
  • 如何创建一个vue工程
  • 50 代码审计-PHP无框架项目SQL注入挖掘技巧
  • 基于Spring、SpringMVC、MyBatis的企业博客网站
  • spring日志输出到elasticsearch
  • 谷歌 Gemini 模型发布计划推迟:无法可靠处理部分非英语沟通
  • Ubuntu显卡及内核更新问题
  • SpringBoot错误处理机制解析
  • 牛客剑指offer刷题模拟篇
  • Locust单机多核压测,以及主从节点的数据通信处理!
  • ERROR: [pool www] please specify user and group other than root
  • 京东商品详情接口在电商行业中的重要性及实时数据获取实现
  • WT2003H MP3语音芯片方案:强大、灵活且易于集成的音频解决方案
  • 机器学习深度学学习分类模型中常用的评价指标总结记录与代码实现说明
  • fastapi 后端项目目录结构 mysql fastapi 数据库操作
  • 研习代码 day47 | 动态规划——子序列问题3
  • L1-017:到底有多二
  • Python多线程使用(二)
  • 记录一次docker搭建tomcat容器的网页不能访问的问题
  • GPT3年终总结
  • Kafka生产者发送消息的流程