ThreadPoolExecutor参数的用法及说明

ThreadPoolExecutor参数说明

一、ThreadPoolExecutor核心参数说明

1、corePoolSize:核心线程数

* 核心线程会一直存活,及时没有任务需要执行

* 当线程数小于核心线程数时,即使有线程空闲,线程池也会优先创建新线程处理

* 设置allowCoreThreadTimeout=true(默认false)时,核心线程会超时关闭

2、queueCapacity:任务队列容量(阻塞队列)

* 当核心线程数达到最大时,新任务会放在队列中排队等待执行

3、maxPoolSize:最大线程数

* 当线程数>=corePoolSize,且任务队列已满时。线程池会创建新线程来处理任务

* 当线程数=maxPoolSize,且任务队列已满时,线程池会拒绝处理任务而抛出异常

4、 keepAliveTime:线程空闲时间

* 当线程空闲时间达到keepAliveTime时,线程会退出,直到线程数量=corePoolSize

* 如果allowCoreThreadTimeout=true,则会直到线程数量=0

5、allowCoreThreadTimeout:允许核心线程超时

6、rejectedExecutionHandler:任务拒绝处理器

* 两种情况会拒绝处理任务:

  • - 当线程数已经达到maxPoolSize,切队列已满,会拒绝新任务
  • - 当线程池被调用shutdown()后,会等待线程池里的任务执行完毕,再shutdown。如果在调用shutdown()和线程池真正shutdown之间提交任务,会拒绝新任务

* 线程池会调用rejectedExecutionHandler来处理这个任务。如果没有设置默认是AbortPolicy,会抛出异常

* ThreadPoolExecutor类有几个内部实现类来处理这类情况:

  • - AbortPolicy 丢弃任务,抛运行时异常
  • - CallerRunsPolicy 执行任务
  • - DiscardPolicy 忽视,什么都不会发生
  • - DiscardOldestPolicy 从队列中踢出最先进入队列(最后一个执行)的任务

* 实现RejectedExecutionHandler接口,可自定义处理器

二、ThreadPoolExecutor执行顺序

线程池按以下行为执行任务

1. 当线程数小于核心线程数时,创建线程。

2. 当线程数大于等于核心线程数,且任务队列未满时,将任务放入任务队列。

3. 当线程数大于等于核心线程数,且任务队列已满

  • -1 若线程数小于最大线程数,创建线程
  • -2 若线程数等于最大线程数,抛出异常,拒绝任务

三、ThreadPoolExecutor如何设置参数

1、默认值

* corePoolSize=1 * queueCapacity=Integer.MAX_VALUE * maxPoolSize=Integer.MAX_VALUE * keepAliveTime=60s * allowCoreThreadTimeout=false * rejectedExecutionHandler=AbortPolicy()

2、如何来设置

* 需要根据几个值来决定

  • - tasks :每秒的任务数,假设为1000
  • - taskcost:每个任务花费时间,假设为0.1s
  • - responsetime:系统允许容忍的最大响应时间,假设为1s

* 做几个计算

- corePoolSize = 每秒需要多少个线程处理?

* 一颗CPU核心同一时刻只能执行一个线程,然后操作系统切换上下文,核心开始执行另一个线程的代码,以此类推,超过cpu核心数,就会放入队列,如果队列也满了,就另起一个新的线程执行,所有推荐:corePoolSize = ((cpu核心数 * 2) + 有效磁盘数),java可以使用Runtime.getRuntime().availableProcessors()获取cpu核心数

- queueCapacity = (coreSizePool/taskcost)*responsetime

* 计算可得 queueCapacity = corePoolSize/0.1*1。意思是队列里的线程可以等待1s,超过了的需要新开线程来执行

* 切记不能设置为Integer.MAX_VALUE,这样队列会很大,线程数只会保持在corePoolSize大小,当任务陡增时,不能新开线程来执行,响应时间会随之陡增。

- maxPoolSize = (max(tasks)- queueCapacity)/(1/taskcost)

* 计算可得 maxPoolSize = (1000-corePoolSize)/10,即(每秒并发数-corePoolSize大小) / 10

* (最大任务数-队列容量)/每个线程每秒处理能力 = 最大线程数

  • - rejectedExecutionHandler:根据具体情况来决定,任务不重要可丢弃,任务重要则要利用一些缓冲机制来处理
  • - keepAliveTime和allowCoreThreadTimeout采用默认通常能满足

