ThreadPoolExecutor
概述
添加任务的流程
步骤 1:检查线程池状态
首先,ThreadPoolExecutor 会检查当前线程池的状态,确保线程池还没有关闭。如果线程池已经关闭,会抛出 RejectedExecutionException。
步骤 2:检查核心线程数
如果线程池的状态正常,ThreadPoolExecutor 会检查当前线程池中的线程数量是否小于核心线程数(corePoolSize)。如果是,会创建一个新的线程来执行任务。
步骤 3:检查任务队列
如果当前线程池中的线程数量已经达到核心线程数,ThreadPoolExecutor 会尝试将任务放入任务队列(workQueue)中。如果任务队列已满,会进入下一步。
步骤 4:检查最大线程数
如果任务队列已满,ThreadPoolExecutor 会检查当前线程池中的线程数量是否小于最大线程数(maximumPoolSize)。如果是,会创建一个新的非核心线程来执行任务。
步骤 5:执行拒绝策略
如果当前线程池中的线程数量已经达到最大线程数,并且任务队列也已满,ThreadPoolExecutor 会调用拒绝策略(RejectedExecutionHandler)来处理这个任务。默认的拒绝策略是 AbortPolicy,它会抛出 RejectedExecutionException。
Worker
executor.execute(Runnable command)
/** * executor.execute(Runnable command) * * java.util.concurrent.ThreadPoolExecutor#execute(java.lang.Runnable) * public void execute(Runnable command) { * if (command == null) * throw new NullPointerException(); * * int c = ctl.get(); * if (workerCountOf(c) < corePoolSize) { // 如果 正在运行的线程数 < corePoolSize,会开启一个新的core线程执行; * if (addWorker(command, true)) // 如果 添加&启动新的core线程成功 -> 结束 * return; * c = ctl.get(); * } * if (isRunning(c) && workQueue.offer(command)) { // 如果 正在运行的线程数 >= corePoolSize,任务添加至阻塞队列成功 * int recheck = ctl.get(); * if (! isRunning(recheck) && remove(command)) // * reject(command); * else if (workerCountOf(recheck) == 0) * addWorker(null, false); // * } * else if (!addWorker(command, false)) // 如果 正在运行的线程数 >= corePoolSize,阻塞队列已满 -> 添加非core线程 * reject(command); // 如果失败 -> 拒绝任务 * } * * java.util.concurrent.ThreadPoolExecutor#addWorker(java.lang.Runnable, boolean) * private boolean addWorker(Runnable firstTask, boolean core) { * retry: * for (;;) { * int c = ctl.get(); * int rs = runStateOf(c); * * // Check if queue empty only if necessary. * if (rs >= SHUTDOWN && * ! (rs == SHUTDOWN && * firstTask == null && * ! workQueue.isEmpty())) * return false; * * for (;;) { * int wc = workerCountOf(c); * if (wc >= CAPACITY || * wc >= (core ? corePoolSize : maximumPoolSize)) * return false; * if (compareAndIncrementWorkerCount(c)) // 如果新增WorkerCount成功 -> break遍历,执行下面的逻辑 * break retry; * c = ctl.get(); // Re-read ctl * if (runStateOf(c) != rs) * continue retry; * // else CAS failed due to workerCount change; retry inner loop * } * } * * boolean workerStarted = false; * boolean workerAdded = false; * Worker w = null; * try { * w = new Worker(firstTask); // 构建新的Worker * final Thread t = w.thread; * if (t != null) { * final ReentrantLock mainLock = this.mainLock; * mainLock.lock(); * try { * // Recheck while holding lock. * // Back out on ThreadFactory failure or if * // shut down before lock acquired. * int rs = runStateOf(ctl.get()); * * if (rs < SHUTDOWN || * (rs == SHUTDOWN && firstTask == null)) { * if (t.isAlive()) // precheck that t is startable * throw new IllegalThreadStateException(); * workers.add(w); // 将Worker添加至workers * int s = workers.size(); * if (s > largestPoolSize) * largestPoolSize = s; * workerAdded = true; * } * } finally { * mainLock.unlock(); * } * if (workerAdded) { * t.start(); // 启动线程 * workerStarted = true; * } * } * } finally { * if (! workerStarted) * addWorkerFailed(w); * } * return workerStarted; * } * * * java.util.concurrent.ArrayBlockingQueue#offer(java.lang.Object) * * java.util.concurrent.ThreadPoolExecutor#reject(java.lang.Runnable) * final void reject(Runnable command) { * handler.rejectedExecution(command, this); * } * */
executor.submit(Runnable task)
/** * executor.submit(Runnable task) * * java.util.concurrent.AbstractExecutorService#submit(java.lang.Runnable) * public Future<?> submit(Runnable task) { * if (task == null) throw new NullPointerException(); * RunnableFuture<Void> ftask = newTaskFor(task, null); // 将Runnable封装为FutureTask * execute(ftask); // 执行任务 * return ftask; * } * * java.util.concurrent.AbstractExecutorService#newTaskFor(java.lang.Runnable, java.lang.Object) * protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { * return new FutureTask<T>(runnable, value); * } * * java.util.concurrent.FutureTask#FutureTask(java.lang.Runnable, java.lang.Object) * public FutureTask(Runnable runnable, V result) { * this.callable = Executors.callable(runnable, result); * this.state = NEW; // ensure visibility of callable * } * * java.util.concurrent.Executor#execute(java.lang.Runnable) * java.util.concurrent.ThreadPoolExecutor#execute(java.lang.Runnable) * 和executor.execute(Runnable command)逻辑一致 */
executor.submit(Callable<T> task)
/** * executor.submit(Callable<T> task) * * java.util.concurrent.AbstractExecutorService#submit(java.util.concurrent.Callable) * public <T> Future<T> submit(Callable<T> task) { * if (task == null) throw new NullPointerException(); * RunnableFuture<T> ftask = newTaskFor(task); // 构建FutureTask * execute(ftask); // 执行任务 * return ftask; * } * * java.util.concurrent.AbstractExecutorService#newTaskFor(java.util.concurrent.Callable) * protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { * return new FutureTask<T>(callable); * } * * java.util.concurrent.FutureTask#FutureTask(java.util.concurrent.Callable) * public FutureTask(Callable<V> callable) { * if (callable == null) * throw new NullPointerException(); * this.callable = callable; * this.state = NEW; // ensure visibility of callable * } * * java.util.concurrent.Executor#execute(java.lang.Runnable) * java.util.concurrent.ThreadPoolExecutor#execute(java.lang.Runnable) * 和executor.execute(Runnable command)逻辑一致 */
runWorker如何被执行?
/** * runWorker(Worker w) 如何被调用? * * 1、无论是 executor.submit(Callable<T> task)、executor.submit(Runnable task)、executor.execute(Runnable command)都会调用java.util.concurrent.ThreadPoolExecutor#execute(java.lang.Runnable) * 2、在第一步中会调用java.util.concurrent.ThreadPoolExecutor#addWorker(java.lang.Runnable, boolean), 会将被提交的任务封装为Worker * a、Worker实现了Runnable,run方法即是Worker实际执行的任务 * b、new Worker时,构造方法中创建了新的线程,Worker的thread指向新的线程; * c、在ThreadPoolExecutor#execute中,会将Worker指向的thread进行启动,此时将会执行Worker的run方法,run方法内部实现的是java.util.concurrent.ThreadPoolExecutor#runWorker(java.util.concurrent.ThreadPoolExecutor.Worker) */
public class ThreadPoolExecutor extends AbstractExecutorService { private final class Worker extends AbstractQueuedSynchronizer implements Runnable{ final Thread thread; Runnable firstTask; Worker(Runnable firstTask) { setState(-1); // inhibit interrupts until runWorker this.firstTask = firstTask; this.thread = getThreadFactory().newThread(this); } public void run() { runWorker(this); } public void unlock() { release(1); } public void lock() { acquire(1); } protected boolean tryRelease(int unused) { setExclusiveOwnerThread(null); setState(0); return true; } protected boolean tryAcquire(int unused) { if (compareAndSetState(0, 1)) { setExclusiveOwnerThread(Thread.currentThread()); return true; } return false; } public ThreadFactory getThreadFactory() { return threadFactory; } } private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0)); // The queue used for holding tasks and handing off to worker threads. private final BlockingQueue<Runnable> workQueue; // Set containing all worker threads in pool. Accessed only when holding mainLock. 包含所有线程的Set,仅持有mainlock访问 private final HashSet<Worker> workers = new HashSet<Worker>(); // Tracks largest attained pool size. Accessed only under mainLock. 达到最大的线程数量,仅持有mainlock访问 private int largestPoolSize; // Handler called when saturated or shutdown in execute. 当执行时线程池 饱和或关闭 时调用的拒绝策略 private volatile RejectedExecutionHandler handler; // Timeout in nanoseconds for idle threads waiting for work. 空闲线程生存时间 // Threads use this timeout when there are more than corePoolSize present or if allowCoreThreadTimeOut. 当线程池的线程数量>corePoolSize时被使用 // Otherwise they wait forever for new work. private volatile long keepAliveTime; // Core pool size is the minimum number of workers to keep alive (and not allow to time out etc) unless allowCoreThreadTimeOut is set, in which case the minimum is zero. // 线程池中 最小的保持活着的线程数量 private volatile int corePoolSize; // Maximum pool size. private volatile int maximumPoolSize; // The default rejected execution handler private static final RejectedExecutionHandler defaultHandler = new java.util.concurrent.ThreadPoolExecutor.AbortPolicy(); // Factory for new threads. private volatile ThreadFactory threadFactory; final void runWorker(Worker w) { } } public class Executors { static class DefaultThreadFactory implements ThreadFactory { private static final AtomicInteger poolNumber = new AtomicInteger(1); private final ThreadGroup group; private final AtomicInteger threadNumber = new AtomicInteger(1); private final String namePrefix; DefaultThreadFactory() { SecurityManager s = System.getSecurityManager(); group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-"; } public Thread newThread(Runnable r) { Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); if (t.isDaemon()) t.setDaemon(false); if (t.getPriority() != Thread.NORM_PRIORITY) t.setPriority(Thread.NORM_PRIORITY); return t; } } public static ThreadFactory defaultThreadFactory() { return new Executors.DefaultThreadFactory(); } }
ThreadFactory
构建方式
引入:com.google.guava包
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
链路
execute(Runnable command)
// java.util.concurrent.ThreadPoolExecutor.execute add core线程 -> add Queue -> add 非core线程 /** * Executes the given task sometime in the future.The task may execute in a new thread or in an existing pooled thread. * 执行给定的任务;可能在 新线程或已存在的线程 中执行; * @param command */ public void execute(Runnable command) { int c = ctl.get(); // If fewer than corePoolSize threads are running, try to start a new thread with the given command as its first task. // 如果 运行的线程数 < 核心线程数 -> 创建一个新的core线程执行该任务 if (workerCountOf(c) < corePoolSize) { if (addWorker(command, true)) return; c = ctl.get(); } // 如果 线程数 >= core && 线程池正在运行 && 任务添加队列成功 -> if (isRunning(c) && workQueue.offer(command)) { int recheck = ctl.get(); if (! isRunning(recheck) && remove(command)) reject(command); else if (workerCountOf(recheck) == 0) addWorker(null, false); } // 添加非core线程, 如果失败 -> 拒绝任务 else if (!addWorker(command, false)) reject(command); } // java.util.concurrent.ThreadPoolExecutor.addWorker /** * Checks if a new worker can be added with respect to current pool state and the given bound (either core or maximum). * 检测 当前线程池的state和边界,一个新的线程是否能被添加 */ private boolean addWorker(Runnable firstTask, boolean core) { retry: for (;;) { int c = ctl.get(); int rs = runStateOf(c); // Check if queue empty only if necessary. if (rs >= SHUTDOWN && ! (rs == SHUTDOWN && firstTask == null && ! workQueue.isEmpty())) return false; for (;;) { int wc = workerCountOf(c); if (wc >= CAPACITY || wc >= (core ? corePoolSize : maximumPoolSize)) return false; if (compareAndIncrementWorkerCount(c)) break retry; c = ctl.get(); // Re-read ctl if (runStateOf(c) != rs) continue retry; // else CAS failed due to workerCount change; retry inner loop } } boolean workerStarted = false; boolean workerAdded = false; Worker w = null; try { w = new Worker(firstTask); // 将提交的Runnable封装为一个Worker(创建一个新的线程) final Thread t = w.thread; if (t != null) { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { // Recheck while holding lock. // Back out on ThreadFactory failure or if // shut down before lock acquired. int rs = runStateOf(ctl.get()); if (rs < SHUTDOWN || (rs == SHUTDOWN && firstTask == null)) { if (t.isAlive()) // precheck that t is startable throw new IllegalThreadStateException(); workers.add(w); // 将新建的worker添加到workers int s = workers.size(); if (s > largestPoolSize) largestPoolSize = s; workerAdded = true; } } finally { mainLock.unlock(); } if (workerAdded) { t.start(); // 如果worker创建成功 -> 启动该worker线程 workerStarted = true; } } } finally { if (! workerStarted) addWorkerFailed(w); } return workerStarted; } // java.util.concurrent.ThreadPoolExecutor.Worker.run public void run() { runWorker(this); } // java.util.concurrent.ThreadPoolExecutor.runWorker /** * Main worker run loop. Repeatedly gets tasks from queue and executes them */ final void runWorker(Worker w) { Thread wt = Thread.currentThread(); Runnable task = w.firstTask; w.firstTask = null; w.unlock(); // allow interrupts // 使用AQS的独占模式释放lock boolean completedAbruptly = true; try { while (task != null || (task = getTask()) != null) { w.lock(); // 使用AQS的独占模式获取lock // If pool is stopping, ensure thread is interrupted; // if not, ensure thread is not interrupted. This // requires a recheck in second case to deal with // shutdownNow race while clearing interrupt if ((runStateAtLeast(ctl.get(), STOP) || (Thread.interrupted() && runStateAtLeast(ctl.get(), STOP))) && !wt.isInterrupted()) wt.interrupt(); try { beforeExecute(wt, task); // 任务执行前 Throwable thrown = null; try { task.run(); // 任务执行 } catch (RuntimeException x) { thrown = x; throw x; } catch (Error x) { thrown = x; throw x; } catch (Throwable x) { thrown = x; throw new Error(x); } finally { afterExecute(task, thrown); // 任务执行后 } } finally { task = null; w.completedTasks++; w.unlock(); // 使用AQS的独占模式释放lock } } completedAbruptly = false; } finally { processWorkerExit(w, completedAbruptly); } }