java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > ThreadPoolExecutor参数说明

ThreadPoolExecutor参数的用法及说明

作者:晨曦下的微笑

这篇文章主要介绍了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:任务拒绝处理器

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

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

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

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

二、ThreadPoolExecutor执行顺序

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

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

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

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

三、ThreadPoolExecutor如何设置参数

1、默认值

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

2、如何来设置

* 需要根据几个值来决定

* 做几个计算

- 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

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

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:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@code maximumPoolSize < corePoolSize}
     * @throws NullPointerException if {@code workQueue} is null
     */
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> 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<Runnable>());

总结

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

您可能感兴趣的文章:
阅读全文