ThreadPoolExecutor参数allowCoreThreadTimeOut

ThreadPoolExecutor的执行流程有一点可能被吐槽过,就是只有缓存队列已经满了的时候才会使用到maxPoolSize创建新的线程.也就是说如果corePoolSize设为0的时候,要等到队列满了,才会创建线程去执行任务

之前有被问到,希望没有任务的时候线程池里的线程可以停掉。可能对性能和资源有过考虑的人都会想到这个问题吧

今天看JDK源码的时候发现了ThreadPoolExecutor在1.6的时候已经支持了

allowCoreThreadTimeOut参数就是为此设计的

    /**
     * Sets the policy governing whether core threads may time out and
     * terminate if no tasks arrive within the keep-alive time, being
     * replaced if needed when new tasks arrive. When false, core
     * threads are never terminated due to lack of incoming
     * tasks. When true, the same keep-alive policy applying to
     * non-core threads applies also to core threads. To avoid
     * continual thread replacement, the keep-alive time must be
     * greater than zero when setting {@code true}. This method
     * should in general be called before the pool is actively used.
     *
     * @param value {@code true} if should time out, else {@code false}
     * @throws IllegalArgumentException if value is {@code true}
     *         and the current keep-alive time is not greater than zero
     *
     * @since 1.6
     */
    public void allowCoreThreadTimeOut(boolean value) {
        if (value && keepAliveTime <= 0)
            throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
        if (value != allowCoreThreadTimeOut) {
            allowCoreThreadTimeOut = value;
            if (value)
                interruptIdleWorkers();
        }
    }

在ThreadPoolExecutor构造函数的注释上也有明确说明:corePoolSize 的数量会一直保持,即使这些线程是空闲的,除非设置了allowCoreThreadTimeOut

/**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters and default thread factory and rejected execution handler.
     * It may be more convenient to use one of the {@link Executors} factory
     * methods instead of this general purpose constructor.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @param maximumPoolSize the maximum number of threads to allow in the
     *        pool
     * @param keepAliveTime when the number of threads is greater than
     *        the core, this is the maximum time that excess idle threads
     *        will wait for new tasks before terminating.
     * @param unit the time unit for the {@code keepAliveTime} argument
     * @param workQueue the queue to use for holding tasks before they are
     *        executed.  This queue will hold only the {@code Runnable}
     *        tasks submitted by the {@code execute} method.
     * @throws IllegalArgumentException if one of the following holds:
* {@code corePoolSize < 0}
* {@code keepAliveTime < 0}
* {@code maximumPoolSize <= 0}
* {@code maximumPoolSize < corePoolSize} * @throws NullPointerException if {@code workQueue} is null */ public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); }

当然,在Executors的静态工厂里面的newCachedThreadPool提供了另外一种思路

coreSize为0
SynchronousQueue最多只能有一个任务在队列里面

也就是说这个线程池的任务会被立即分配一个线程去处理,如果没有空闲的线程会立即创建线程。

在空闲的时候,线程数量会减少直至为0,这一点倒是满足了要求。可是队列中最多只会缓存一个任务,当任务的处理速度慢于任务进入线程池的速度时,线程数量就会不断膨胀。如果maxPoolSize设置成一个比较小的数字时,可能就会有大量任务被拒绝策略处理。

所以正如注释中所说,newCachedThreadPool只适合于任务处理速度很快的场景下。比如做一些计算,不需要依赖其它服务

/**
     * Creates a thread pool that creates new threads as needed, but
     * will reuse previously constructed threads when they are
     * available.  These pools will typically improve the performance
     * of programs that execute many short-lived asynchronous tasks.
     * Calls to {@code execute} will reuse previously constructed
     * threads if available. If no existing thread is available, a new
     * thread will be created and added to the pool. Threads that have
     * not been used for sixty seconds are terminated and removed from
     * the cache. Thus, a pool that remains idle for long enough will
     * not consume any resources. Note that pools with similar
     * properties but different details (for example, timeout parameters)
     * may be created using {@link ThreadPoolExecutor} constructors.
     *
     * @return the newly created thread pool
     */
    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue());

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(ThreadPoolExecutor参数的用法及说明)