上一篇我们简单介绍了Java线程池ThreadPoolExecutor的基本应用和简单的操作流程。这一篇我们将深入理解线程池的实现方法,只有彻底掌握,才能正确运用!
一、构造方法
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.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
二、线程的提交
ThreadPoolExecutor在构造完成后,便可以提交线程任务,提交方法主要有execute和submit。先介绍execute。源码如下:
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.
*/
int c = ctl.get(); //获取线程池状态
if (workerCountOf(c) < corePoolSize) { //工作线程数如果小于核心线程数
if (addWorker(command, true)) //构造worker,添加线程并启动
return;
c = ctl.get();
}
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);
}
else if (!addWorker(command, false)) //如果阻塞队列满,可能还没到最大线程数,所以尝试增加一个worker
reject(command);
}
首先,线程池会获取自身状态,这里,线程池的状态由AtomicInteger变量ctl保存,ctl的高三位用来保存运行状态,第三位保存workerCount,也就是当前的有效线程数。因此,workerCount的上限是(2^29-1)。
-1:RUNNING; 0:SHUTDOWN; 1:STOP; 2:TIDYING; 3:TERMINATED
其次,理解Worker和Task的区别。Worker是Task的封装,构造它时,会装入task并创建一个线程。该线程在运行完task后,会从Queue中获取新的task运行。上述代码中,第一步和第三步会直接创建Worker来运行满足条件的task,第二步的task会进入阻塞队列等待线程池中的Worker调取。
核心方法:addWorker
源代码如下:
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);
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);
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;
}
首先,addWorker对线程池状态进行了检测:
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;
这里,是对线程池状态的判定。
- 如果状态为RUNNING(0),则直接跳过。
- 如果状态为STOP、DITYING或者TERMINATED(rs>=SHUTDOWN && !(rs == SHUTDOWN)),那么不接受新线程,返回false。
- 如果状态为>=SHUTDOWN,同时 firstTask != null,那么拒绝新线程。如果firstTask == null,那么可能是增加新线程来消耗Queue中的线程。但是同时还要检测queue是否为空,如果为空,那么队列已空,不需要增加消耗线程,如果队列没有空,那么可以将null插入队列中来清空队列。
for (;;) {
int wc = workerCountOf(c); //获取工作线程数
if (wc >= CAPACITY || //CAPACITY是ctl允许的最大工作线程数(2^29-1),不允许超出,否则导致状态变量出错
wc >= (core ? corePoolSize : maximumPoolSize)) //core为true 则判断最大核心线程数 否则最大线程数
return false; //如果超出规定的最大线程数 返回false
if (compareAndIncrementWorkerCount(c)) //CAS增加Worker数量
break retry;
c = ctl.get(); // Re-read ctl
if (runStateOf(c) != rs) //查看状态是否改变 如果改变需要重来
continue retry;
// else CAS failed due to workerCount change; retry inner loop
}
private final class Worker
extends AbstractQueuedSynchronizer
implements Runnable
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker 初始化 锁为-1
this.firstTask = firstTask;
this.thread = getThreadFactory().newThread(this);
}
/** Delegates main run loop to outer runWorker */
public void run() {
runWorker(this);
}
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
while (task != null || (task = getTask()) != null) { //Worker初始时是有task的 如果没有,那就去队列中取
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);
}
}
private Runnable getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
int wc = workerCountOf(c);
// Are workers subject to culling?
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
三、终止线程
shutdown方法停止接收新的任务,会完成线程池和队列中的任务。源码如下:
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess(); //判断可否操作目标线程
advanceRunState(SHUTDOWN); //设置线程池状态SHUTDOWN
interruptIdleWorkers(); //中断所有空闲线程
onShutdown(); // hook for ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
tryTerminate();
}
shutdown完成了这几件事:
1)检查能否操作线程
2)将线程池状态转为SHUTDOWN
3)中断所有空闲线程
shutdownNow会立刻停止接收新任务,且不再从队列中获取任务,而且停止正在运行的线程。
public List<Runnable> shutdownNow() {
List<Runnable> tasks;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(STOP); //设置状态
interruptWorkers();
tasks = drainQueue();
} finally {
mainLock.unlock();
}
tryTerminate();
return tasks;
}
它会中断所有线程。而不是空闲线程。然后抛弃队列中的所有任务。