线程池之ThreadPoolExecutor

线程池状态转换

java.util.concurrent.ThreadPoolExecutor为JDK的线程池对象,线程池的状态和状态转换如下图:

image

使用方式

1
2
3
4
5
6
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 10, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(10));
executor.execute(new Runnable() {
@Override
public void run() {
}
});

java.util.concurrent.Executors工具类下常用的快捷创建线程池的方法newCachedThreadPool()newFixedThreadPool()newSingleThreadExecutor实际上都是对ThreadPoolExecutor的封装。

1
2
3
4
5
6
public static ExecutorService newCachedThreadPool() {
// 实际上newCachedThreadPool创建的线程池的最大线程数量并不是Integer.MAX_VALUE
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
1
2
3
4
5
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
1
2
3
4
5
6
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}

状态源码常量

1
2
3
4
5
6
7
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0)); // 默认值为-536870912,保存了当前线程池状态信息和线程数量信息

private static final int RUNNING = -1 << COUNT_BITS; // -536870912
private static final int SHUTDOWN = 0 << COUNT_BITS; // 0
private static final int STOP = 1 << COUNT_BITS; // 536870912
private static final int TIDYING = 2 << COUNT_BITS; // 1073741824
private static final int TERMINATED = 3 << COUNT_BITS; // 1610612736

源码解析

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
int c = ctl.get();
// 判断当前线程池线程数量是否小于corePoolSize
if (workerCountOf(c) < corePoolSize) {
/**
* 创新新线程执行任务
* 第二个参数:ture表示通过corePoolSize控制线程数量;false表示通过maximumPoolSize控制线程数量
*/
if (addWorker(command, true))
return;
c = ctl.get();
}
// 线程池RUNNING状态且任务向任务队列添加成功
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
// 再次判断线程池状态,如果线程池非RUNNING状态则移除新增的任务,最后抛出RejectedExecutionException异常
if (! isRunning(recheck) && remove(command))
reject(command);
// 执行到这一步说明corePoolSize为0,因此向线程池创建添加一个非核心新线程
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
// 执行到这一步说明线程池非RUNNING状态或者核心线程数量和任务队列已满,尝试直接创建新非核心线程执行该任务.
// 只有当线程池RUNNING状态且线程数量小于maximumPoolSize时执行任务成功;否则抛出RejectedExecutionException异常
// 由此可见只有当核心线程全部创建且任务队列已满时,才会创建非核心线程
else if (!addWorker(command, false))
reject(command);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);

/**
* 以下的复杂判断可以拆解成下面3种情况
* rs > SHUTDOWN 或
* rs = SHUTDOWN && firstTask != null 或
* rs = SHUTDOWN && workQueue.isEmpty()
* 即当以下情况该判断不返回false
* 1: 线程池状态为RUNNING
* 2: 线程池状态为SHUTDOWN且firstTask为null且任务队列存在未执行任务(该场景为创新一个空任务的线程执行任务队列中等待执行的任务)
*/
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;

for (;;) {
int wc = workerCountOf(c);
// 线程池最大线程数量实际为CAPACITY=536870911,即Executors.newCachedThreadPool()创建的线程池的线程最大数量并不是Integer.MAX_VALUE
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
if (compareAndIncrementWorkerCount(c))
break retry;
c = ctl.get();
if (runStateOf(c) != rs)
continue retry;
}
}

boolean workerStarted = false;
boolean workerAdded = false;
Worker w = null;
try {
// 每个Worker对象init时创建并持有一个新线程
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
int rs = runStateOf(ctl.get());

/**
* 1:线程池RUNNING状态时添加Worker对象
* 2:线程池SHUTDOWN状态时只允许添加空任务的Worker对象去执行任务队列中等待执行的任务
*/
if (rs < SHUTDOWN ||
(rs == SHUTDOWN && firstTask == null)) {
if (t.isAlive())
throw new IllegalThreadStateException();
workers.add(w);
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock();
}
if (workerAdded) {
// 启动Worker对象中的线程开始执行任务
t.start();
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock();
boolean completedAbruptly = true;
try {
/**
* 1:Worker对象持有任务,执行任务;
* 2:Worker对象未持有任务(任务执行完成或本身创建时就是个空任务线程),从任务队列中获取(任务队列为空则阻塞)任务去执行
*/
while (task != null || (task = getTask()) != null) {
w.lock();
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);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
private Runnable getTask() {
boolean timedOut = false;

for (;;) {
int c = ctl.get();
int rs = runStateOf(c);

/**
* 1:线程池状态为STOP、TIDYING、TERMINATED返回null
* 2:线程池状态为SHUTDOWN且任务队列为空返回null
* 当getTask()返回null时,runWorker()方法退出while循环并销毁该Worker线程
*/
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}

int wc = workerCountOf(c);

// 判断该Worker线程是否需要超时回收
// 当allowCoreThreadTimeOut为true或者该Worker线程为非核心线程时,需要超时回收
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;

if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}

try {
/**
* 1:不需要超时回收的Worker线程通过take()无限阻塞直到从任务队列获取到新任务
* 2:需要超时回收的Worker线程如果在keepAliveTime时间范围内未获取到新任务,则返回null然后线程回收
*/
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}

allowCoreThreadTimeOuttrue或者该Worker线程为非核心线程时,则Worker线程需要超时回收。

不需要超时回收的Worker线程从工作流程:

image

需要超时回收的Worker线程从工作流程:

image

shutdown和shutdownNow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// shutdown()只中断未执行任务的Worker线程
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(SHUTDOWN);
// 中断未执行任务的Worker线程
interruptIdleWorkers();
onShutdown(); // hook for ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
tryTerminate();
}

// shutdownNow()会中断所有Worker线程
public List<Runnable> shutdownNow() {
List<Runnable> tasks;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(STOP);
// 中断所有Worker线程
interruptWorkers();
tasks = drainQueue();
} finally {
mainLock.unlock();
}
tryTerminate();
return tasks;
}

由于shutdown()不会中断正在执行任务的Worker线程,所有存在当任务执行一直完成不了时,线程池始终处于SHUTDOWN状态而无法关闭。

execute和submit

1
2
3
4
5
6
7
public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException();
// submit方法把任务包装后再调用execute方法
RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask);
return ftask;
}

submit方法会通过newTaskFor方法创建FutureTask对象来包装任务,而FutureTaskrun方法会对任务的执行进行异常处理,保证任务执行中出现的异常不会向上抛出。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public void run() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)
set(result);
}
} finally {
runner = null;
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}

由此可见,execute执行任务时,任务出现异常直接会向上抛,最终在runWorker方法中异常退出导致该Worker线程直接被回收;而submit则处理了任务中出现的异常,即使任务执行出现异常也不会导致Worker线程的回收。

>