线程池中非核心线程何时创建,先说结论:任务队列满了,才会创建非核心线程。
public class ThreadPoolVerify {
public static void main(String[] args){
int taskIndex = 0;
BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>(20);
ThreadPoolExecutor executor = new ThreadPoolExecutor(5,10,
30, TimeUnit.SECONDS, blockingQueue, new ThreadPoolExecutor.CallerRunsPolicy());
while (taskIndex<50){
System.out.println(String.format("当前任务队列任务数量:%d", executor.getQueue().size()));
executor.execute(new Task(taskIndex));
taskIndex++;
}
executor.shutdown();
}
}
class Task implements Runnable {
private int taskIndex=0;
public Task(int taskIndex) {
this.taskIndex = taskIndex;
}
[@Override](https://my.oschina.net/u/1162528)
public void run() {
try {
System.out.println(String.format("线程名称:%s, 处理第%d个任务。",
Thread.currentThread().
5281



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



