java 线程池参数

创建线程池的构造函数如下:

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }
 
corePoolSize        核心线程数,线程池中最小线程数,即使线程空闲,也不会被销毁;任务首先提交到核心线程中执行。(初始化时不创建线程,提交任务时创建线程)(如果设置allowCoreThreadTimeOut为true,核心线程也会被销毁,使用较少)
maximumPoolSize 线程池中允许创建的最大线程数。
keepAliveTime 当线程数大于corePoolSize时,线程最多等待keepAliveTime时间后,将被销毁。
unit keepAliveTime参数的时间单位,一般为毫秒。
workQueue 提交任务的缓存队列。
threadFactory 创建线程的工厂。
handler 当corePoolSize已满,缓存队列已满,maximumPoolSize已满时,又发生任务提交时的处理器。
 
提交任务时:
如果线程数小于核心线程数(corePoolSize)(或者核心线程有空闲),则新创建线程(或者使用空闲线程)执行任务;
如果线程数等于核心线程数,则将任务缓存到队列(workQueue);
如果缓存队列已满,则继续创建线程执行任务,直到达到最大线程数;
如果线程数达到最大线程数(maximumPoolSize),再提交任务,将使用handler来处理无法处理的任务。

原文地址:https://www.cnblogs.com/silenceshining/p/15631015.html