概述
线程池着手解决2个不同的问题:在执行大量的异步任务时,他们通过减少每个任务创建线程的开销,提供了更好的性能;他们也提供了一种边界和管理资源的方法,包括任务执行时的线程管理。每一个ThreadPoolExecutor也保存了一些基本的统计信息,比如已经完成的任务数。
ExecutorService使用线程池中的线程执行每一个提交的任务,通常由Executors的工厂方法来配置ExecutorService。
为了能够应用于各种各样的上下文环境中,该类提供了很多可调整的参数和可拓展的钩子。但是,推荐程序员使用更简便的Executors工厂方法:
- Executors.newCachedThreadPool():无界线程池,具备自动回收线程机制;
- Executors.newFixedThreadPool(int):固定大小的线程池;
- Executors.newSingleThreadExecutor():单个后台线程;
这些工厂方法配置了最常见的使用场景。
如果要手动配置和调整此类,请见以下指南:
core and maximum pool size
ThreadPoolExecutor会自动调整线程池的大小,这通过corePoolSize和maximumPoolSize设置的边界实现。当一个新的task通过execute()方法提交到线程池时,如果线程池中正在运行的线程小于corePoolSize,会创建一个新的线程来执行task,即使其它工作线程是空闲的;如果线程中正在运行的线程大于corePoolSize,而小于maximumPoolSize,只有在当queue队列满了时才会创建一个新的线程。通过设置maximumPoolSize和corePoolSize相同,就可以创建一个固定大小的线程池。通过设置maximumPoolSize为一个将近无限大的值,例如Integer.MAX_VALUE,可以设置线程池容纳任意数量的task。一般而言,core 和 maximum pool size一般在构造函数中设置,你也可以通过setCorePoolSize()和setMaxmumPoolSize()方法动态修改。
待续
记录线程池当前线程数和当前状态的32位指针
该32位指针是一个AtomicInteger类型的变量,它记录了以下信息:
[ 3 bit runState ] [ 29 bit workerCount ]
workerCount:指示当前线程池中有效的线程数。
runState:指示线程池当前状态。线程池的状态有5种:running、shutdown、stop、tidying、terminated。
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
//COUNT_BITS = 29
private static final int COUNT_BITS = Integer.SIZE - 3;
// CAPACITY = (1 << 29) - 1 = (2^29)-1,它是workerCount的最大值
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
// runState is stored in the high-order bits
//running状态的二进制形式: 111|00000,00000000,00000000,00000000
private static final int RUNNING = -1 << COUNT_BITS;
//shutdown状态的二进制形式: 000|00000,00000000,00000000,00000000
private static final int SHUTDOWN = 0 << COUNT_BITS;
//stop状态的二进制形式: 001|00000,00000000,00000000,00000000
private static final int STOP = 1 << COUNT_BITS;
//stop状态的二进制形式: 010|00000,00000000,00000000,00000000
private static final int TIDYING = 2 << COUNT_BITS;
//terminated状态的二进制形式:011|00000,00000000,00000000,00000000
private static final int TERMINATED = 3 << COUNT_BITS;
线程池的状态
- running:接受新的task,并且会处理队列中的task。
- shutdown:不会接受新的task,但是仍会处理队列中的task。
- stop:不会接受新的task,不会处理队列中的task,并且会中断正在进行中的task。
- tidying:所有的task都已经停止,workerCount等于0,转换到tidying状态的线程会调用terminated()钩子方法。
- terminated:terminated()方法执行结束。
线程池的状态转换
- RUNNING -> SHUTDOWN
- 触发状态转换的动作:调用shutdown()方法
- (RUNNING or SHUTDOWN) -> STOP
- 触发状态转换的动作:调用shutdownNow()方法
- SHUTDOWN -> TIDYING
- 触发状态转换的动作:当队列和线程池都为空。
- STOP -> TIDYING
- 触发状态转换的动作:当线程池为空。
- TIDYING -> TERMINATED
- 触发状态转换的动作:当terminated()方法执行结束
源码分析
构造函数
待续。
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
execute()方法
1. 检查当前线程池中的线程数是否<核心线程数,如果小于核心线程数,就调用addWorker方法创建一个新的线程执行任务,addworker中的第二个参数传入true,表示当前创建的是核心线程。如果当前线程数>=核心线程数或者创建线程失败的话,直接进入第二种情况。
2. 通过调用isRunning方法判断线程池是否还在运行,如果线程池状态不是running,那就直接退出execute方法,没有执行的必要了;如果线程池的状态是running,尝试着把任务加入到queue中,再次检查线程池的状态, 如果当前不是running,可能在入队后调用了shutdown方法,所以要在queue中移除该任务,默认采用拒绝策略直接抛出异常。如果当前线程数为0,可能把allowCoreThreadTimeOut设为了true,正好核心线程全部被回收,所以必须要创建一个空的线程,让它自己去queue中去取任务执行。
3. 如果当前线程数>核心线程数,并且入队失败,调用addWorker方法创建一个新的线程去执行任务,第二个参数是false,表示当前创建的线程不是核心线程。这种情况表示核心线程已满并且queue已满,如果当前线程数小于最大线程数,创建线程执行任务。如果当前线程数>=最大线程数,默认直接采取拒绝策略。
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
/*
* Proceed in 3 steps:
*
* 1. If fewer than corePoolSize threads are running, try to
* start a new thread with the given command as its first
* task. The call to addWorker atomically checks runState and
* workerCount, and so prevents false alarms that would add
* threads when it shouldn't, by returning false.
*
* 2. If a task can be successfully queued, then we still need
* to double-check whether we should have added a thread
* (because existing ones died since last checking) or that
* the pool shut down since entry into this method. So we
* recheck state and if necessary roll back the enqueuing if
* stopped, or start a new thread if there are none.
*
* 3. If we cannot queue task, then we try to add a new
* thread. If it fails, we know we are shut down or saturated
* and so reject the task.
*/
//获取记录线程池当前线程数量和线程状态的32位指针
int c = ctl.get();
//如果线程池的当前线程数量小于corePoolSize
if (workerCountOf(c) < corePoolSize) {
//将task加入线程池,第二个参数是true,表示当前边界是corePoolSize
if (addWorker(command, true))
return;
c = ctl.get();
}
//如果线程池的当前线程数量大于等于corePoolSize,将task加入阻塞队列
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);
}
//如果线程池的当前线程数量大于等于corePoolSize,并且task加入阻塞队列失败,将task加入线程池,第二个参数是false,表示当前边界是maxmumPoolSize
else if (!addWorker(command, false))
//如果task加入线程池再次失败,执行拒绝策略
reject(command);
}
addWorker()方法
基于线程池的当前状态和给定的边界(corePoolSize和maximumPoolSize)检查一个新的worker能否被添加到线程池。如果可以添加,32位指针的workerCount将会作出相应的调整,并且新的worker会被创建和启动,运行firstTask作为它的第一个task。该方法会返回false,如果线程池已经处于stop状态或者shutdown状态。它也会返回false,如果thread factory无法创建一个新的线程。如果线程创建失败,或者thread factory返回null,或者在执行Thread.start()方法时抛出异常(比如OutOfMemoryError),我们会执行回滚。
该方法的参数如下:
- firstTask:新线程首先运行的task。worker中创建的新线程在启动后,会首先运行该task,再从阻塞队列中弹出task来运行。
- core:如果为true,使用corePoolSize作为边界,否则使用maximumPoolSize作为边界。
该方法的流程如下:
1、基于线程池的当前状态和给定的边界(corePoolSize和maximumPoolSize)检查一个新的worker能否被添加到线程池:
- 如果线程池的当前状态不对,返回false,该task不能添加到线程池。
- 如果线程池的当前线程数量超出了边界(corePoolSize或maximumPoolSize),返回false,该task不能添加到线程池。
2、如果可以添加,32位指针的workerCount加一。创建一个新的线程,并调用Thread.start()方法启动它。
private boolean addWorker(Runnable firstTask, boolean core) {
//标识retry
retry:
for (;;) {
//获取记录线程池当前线程数量和线程状态的32位指针
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);
//判断当前线程数量是否大于等于capacity
if (wc >= CAPACITY ||
//如果以corePoolSize为边界,判断当前线程数量是否大于等于corePoolSize
//如果以maximumPoolSize为边界,判断当前线程数量是否大于等于maximumPoolSize
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
//通过cas增加32位指针的workerCount字段,增加成功则退出retry标识
if (compareAndIncrementWorkerCount(c))
break retry;
//再次读取32位指针
c = ctl.get(); // Re-read ctl
//如果此次读取的线程池当前状态与上次读取的不相同,跳转到retry标识处重新运行
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 {
//Runnable封装成Worker
w = new Worker(firstTask);
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());
//如果当前状态小于shutdown
if (rs < SHUTDOWN ||
(rs == SHUTDOWN && firstTask == null)) {
if (t.isAlive()) // precheck that t is startable
throw new IllegalThreadStateException();
//将Worker添加到worker集合
workers.add(w);
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
//flag标识worker添加成功
workerAdded = true;
}
} finally {
mainLock.unlock();
}
//如果worker添加成功,启动线程,标识worker启动成功
if (workerAdded) {
t.start();
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
//返回worker是否启动成功
return workerStarted;
}
Worker
/**
* Class Worker mainly maintains interrupt control state for
* threads running tasks, along with other minor bookkeeping.
* This class opportunistically extends AbstractQueuedSynchronizer
* to simplify acquiring and releasing a lock surrounding each
* task execution. This protects against interrupts that are
* intended to wake up a worker thread waiting for a task from
* instead interrupting a task being run. We implement a simple
* non-reentrant mutual exclusion lock rather than use
* ReentrantLock because we do not want worker tasks to be able to
* reacquire the lock when they invoke pool control methods like
* setCorePoolSize. Additionally, to suppress interrupts until
* the thread actually starts running tasks, we initialize lock
* state to a negative value, and clear it upon start (in
* runWorker).
*/
private final class Worker
extends AbstractQueuedSynchronizer
implements Runnable
{
/**
* This class will never be serialized, but we provide a
* serialVersionUID to suppress a javac warning.
*/
private static final long serialVersionUID = 6138294804551838833L;
/** Thread this worker is running in. Null if factory fails. */
final Thread thread;
/** Initial task to run. Possibly null. */
Runnable firstTask;
/** Per-thread task counter */
volatile long completedTasks;
/**
* Creates with given first task and thread from ThreadFactory.
* @param firstTask the first task (null if none)
*/
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
this.thread = getThreadFactory().newThread(this);
}
/** Delegates main run loop to outer runWorker */
public void run() {
runWorker(this);
}
// Lock methods
//
// The value 0 represents the unlocked state.
// The value 1 represents the locked state.
protected boolean isHeldExclusively() {
return getState() != 0;
}
protected boolean tryAcquire(int unused) {
if (compareAndSetState(0, 1)) {
setExclusiveOwnerThread(Thread.currentThread());
return true;
}
return false;
}
protected boolean tryRelease(int unused) {
setExclusiveOwnerThread(null);
setState(0);
return true;
}
public void lock() { acquire(1); }
public boolean tryLock() { return tryAcquire(1); }
public void unlock() { release(1); }
public boolean isLocked() { return isHeldExclusively(); }
void interruptIfStarted() {
Thread t;
if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
try {
t.interrupt();
} catch (SecurityException ignore) {
}
}
}
}
runWorker()方法
循环运行task,task的来源为Worker的firstTask,或者阻塞队列中的task:
- 如果Worker的firstTask不为null,首先运行该task,并重置Worker的firstTask为null;
- 如果从阻塞队列中弹出的task不为null,运行该task;
在运行task之前,会执行beforeExecute方法, 在运行task之后,执行afterExecute方法,这两个都是钩子方法,继承了ThreadPoolExecutor可以重写此方法,嵌入自定义的逻辑。beforeExecute方法和afterExecute方法都有可能抛出异常,而导致线程死亡。
/**
* Main worker run loop. Repeatedly gets tasks from queue and
* executes them, while coping with a number of issues:
*
* 1. We may start out with an initial task, in which case we
* don't need to get the first one. Otherwise, as long as pool is
* running, we get tasks from getTask. If it returns null then the
* worker exits due to changed pool state or configuration
* parameters. Other exits result from exception throws in
* external code, in which case completedAbruptly holds, which
* usually leads processWorkerExit to replace this thread.
*
* 2. Before running any task, the lock is acquired to prevent
* other pool interrupts while the task is executing, and then we
* ensure that unless pool is stopping, this thread does not have
* its interrupt set.
*
* 3. Each task run is preceded by a call to beforeExecute, which
* might throw an exception, in which case we cause thread to die
* (breaking loop with completedAbruptly true) without processing
* the task.
*
* 4. Assuming beforeExecute completes normally, we run the task,
* gathering any of its thrown exceptions to send to afterExecute.
* We separately handle RuntimeException, Error (both of which the
* specs guarantee that we trap) and arbitrary Throwables.
* Because we cannot rethrow Throwables within Runnable.run, we
* wrap them within Errors on the way out (to the thread's
* UncaughtExceptionHandler). Any thrown exception also
* conservatively causes thread to die.
*
* 5. After task.run completes, we call afterExecute, which may
* also throw an exception, which will also cause thread to
* die. According to JLS Sec 14.20, this exception is the one that
* will be in effect even if task.run throws.
*
* The net effect of the exception mechanics is that afterExecute
* and the thread's UncaughtExceptionHandler have as accurate
* information as we can provide about any problems encountered by
* user code.
*
* @param w the worker
*/
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
//如果Worker的firstTask不为null,首先运行该task
//如果Worker的firstTask为null,从阻塞队列中弹出task
while (task != null || (task = getTask()) != null) {
w.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();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
getTask()方法
从阻塞队列中阻塞等待或者超时等待一个task。只有当以下情形时返回null:
- worker的数量超出了maximumPoolSize。
- 线程池已经处于stop状态。
- 线程池已经处于shutdown状态,并且阻塞队列为空。
- worker在等待一个task时超时了,或者超时的worker应该停止了,即满足allowCoreThreadTimeOut || wc > corePoolSize
private Runnable getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
//获取32位指针
int c = ctl.get();
//获取线程池的当前状态
int rs = runStateOf(c);
// Check if queue empty only if necessary.
//如果当前状态大于等于shutdown
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
//获取线程池的当前线程数量
int wc = workerCountOf(c);
// Are workers subject to culling?
//如果当前线程数量大于corePoolSize
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
//如果当前线程数量大于corePoolSize,从队列中阻塞等待取出一个task
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}