1 /** 2 * ThreadPoolExecutor execute 3 */ 4 public void execute(Runnable command) { 5 //判空 6 if (command == null) 7 throw new NullPointerException(); 8 //获取执行器计数 9 int c = ctl.get(); 10 //活动线程与核心线程比较,小于侧创建新的线程。 11 if (workerCountOf(c) < corePoolSize) { 12 //添加任务 13 if (addWorker(command, true)) 14 //添加成功退出 15 return; 16 c = ctl.get(); 17 } 18 19 //如果执行器未关闭,添加到队列中 20 if (isRunning(c) && workQueue.offer(command)) { 21 int recheck = ctl.get(); 22 //如果执行器关闭,删除队列 23 if (! isRunning(recheck) && remove(command)) 24 //调用拒绝策略 25 reject(command); 26 //无活动线程,创建新的线程 27 else if (workerCountOf(recheck) == 0) 28 addWorker(null, false); 29 } 30 //无法创建新的任务 31 else if (!addWorker(command, false)) 32 //调用拒绝策略 33 reject(command); 34 }
原文:https://www.cnblogs.com/seeseabky/p/12118869.html