corePoolSize:核心线程池大小 cSize,不初始化默认是0个
maxinumPoolsSize:线程池最大容量 mSize
keepAliveTime:当线程数量大于核心时,多余的空闲线程在终止之前等待新任务的最大时间。
unit:时间单位
workQueue:工作队列 nWorks
ThreadFactory:线程工厂
handler:拒绝策略
运行机制
通过new 创建线程池时,除非调用prestartAllCoreThreads方法初始化核心线程,否则此时线程池中有0个线程,即使工作队列中存在多个任务,同样不会执行。,
public class CoreThreadDemo {
/**
* 通过LinkedBlockingQueue方式 让线程池执行
*
*/
public static void mainone(String[] args) throws Exception
{
LinkedBlockingQueue<Runnable> object = new LinkedBlockingQueue<>();
for (int i= 0;i<100;i++)
{
object.put(()->{
System.out.println(Thread.currentThread().getName());
});
}
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,20,2000,TimeUnit.SECONDS,object);
//启动所有核心线程
threadPoolExecutor.prestartAllCoreThreads();
}
/**
* 任务数X
* x <= cSize 只启动X个线程
*/
public static void maintwo(String[] args) throws Exception
{
Linked