资讯专栏INFORMATION COLUMN

线程池RejectedExecutionHandler源码

不知名网友 / 1097人阅读

备忘下线程池RejectedExecutionHandler的源码。

CallerRunsPolicy

</>复制代码

  1. /**
  2. * A handler for rejected tasks that runs the rejected task
  3. * directly in the calling thread of the {@code execute} method,
  4. * unless the executor has been shut down, in which case the task
  5. * is discarded.
  6. */
  7. public static class CallerRunsPolicy implements RejectedExecutionHandler {
  8. /**
  9. * Creates a {@code CallerRunsPolicy}.
  10. */
  11. public CallerRunsPolicy() { }
  12. /**
  13. * Executes task r in the caller"s thread, unless the executor
  14. * has been shut down, in which case the task is discarded.
  15. *
  16. * @param r the runnable task requested to be executed
  17. * @param e the executor attempting to execute this task
  18. */
  19. public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
  20. if (!e.isShutdown()) {
  21. r.run();
  22. }
  23. }
  24. }
AbortPolicy

</>复制代码

  1. /**
  2. * A handler for rejected tasks that throws a
  3. * {@code RejectedExecutionException}.
  4. */
  5. public static class AbortPolicy implements RejectedExecutionHandler {
  6. /**
  7. * Creates an {@code AbortPolicy}.
  8. */
  9. public AbortPolicy() { }
  10. /**
  11. * Always throws RejectedExecutionException.
  12. *
  13. * @param r the runnable task requested to be executed
  14. * @param e the executor attempting to execute this task
  15. * @throws RejectedExecutionException always.
  16. */
  17. public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
  18. throw new RejectedExecutionException("Task " + r.toString() +
  19. " rejected from " +
  20. e.toString());
  21. }
  22. }
DiscardPolicy

</>复制代码

  1. /**
  2. * A handler for rejected tasks that silently discards the
  3. * rejected task.
  4. */
  5. public static class DiscardPolicy implements RejectedExecutionHandler {
  6. /**
  7. * Creates a {@code DiscardPolicy}.
  8. */
  9. public DiscardPolicy() { }
  10. /**
  11. * Does nothing, which has the effect of discarding task r.
  12. *
  13. * @param r the runnable task requested to be executed
  14. * @param e the executor attempting to execute this task
  15. */
  16. public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
  17. }
  18. }
DiscardOldestPolicy

</>复制代码

  1. /**
  2. * A handler for rejected tasks that discards the oldest unhandled
  3. * request and then retries {@code execute}, unless the executor
  4. * is shut down, in which case the task is discarded.
  5. */
  6. public static class DiscardOldestPolicy implements RejectedExecutionHandler {
  7. /**
  8. * Creates a {@code DiscardOldestPolicy} for the given executor.
  9. */
  10. public DiscardOldestPolicy() { }
  11. /**
  12. * Obtains and ignores the next task that the executor
  13. * would otherwise execute, if one is immediately available,
  14. * and then retries execution of task r, unless the executor
  15. * is shut down, in which case task r is instead discarded.
  16. *
  17. * @param r the runnable task requested to be executed
  18. * @param e the executor attempting to execute this task
  19. */
  20. public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
  21. if (!e.isShutdown()) {
  22. e.getQueue().poll();
  23. e.execute(r);
  24. }
  25. }
  26. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65779.html

相关文章

  • java线程——ThreadPoolExecutor源码解析

    摘要:将线程池状态置为并不会立即停止,停止接收外部的任务,内部正在跑的任务和队列里等待的任务,会执行完,才真正停止。将线程池状态置为。 在Java中,我们经常使用的线程池就是ThreadPoolExecutor,此外还有定时的线程池ScheduledExecutorService(),但是需要注意的是Executors.newCachedThreadPool()的线程是没有上届的,在使用时,...

    TerryCai 评论0 收藏0
  • 后端ing

    摘要:当活动线程核心线程非核心线程达到这个数值后,后续任务将会根据来进行拒绝策略处理。线程池工作原则当线程池中线程数量小于则创建线程,并处理请求。当线程池中的数量等于最大线程数时默默丢弃不能执行的新加任务,不报任何异常。 spring-cache使用记录 spring-cache的使用记录,坑点记录以及采用的解决方案 深入分析 java 线程池的实现原理 在这篇文章中,作者有条不紊的将 ja...

    roadtogeek 评论0 收藏0
  • 线程,这一篇或许就够了

    摘要:创建方法最大线程数即源码单线程化的线程池有且仅有一个工作线程执行任务所有任务按照指定顺序执行,即遵循队列的入队出队规则创建方法源码还有一个结合了和,就不介绍了,基本不用。 *本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 为什么用线程池 创建/销毁线程伴随着系统开销,过于频繁的创建/销毁线程,会很大程度上影响处理效率 >例如: > >记创建线程消耗时间T1,执行...

    UsherChen 评论0 收藏0
  • 一看就懂的Java线程分析详解

    摘要:任务性质不同的任务可以用不同规模的线程池分开处理。线程池在运行过程中已完成的任务数量。如等于线程池的最大大小,则表示线程池曾经满了。线程池的线程数量。获取活动的线程数。通过扩展线程池进行监控。框架包括线程池,,,,,,等。 Java线程池 [toc] 什么是线程池 线程池就是有N个子线程共同在运行的线程组合。 举个容易理解的例子:有个线程组合(即线程池,咱可以比喻为一个公司),里面有3...

    Yangder 评论0 收藏0
  • ThreadPoolExecutor源码阅读

    摘要:介绍线程池一般包含三个主要部分调度器决定由哪个线程来执行任务执行任务所能够的最大耗时等线程队列存放并管理着一系列线程这些线程都处于阻塞状态或休眠状态任务队列存放着用户提交的需要被执行的任务一般任务的执行的即先提交的任务先被执行调度器并非是必 介绍 线程池一般包含三个主要部分: 调度器: 决定由哪个线程来执行任务, 执行任务所能够的最大耗时等 线程队列: 存放并管理着一系列线程, 这些...

    Meathill 评论0 收藏0

发表评论

0条评论

不知名网友

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<