前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >四种线程池拒绝策略

四种线程池拒绝策略

作者头像
用户6182664
发布2020-05-09 14:41:39
1K0
发布2020-05-09 14:41:39
举报

一、前言

线程池,相信很多人都有用过,没用过相信的也有学习过。但是,线程池的拒绝策略,相信知道的人会少许多。

二、四种线程池拒绝策略

当线程池的任务缓存队列已满并且线程池中的线程数目达到maximumPoolSize时,如果还有任务到来就会采取任务拒绝策略,通常有以下四种策略: ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。ThreadPoolExecutor.DiscardPolicy:丢弃任务,但是不抛出异常。ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新提交被拒绝的任务 ThreadPoolExecutor.CallerRunsPolicy:由调用线程(提交任务的线程)处理该任务

三、线程池默认的拒绝策略

既然有四种拒绝策略可以选择,那么线程池的默认拒绝策略是什么呢?查看

java.util.concurrent.ThreadPoolExecutor类的源码,我们可以看到:

代码语言:javascript
复制
/**
 * The default rejected execution handler
 */
 private static final RejectedExecutionHandler defaultHandler =
        new AbortPolicy();

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory) {
 this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             threadFactory, defaultHandler);
}

线程池的默认拒绝策略为AbortPolicy,即丢弃任务并抛出RejectedExecutionException异常。我们可以通过代码来验证这一点,现有如下代码:

代码语言:javascript
复制
public class ThreadPoolTest {
  public static void main(String[] args) {
    
    // 等待线程队列
    BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(100);
    
    //线程工程类
    ThreadFactory factory = r -> new Thread(r, "test-thread-pool");
    
    // 自定义线程池,核心线程数5,最大线程数5,等待最长时间0秒
    ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 0L,
        TimeUnit.SECONDS, queue, factory);
    
    // 死循环,持续占有线程
    while (true) {
      executor.submit(() -> {
        try {
          System.out.println(queue.size());
          Thread.sleep(10000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      });
    }
  }
}

输出结果:
0
0
42
50
91

Exception in thread "main"

java.util.concurrent.RejectedExecutionException:

Task java.util.concurrent.FutureTask@1b28cdfa

rejected from java.util.concurrent.ThreadPoolExecutor@eed1f14

[Running, pool size = 5, active threads = 5, queued tasks = 100,

completed tasks = 0]

看到报错提示:线程池大小为5,活跃线程数为5,等待队列为100。

结果是符合预期的,这也证明了线程池的默认拒绝策略是ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。

回顾下自定义线程池参数以及含义,以及下图

代码语言:javascript
复制
public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              RejectedExecutionHandler handler) {
 this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), handler);
 }

1、int corePoolSize 核心线程数,执行任务的主要线程数,当线程达到此数量会进入阻塞线程队列,等待线程执行。

2、 int maximumPoolSize 最大线程数,当核心线程数使用完毕,阻塞队列也达到最大时,核心线程数会调整为最大线程数,执行任务。long keepAliveTime 空闲线程最大存活时间,超过此时间会关闭空闲线程。达到keepAliveTime也将关闭。

3、TimeUnit unit 等待时间单位BlockingQueue<Runnable> workQueue 阻塞线程队列,可以设置等待线程队列的大小,超过队列长度,核心线程会自动扩容到最大线程数。

4、RejectedExecutionHandler handler拒绝策略,超过最大核心线程,线程等待队列已满的时候,会执行拒绝策略。

四、设置线程池拒绝策略

如果我们想要根据实际业务场景需要,设置其他的线程池拒绝策略,可以通过ThreadPoolExecutor重载的构造方法进行设置:

现在的开发中,相信大家都有使用spring,其实我们也可以通过spring提供的org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor构建线程池。如下:

代码语言:javascript
复制
@Configuration
public class TaskExecutorConfig implements AsyncConfigurer {
/**
* Set the ThreadPoolExecutor's core pool size.
*/
private static final int CORE_POOL_SIZE = 5;
/**
* Set the ThreadPoolExecutor's maximum pool size.
*/
private static final int MAX_POOL_SIZE = 5;
/**
* Set the capacity for the ThreadPoolExecutor's BlockingQueue.
*/
private static final int QUEUE_CAPACITY = 1000;
/**
* 通过重写getAsyncExecutor方法,制定默认的任务执行由该方法产生
* <p>
* 配置类实现AsyncConfigurer接口并重写getAsyncExcutor方法,并返回一个ThreadPoolTaskExevutor
* 这样我们就获得了一个基于线程池的TaskExecutor
*/
  @Override
  public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(CORE_POOL_SIZE);
    taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
    taskExecutor.setQueueCapacity(QUEUE_CAPACITY);
    taskExecutor.initialize();
    taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.
    AbortPolicy());
    return taskExecutor;
  }
}

五、拒绝策略场景分析

(1)AbortPolicy AbortPolicy

ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。

代码语言:javascript
复制
A handler for rejected tasks that throws a 
{@code RejectedExecutionException}.

这是线程池默认的拒绝策略,在任务不能再提交的时候,抛出异常,及时反馈程序运行状态。如果是比较关键的业务,推荐使用此拒绝策略,这样子在系统不能承载更大的并发量的时候,能够及时的通过异常发现。

(2)DiscardPolicy

ThreadPoolExecutor.DiscardPolicy:丢弃任务,但是不抛出异常。如果线程队列已满,则后续提交的任务都会被丢弃,且是静默丢弃。

代码语言:javascript
复制
A handler for rejected tasks that silently discards 
therejected task.

使用此策略,可能会使我们无法发现系统的异常状态。建议是一些无关紧要的业务采用此策略。例如,本人的博客网站统计阅读量就是采用的这种拒绝策略。

(3)DiscardOldestPolicy

ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新提交被拒绝的任务。

代码语言:javascript
复制
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.

此拒绝策略,是一种喜新厌旧的拒绝策略。是否要采用此种拒绝策略,还得根据实际业务是否允许丢弃老任务来认真衡量。

(4)CallerRunsPolicy

ThreadPoolExecutor.CallerRunsPolicy:由调用线程处理该任务

代码语言:javascript
复制
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.

如果任务被拒绝了,则由调用线程(提交任务的线程)直接执行此任务,我们可以通过代码来验证这一点:

把之前的代码修改如下:

代码语言:javascript
复制
public static void main(String[] args) {
    BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(10);
    ThreadFactory factory = r -> new Thread(r, "test-thread-pool");
    ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 0L,
        TimeUnit.SECONDS, queue, factory,
        new ThreadPoolExecutor.CallerRunsPolicy());
    for (int i = 0; i < 1000; i++) {
      executor.submit(() -> {
        try {
          System.out.println(Thread.currentThread().getName()
              + ":执行任务");
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      });
    }
  }
  执行结果:
  test-thread-pool:执行任务
  test-thread-pool:执行任务
  test-thread-pool:执行任务
  main:执行任务
  test-thread-pool:执行任务
  test-thread-pool:执行任务
  test-thread-pool:执行任务
  main:执行任务
  可以看到主线程与线程池交替执行。通过结果可以看到,主线程main也执行了
  任务,这正说明了此拒绝策略由调用线程(提交任务的线程)直接执行
  被丢弃的任务的。

六、总结

本文介绍和演示了四种线程池拒绝策略,具体使用哪种策略,还得根据实际业务场景才能做出抉择。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-04-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java程序员那些事 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
  • 二、四种线程池拒绝策略
  • 三、线程池默认的拒绝策略
  • 四、设置线程池拒绝策略
  • 五、拒绝策略场景分析
  • 六、总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档