ThreadPoolExecutor的参数keepAliveTime的作用

直接断点进去:

@Test
public void testKeepAliveTime3() {//生存时间 - 针对救急线程

    ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 10, TimeUnit.SECONDS, new SynchronousQueue<>());
}
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;
}

这里点进去看成员变量private volatile long keepAliveTime;的注释:

* Timeout in nanoseconds for idle threads waiting for work.
* Threads use this timeout when there are more than corePoolSize present or if allowCoreThreadTimeOut. Otherwise they wait forever for new work.

对于英语好的同学应该是秒懂了

看不懂就翻译:

* 等待工作的空闲线程超时(以纳秒为单位)。
当存在超过 corePoolSize 或允许 CoreThreadTimeOut 时,线程使用此超时。否则,他们会永远等待新的工作。

你可能感兴趣的:(线程池,线程,原理)