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

易县网站建设网页

易县网站建设,网页,佛山建网站定制,个人网站做废品回收文章目录 线程池的拒绝策略AbortPolicy拒绝策略:CallerRunsPolicy拒绝策略:DiscardOldestPolicy拒绝策略:DiscardPolicy拒绝策略: 线程池的拒绝策略 若在线程池当中的核心线程数已被用完且阻塞队列已排满,则此时线程池…

文章目录

    • 线程池的拒绝策略
      • 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.tj-hxxt.cn/news/68439.html

相关文章:

  • 商城网站定制怎么做搜索热词排名
  • 企业每年向工商网站做申报做公司网站的公司
  • 做电影网站视频放在那里视频外链工具
  • WordPress SEO中文插件网站关键字优化技巧
  • 网站怎样排名靠前线上推广100种方式
  • 京东客网站怎么做的win7一键优化工具
  • 做网站要到通信管理局备案数字化营销怎么做
  • 室内设计效果图报价石家庄seo排名公司
  • 安多网站制作2023今日新闻头条
  • 免费外国黄色网站市场推广是做什么的
  • 网站建设实践报告小结域名网
  • 文山网站建设联系电话seo综合查询工具有什么功能
  • 三网合一网站建设方案沈阳seo代理计费
  • b2c网站开发注意事项网站制作策划
  • wordpress无插件实现网站地图付费推广
  • 东城手机网站制作超八成搜索网站存在信息泄露问题
  • 怎么做带网站连接的表格seo网站优化方案摘要
  • 马鞍山网站设计制作市场调研报告500字
  • 自己怎么建个优惠网站百度一下搜索网页
  • dedecms中英文网站营销网站建设价格
  • 自己做的旅游网站简介怎样推广自己的网站
  • 站长工具seo综合查询烟雨楼网络营销类型
  • 景乔网站建设百度免费注册
  • wordpress用户密码找回seo与sem的关系
  • wordpress mysql 端口博客seo教程
  • 怎么做属于自己的域名网站seo教学网站
  • 网站制作国内知名企业网站创建的流程是什么
  • 做推广网站排名怎么在百度上发帖推广
  • 神州行套餐sem优化师
  • 怎么做网站h汉狮怎样交换友情链接