之前写过一篇线程池,可以大致了解关于线程池的概念以及运行过程。 Java 线程池
线程池常量
// 标记线程池昨天,默认RUNNING
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
// 线程个数
private static final int COUNT_BITS = Integer.SIZE - 3;
// 线程最大个数
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
// 线程池状态
private static final int RUNNING = -1 << COUNT_BITS;
private static final int SHUTDOWN = 0 << COUNT_BITS;
private static final int STOP = 1 << COUNT_BITS;
private static final int TIDYING = 2 << COUNT_BITS;
private static final int TERMINATED = 3 << COUNT_BITS;
ThreadPoolExecutor#execute
execute(Runnable command) 方法是提交任务到线程池执行的。
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
// 获取当前线程池的状态+线程个数遍历组合值
int c = ctl.get();
// 当前线程池小于核心线程池,开启新线程执行
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
// 如果线程池处于RUNNING,将任务添加到队列中
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))
reject(command);
}
这块代码的逻辑就是线程池如何处理新任务的逻辑。
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// 检测队列是否为空
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
// 循环CAS 增加线程数
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) {
// 调用 w.run,实际调用 runWorker方法
t.start();
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
addWorker() 方法中参数 core 表明当前任务是绑定核心线程还是非核心线程。
非核心线程池回收
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;
// 判断当前活跃线程数是否大于最大线程数 || 允许超时 || 上次超时获取到的线程为null,接着判断 如果 wc >1 或队列为空,则将线程数减1,并且返回一个null的task
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;
}
}
}
当 timed = true
时,会执行 workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS)
方法。而 timed 的判断是在 boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
,allowCoreThreadTimeOut
默认 false,所以看 wc > corePoolSize
条件了,wc 是活跃线程数,表达式的意思是判断活跃线程数>核心线程数。
如果当前活跃线程数大于核心线程数,给定 keepAliveTime
时间等待获取队列任务,如果获取不到,返回null;否则直接从队列中获取任务。
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
// 循环获取task,在这里调用 getTask 方法,获取任务,如果获取不到任务,则执行 processWorkerExit 方法处理线程退出
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);
}
}
// 当线程获取不到 tasks,就调用 processWorkerExit 方法,处理线程退出
private void processWorkerExit(Worker w, boolean completedAbruptly) {
if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
decrementWorkerCount();
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
completedTaskCount += w.completedTasks;
// 执行remove操作,线程回收
workers.remove(w);
} finally {
mainLock.unlock();
}
tryTerminate();
int c = ctl.get();
if (runStateLessThan(c, STOP)) {
if (!completedAbruptly) {
int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
if (min == 0 && ! workQueue.isEmpty())
min = 1;
if (workerCountOf(c) >= min)
return; // replacement not needed
}
addWorker(null, false);
}
}
对于线程池中的线程,是没有核心线程和非核心线程的区别,只有当前工作线程。线程池对于非核心线程的回收,只是判断当前工作线程是否大于核心线程。所以,当工作线程大于核心线程数之后,所以的线程都是 poll,也就是说,所以线程都可能被回收。
总结:在 runWorker 执行任务时,会循环从队列中获取任务(getTask),当活跃线程大于核心线程时,就会设置过期超时时间获取队列任务,否则会直接获取队列任务。当获取不到任务时,就会执行线程退出任务,这时候,就会把多余的线程删除。