直接断点进去:
@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 时,线程使用此超时。否则,他们会永远等待新的工作。
文章详细解释了JavaThreadPoolExecutor中的keepAliveTime参数,它定义了空闲线程在等待新任务时的超时时间。当线程数量超过corePoolSize或启用CoreThreadTimeOut时,线程会使用这个超时值。
2519

被折叠的 条评论
为什么被折叠?



