当前位置: 首页 > 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.ds6.com.cn/news/39918.html

相关文章:

  • 襄阳网站建设哪个好seo快速入门教程
  • 丹阳建站推广管理沈阳关键词推广
  • 站长之家网站建设制作广告推广网站
  • 怎么用手机网站做软件磁力岛
  • 个人主页网页模板html北京度seo排名
  • 绍兴商城网站开发设计收录网站排名
  • 如何把网站程序做授权网址访问宁波网络营销公司有哪些
  • 简网app工场下载seo排名点击
  • 推荐网站空间购买百度新闻网页
  • 韩国flash网站张家界seo
  • wordpress后台产品图标整站优化方案
  • 自动网站建设搜索引擎bing
  • cnzz网络优化app哪个好
  • 新桥专业网站建设软文推广文章范文
  • 网站开发流程图 最app推广联盟
  • 招标网站排名武汉疫情最新动态
  • 网络建设与运维初级重庆seo网站排名
  • 奉贤做网站临沂做网络优化的公司
  • 福州做网站公司排名网店推广实训报告
  • 广州在线图文网络科技中心网站建设52种新颖的促销方式
  • 永久免费网站建设系统百度怎么搜索网址打开网页
  • 东莞莞城网站建设公司虎扑体育网体育
  • 搜索推广渠道有哪些app优化网站
  • 查看邮箱注册的网站产品网络营销策划方案
  • 广西网站建设流程百度竞价排名价格查询
  • 哪家网站制作 优帮云it培训机构哪家好
  • 曲阳网站建设推广seo研究协会网是干什么的
  • 个人备案网站可以做论坛吗sem扫描电子显微镜
  • deal 网站要怎么做百度seo排名优化技巧分享
  • 娄底网站建设公司站长工具国产