只要是Java面试,基本上线程池是没跑了。但是有个点总是卡在那里,不是很清楚,就是核心线程池数满了,没到最大线程数的时候。到底什么时候开始建线程呢,本文就从代码结果层面来测试一下。
首先,我们来创建了一个线程池,设置核心数5个,最大线程数10个,任务队列10个,alive时间是2s,每个任务就是随机的sleep一会,主要用来占用时间。
创建完线程后,每隔一秒钟,获取一下线程池的状态。
根据输出发现,当任务数大于5个后,active数依然没变,只要任务数为16个的时候,active才变为6,也就是说当5个核心数满了,任务队列也满了后,才开始创建新的线程。
当线程执行完任务的时候,等待设置的alive时间后,开始销毁,知道active=0。
也就是核心线程也是会被销毁的。
mac,jdk8,ThreadPoolExecutor
package com.koolearn;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @author POPE
* @version 1.0
* @date 2020/7/10 5:22 下午
*/
public class ThreadPoolTest {
/**
* 测试JAVA线程池 核心池满了后什么情况下创建新的线程
* 结论:当任务来的时候,判断是否有线程,没有就创建,当大于核心线程池后,放到任务队列,队列满了后创建新的线程执行,直到线程数等于最大线程数
* 当任务执行完后,线程会等待设置的时长后销毁,包括核心线程,都会销毁,直到active的线程数是0
* @param args
*/
public static void main(String[] args) {
BlockingQueue workQueue = new ArrayBlockingQueue<>(10);
ThreadPoolExecutor es = new ThreadPoolExecutor(5,10,2, TimeUnit.SECONDS,workQueue);
for (int i = 0; i < 20; i++) {
int a = i;
es.submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(a*500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
String name = Thread.currentThread().getName()+"-"+i;
System.out.println(name+":es.getActiveCount():"+es.getActiveCount());
System.out.println(name+":es.getCorePoolSize():"+es.getCorePoolSize());
System.out.println(name+":es.getMaximumPoolSize():"+es.getMaximumPoolSize());
System.out.println(name+":es.getTaskCount():"+es.getTaskCount());
System.out.println("--------------------------------");
}
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String name = Thread.currentThread().getName()+"-"+"last"+i;
System.out.println(name+":es.getActiveCount():"+es.getActiveCount());
System.out.println(name+":es.getCorePoolSize():"+es.getCorePoolSize());
System.out.println(name+":es.getMaximumPoolSize():"+es.getMaximumPoolSize());
System.out.println(name+":es.getTaskCount():"+es.getTaskCount());
System.out.println("--------------------------------");
}
es.shutdown();
}
}
main-0:es.getActiveCount():1
main-0:es.getCorePoolSize():5
main-0:es.getMaximumPoolSize():10
main-0:es.getTaskCount():1
--------------------------------
main-1:es.getActiveCount():2
main-1:es.getCorePoolSize():5
main-1:es.getMaximumPoolSize():10
main-1:es.getTaskCount():2
--------------------------------
main-2:es.getActiveCount():3
main-2:es.getCorePoolSize():5
main-2:es.getMaximumPoolSize():10
main-2:es.getTaskCount():3
--------------------------------
main-3:es.getActiveCount():4
main-3:es.getCorePoolSize():5
main-3:es.getMaximumPoolSize():10
main-3:es.getTaskCount():4
--------------------------------
main-4:es.getActiveCount():5
main-4:es.getCorePoolSize():5
main-4:es.getMaximumPoolSize():10
main-4:es.getTaskCount():5
--------------------------------
main-5:es.getActiveCount():5
main-5:es.getCorePoolSize():5
main-5:es.getMaximumPoolSize():10
main-5:es.getTaskCount():6
--------------------------------
main-6:es.getActiveCount():5
main-6:es.getCorePoolSize():5
main-6:es.getMaximumPoolSize():10
main-6:es.getTaskCount():7
--------------------------------
main-7:es.getActiveCount():5
main-7:es.getCorePoolSize():5
main-7:es.getMaximumPoolSize():10
main-7:es.getTaskCount():8
--------------------------------
main-8:es.getActiveCount():5
main-8:es.getCorePoolSize():5
main-8:es.getMaximumPoolSize():10
main-8:es.getTaskCount():9
--------------------------------
main-9:es.getActiveCount():5
main-9:es.getCorePoolSize():5
main-9:es.getMaximumPoolSize():10
main-9:es.getTaskCount():10
--------------------------------
main-10:es.getActiveCount():5
main-10:es.getCorePoolSize():5
main-10:es.getMaximumPoolSize():10
main-10:es.getTaskCount():11
--------------------------------
main-11:es.getActiveCount():5
main-11:es.getCorePoolSize():5
main-11:es.getMaximumPoolSize():10
main-11:es.getTaskCount():12
--------------------------------
main-12:es.getActiveCount():4
main-12:es.getCorePoolSize():5
main-12:es.getMaximumPoolSize():10
main-12:es.getTaskCount():13
--------------------------------
main-13:es.getActiveCount():5
main-13:es.getCorePoolSize():5
main-13:es.getMaximumPoolSize():10
main-13:es.getTaskCount():14
--------------------------------
main-14:es.getActiveCount():5
main-14:es.getCorePoolSize():5
main-14:es.getMaximumPoolSize():10
main-14:es.getTaskCount():15
--------------------------------
main-15:es.getActiveCount():5
main-15:es.getCorePoolSize():5
main-15:es.getMaximumPoolSize():10
main-15:es.getTaskCount():16
--------------------------------
main-16:es.getActiveCount():6
main-16:es.getCorePoolSize():5
main-16:es.getMaximumPoolSize():10
main-16:es.getTaskCount():17
--------------------------------
main-17:es.getActiveCount():7
main-17:es.getCorePoolSize():5
main-17:es.getMaximumPoolSize():10
main-17:es.getTaskCount():18
--------------------------------
main-18:es.getActiveCount():8
main-18:es.getCorePoolSize():5
main-18:es.getMaximumPoolSize():10
main-18:es.getTaskCount():19
--------------------------------
main-19:es.getActiveCount():9
main-19:es.getCorePoolSize():5
main-19:es.getMaximumPoolSize():10
main-19:es.getTaskCount():20
--------------------------------
main-last0:es.getActiveCount():9
main-last0:es.getCorePoolSize():5
main-last0:es.getMaximumPoolSize():10
main-last0:es.getTaskCount():20
--------------------------------
main-last1:es.getActiveCount():9
main-last1:es.getCorePoolSize():5
main-last1:es.getMaximumPoolSize():10
main-last1:es.getTaskCount():20
--------------------------------
main-last2:es.getActiveCount():9
main-last2:es.getCorePoolSize():5
main-last2:es.getMaximumPoolSize():10
main-last2:es.getTaskCount():20
--------------------------------
main-last3:es.getActiveCount():9
main-last3:es.getCorePoolSize():5
main-last3:es.getMaximumPoolSize():10
main-last3:es.getTaskCount():20
--------------------------------
main-last4:es.getActiveCount():9
main-last4:es.getCorePoolSize():5
main-last4:es.getMaximumPoolSize():10
main-last4:es.getTaskCount():20
--------------------------------
main-last5:es.getActiveCount():9
main-last5:es.getCorePoolSize():5
main-last5:es.getMaximumPoolSize():10
main-last5:es.getTaskCount():20
--------------------------------
main-last6:es.getActiveCount():9
main-last6:es.getCorePoolSize():5
main-last6:es.getMaximumPoolSize():10
main-last6:es.getTaskCount():20
--------------------------------
main-last7:es.getActiveCount():8
main-last7:es.getCorePoolSize():5
main-last7:es.getMaximumPoolSize():10
main-last7:es.getTaskCount():20
--------------------------------
main-last8:es.getActiveCount():5
main-last8:es.getCorePoolSize():5
main-last8:es.getMaximumPoolSize():10
main-last8:es.getTaskCount():20
--------------------------------
main-last9:es.getActiveCount():4
main-last9:es.getCorePoolSize():5
main-last9:es.getMaximumPoolSize():10
main-last9:es.getTaskCount():20
--------------------------------
main-last10:es.getActiveCount():3
main-last10:es.getCorePoolSize():5
main-last10:es.getMaximumPoolSize():10
main-last10:es.getTaskCount():20
--------------------------------
main-last11:es.getActiveCount():2
main-last11:es.getCorePoolSize():5
main-last11:es.getMaximumPoolSize():10
main-last11:es.getTaskCount():20
--------------------------------
main-last12:es.getActiveCount():2
main-last12:es.getCorePoolSize():5
main-last12:es.getMaximumPoolSize():10
main-last12:es.getTaskCount():20
--------------------------------
main-last13:es.getActiveCount():1
main-last13:es.getCorePoolSize():5
main-last13:es.getMaximumPoolSize():10
main-last13:es.getTaskCount():20
--------------------------------
main-last14:es.getActiveCount():0
main-last14:es.getCorePoolSize():5
main-last14:es.getMaximumPoolSize():10
main-last14:es.getTaskCount():20
--------------------------------
main-last15:es.getActiveCount():0
main-last15:es.getCorePoolSize():5
main-last15:es.getMaximumPoolSize():10
main-last15:es.getTaskCount():20
--------------------------------
main-last16:es.getActiveCount():0
main-last16:es.getCorePoolSize():5
main-last16:es.getMaximumPoolSize():10
main-last16:es.getTaskCount():20
--------------------------------
main-last17:es.getActiveCount():0
main-last17:es.getCorePoolSize():5
main-last17:es.getMaximumPoolSize():10
main-last17:es.getTaskCount():20
--------------------------------
main-last18:es.getActiveCount():0
main-last18:es.getCorePoolSize():5
main-last18:es.getMaximumPoolSize():10
main-last18:es.getTaskCount():20
--------------------------------
main-last19:es.getActiveCount():0
main-last19:es.getCorePoolSize():5
main-last19:es.getMaximumPoolSize():10
main-last19:es.getTaskCount():20
--------------------------------