- 线程池executor在空闲状态下的线程个数是?
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 15, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(5), new ThreadPoolExecutor.CallerRunsPolicy());
}
}
答案:5
解释
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) |
创建具有给定的初始参数和默认线厂和拒绝执行处理程序的一个新的 ThreadPoolExecutor
。可以使用其中的 Executors
工厂方法代替这个通用的构造函数更方便。
参数
corePoolSize
-线程的数量保持在游泳池里,即使他们是懒惰的,除非 allowCoreThreadTimeOut
设置
maximumPoolSize
-的最大线程数允许在池
keepAliveTime
当线程数大于核心,这是多余的空闲线程会等待新的任务终止前的最大时间。
unit
-时间单位为 keepAliveTime
论点
workQueue
-队列用于保持任务执行之前。这个队列将只有 Runnable
execute
方法提交的任务