publicThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler){ super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler); // 调用父类的方法,先启动所有的核心线程 prestartAllCoreThreads(); } publicvoidexecute(Runnable command){ if (command == null) thrownew NullPointerException();
int c = ctl.get(); // 1. 如果工作线程数小于核心线程数(corePoolSize),则创建一个工作线程执行任务 if (workerCountOf(c) < corePoolSize) { if (addWorker(command, true)) return; c = ctl.get(); }
// 2. 如果当前是running状态,并且任务队列能够添加任务 if (isRunning(c) && workQueue.offer(command)) { int recheck = ctl.get();