ThreadPoolExecutor
package java.util.concurrent;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.*;
public class ThreadPoolExecutor extends AbstractExecutorService {
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
private static final int COUNT_BITS = Integer.SIZE - 3;//32-3等于29
//28个1 即4 + 8*3
//0000[1111,11111111,11111111,11111111]
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
// 运行状态(runState)存储在bit位高4位
//仅RUNNING是一个负数(高位1),其它状态都是正整数
//1110 [0000,00000000,00000000,00000000]
//可接受新任务,并处理排队的任务
private static final int RUNNING = -1 << COUNT_BITS;
//0000 [0000,00000000,00000000,00000000]
//不接受新任务,但任然处理排队的任务
private static final int SHUTDOWN = 0 << COUNT_BITS;
//0010 [0000,00000000,00000000,00000000]
//不接受新任务,不处理排队的任务以及正在中断中的任务
private static final int STOP = 1 << COUNT_BITS;
//0100 [0000,00000000,00000000,00000000]
//所有任务已终止(workerCount为0),转换为状态TIDYING的线程将运行terminated()回调
private static final int TIDYING = 2 << COUNT_BITS;
//0110 [0000,00000000,00000000,00000000]
//terminated()回调执行完毕
private static final int TERMINATED = 3 << COUNT_BITS;
//TERMINATED > TIDYING > STOP > SHUTDOWN > RUNNING
//状态转换:
//1. RUNNING -> SHUTDOWN调用 shutdown()
//2. RUNNING 或 SHUTDOWN -> STOP调用 shutdownNow()
//3. SHUTDOWN -> TIDYING 队列和线程池都为空
//4. STOP -> TIDYING 线程池为空
//5. TIDYING -> TERMINATED当terminated()回调执行完毕
//调用awaitTermination()方法会等待直到状态是TERMINATED后返回
// Packing and unpacking ctl
//取高4位,低28位全是0
private static int runStateOf(int c) { return c & ~CAPACITY; }
//取低28位,代表工作线程数量
private static int workerCountOf(int c) { return c & CAPACITY; }
//以或的方式合并两个值
private static int ctlOf(int rs, int wc) { return rs | wc; }
//c的控制标识小于s
//TERMINATED > TIDYING > STOP > SHUTDOWN > RUNNING
private static boolean runStateLessThan(int c, int s) {
return c < s;
}
//c的控制标识大于等于s
//TERMINATED > TIDYING > STOP > SHUTDOWN > RUNNING
private static boolean runStateAtLeast(int c, int s) {
return c >= s;
}
//控制标识小于SHUTDOWN必定只有RUNNING状态
private static boolean isRunning(int c) {
return c < SHUTDOWN;
}
/**
* CAS操作
*/
private boolean compareAndIncrementWorkerCount(int expect) {
return ctl.compareAndSet(expect, expect + 1);
}
/**
* ctl递减1,其实就是递减当前工作者线程数,先比较再设置
*/
private boolean compareAndDecrementWorkerCount(int expect) {
return ctl.compareAndSet(expect, expect - 1);
}
/**
* 工作者线程数递减1,在循环中保证有竞争的情况下也能成功
*/
private void decrementWorkerCount() {
do {} while (! compareAndDecrementWorkerCount(ctl.get()));
}
//任务队列,当工作者线程数量超过corePoolSize时,新加的任务会先
//放到队列中,入队失败会继续创建工作者线程运行任务,但此时创建的
//总数不能超过maximumPoolSize,超过则拒绝任务
private final BlockingQueue<Runnable> workQueue;
private final ReentrantLock mainLock = new ReentrantLock();
//保存工作者线程的集合
private final HashSet<Worker> workers = new HashSet<Worker>();
//awaitTermination方法中用于等待调用shutdownNow结束
private final Condition termination = mainLock.newCondition();
//保存历史最大运行的工作线程数
private int largestPoolSize;
//所有工作线程完成的任务总和,只有当工作线程退出后才会累加
private long completedTaskCount;
//创建线程的工厂类
private volatile ThreadFactory threadFactory;
//拒绝一个任务的回调器
private volatile RejectedExecutionHandler handler;
//工作线程在得不到执行任务的情况下默认存活的最大超时时间,超时后销毁工作线程
//注:默认只对超过corePoolSize的线程进行超时销毁工作,要想对核心线程也进行超时销毁
//必须手动设置allowCoreThreadTimeOut为true即调用allowCoreThreadTimeOut()方法
private volatile long keepAliveTime;
//是否允许核心线程无任务执行后超时销毁,默认false也就是不销毁核心工作线程
private volatile boolean allowCoreThreadTimeOut;
//允许的最大核心线程数
private volatile int corePoolSize;
//允许的最大总线程数
private volatile int maximumPoolSize;
//默认的任务拒绝回调器(抛RejectedExecutionException异常)
private static final RejectedExecutionHandler defaultHandler =
new AbortPolicy();
private static final RuntimePermission shutdownPerm =
new RuntimePermission("modifyThread");
/* The context to be used when executing the finalizer, or null. */
private final AccessControlContext acc;
//Worker其实就是用户任务的包装器,与用户任务一样实现了Runnable接口,
//内部维护了一个Thread类实例,当运行任务时不直接运行用户任务,而是运行worker
//然后通过worker去执行用户任务。
//worker主要作用是除了可以执行默认构造器的传入的用户任务,
//也可以继续从任务队列中获取用户任务执行
private final class Worker
extends AbstractQueuedSynchronizer//实现同步器支持锁定、解锁操作
implements Runnable
{
private static final long serialVersionUID = 6138294804551838833L;
//用来执行worker任务的线程
final Thread thread;
//构造器实例化Worker时传入的第一个要执行的用户任务,
//当这个任务执行完毕后会从任务队列中再获取任务执行
Runnable firstTask;
//当前工作线程完成的任务数
volatile long completedTasks;
Worker(Runnable firstTask) {
//这里初始化-1的目的就是其它线程无法获取到锁,
//直到runWorker里面调用w.unlock();释放锁
//也就是说在运行到runWorker之前此线程是无法中断的
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;//最先执行的用户任务
this.thread = getThreadFactory().newThread(this);//创建Thread实例
}
/** 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(); }
//调用shutdownNow()方法最终会中断所有线程进而调用此方法
//不过从这个方法看,只要没运行到runWorker方法那么线程是不可中断的(state初始值是-1),
//这样会不会引起中断信号错失?
//不会!因为在运行shutdownNow方法,中断线程之前会设置状态为STOP,然后执行中断方法,
//此时如果还没运行到runWorker会错失中断,等运行到runWorker执行任务时会检查状态,发现
//是STOP后会手动调用wt.interrupt();来执行线程中断,尽管如此,正在执行的用户任务依旧可以继续执行,
//直到执行完毕后销毁线程,具体看runWorker与getTask方法
void interruptIfStarted() {
Thread t;
if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
try {
t.interrupt();
} catch (SecurityException ignore) {
}
}
}
}
//如果当前runState小于targetState则设置为targetState,否则不设置
private void advanceRunState(int targetState) {
for (;;) {
int c = ctl.get();
if (runStateAtLeast(c, targetState) ||
ctl.compareAndSet(c, ctlOf(targetState, workerCountOf(c))))
break;
}
}
//只要涉及工作线程终止、退出、移除之类操作(如调shutdown,shutdownNow,addWorkerFailed,processWorkerExit)
//时总会调用此方法做状态检查或状态转换,
//如果发现线程池执行了
//shutdown,shutdownNow方法后调用此方法判断是否有必要进行结束清理工作,比如调用了
//shutdownNow后需要中断线程。或当所有线程都退出后,执行terminated回调方法,以及设置最终状态
//为TERMINATED并唤起等待线程池关闭结束的线程。
final void tryTerminate() {
for (;;) {
int c = ctl.get();
//RUNNING状态直接返回
if (isRunning(c) ||
//TIDYING说明其他线程已经在执行tryTerminate方法并试图设置终止状态为TERMINATED
//那么直接返回
runStateAtLeast(c, TIDYING) ||
//状态是SHUTDOWN,但任务队列有任务等待执行,不能终止
//这种情况是由于调用了shutdown方法,此时必须等任务队列的任务都执行完毕后
//才可以终止
(runStateOf(c) == SHUTDOWN && ! workQueue.isEmpty()))
return;
//1.状态是SHUTDOWN且工作队列是空
//2.状态是STOP(调用了shutdownNow方法)
if (workerCountOf(c) != 0) {
//终止线程池前需要中断工作线程,以便其尽快退出,两种情况:
//1. 调用的是shutdow()方法那么此时需要中断空闲的线程
// 这里每次只中断一个空闲线程,不是一次中断所有空闲线程。当一个线程退出后
// 会再次调用tryTerminate方法继续中断下一个空闲线程,这是一个递归的过程。
interruptIdleWorkers(ONLY_ONE);
//2. 调用的是shutdowNow(),调用时会中断所有工作线程
//返回
return;
}
//当所有工作线程都退出后执行回调切换到终止态
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
//将状态设置为TIDYING(执行terminated钩子方法,默认是空实现,一般由子类覆盖)
if (ctl.compareAndSet(c, ctlOf(TIDYING, 0))) {
try {
terminated();
} finally {
//钩子方法执行完毕后将状态设置为TERMINATED
ctl.set(ctlOf(TERMINATED, 0));
//唤醒因调用awaitTermination方法而等待的线程
termination.signalAll();
}
return;
}
} finally {
mainLock.unlock();
}
// else retry on failed CAS
}
}
//关闭权限检查
private void checkShutdownAccess() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(shutdownPerm);
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
for (Worker w : workers)
security.checkAccess(w.thread);
} finally {
mainLock.unlock();
}
}
}
//中断所有线程,由shutdownNow()方法调用
private void interruptWorkers() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
for (Worker w : workers)
w.interruptIfStarted();
} finally {
mainLock.unlock();
}
}
//中断空闲线程,目的是注销空闲线程
private void interruptIdleWorkers(boolean onlyOne) {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
for (Worker w : workers) {
Thread t = w.thread;
//执行w.tryLock()返回true说明是空闲线程,否则说明此线程
//正在执行任务
if (!t.isInterrupted() && w.tryLock()) {
try {
t.interrupt();
} catch (SecurityException ignore) {
} finally {
w.unlock();
}
}
if (onlyOne)
break;
}
} finally {
mainLock.unlock();
}
}
private void interruptIdleWorkers() {
interruptIdleWorkers(false);
}
private static final boolean ONLY_ONE = true;
final void reject(Runnable command) {
handler.rejectedExecution(command, this);
}
void onShutdown() {
}
//有ScheduledThreadPoolExecutor调用检查在shutdown时是否可以运行任务
final boolean isRunningOrShutdown(boolean shutdownOK) {
int rs = runStateOf(ctl.get());
return rs == RUNNING || (rs == SHUTDOWN && shutdownOK);
}
//调用shutdownNow方法是会将任务队列还没执行的任务移除并返回
private List<Runnable> drainQueue() {
BlockingQueue<Runnable> q = workQueue;
ArrayList<Runnable> taskList = new ArrayList<Runnable>();
q.drainTo(taskList);
//DelayQueue中,元素超时前移除会失败
//这种情况下手动一个个移除
if (!q.isEmpty()) {
for (Runnable r : q.toArray(new Runnable[0])) {
if (q.remove(r))
taskList.add(r);
}
}
return taskList;
}
/**
*创建一个工作线程并执行指定的任务(启动线程调用worker.thread.start()),
core为true代表工作线程数不能超过corePoolSize,
*否则不能超过maximumPoolSize
*/
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);//取高4位低28位全0
// Check if queue empty only if necessary.
if (rs >= SHUTDOWN &&//大于SHUTDOWN状态为:STOP,TIDYING,TERMINATED
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
//执行如下方法前提:
//1. 线程池状态是RUNNING
//2. 线程是状态是SHUTDOWN且firstTask为null且队列不是空
// 这种情况是为了让任务队列中的任务尽快执行完毕,
// 情形之一:当一个工作线程因执行了抛异常的任务导致退出时,需要重新添加一个工作者线程
// 来消耗队列任务,查看processWorkerExit末尾
for (;;) {
int wc = workerCountOf(c);
//限制添加的线程数不能超过最大值
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
//累加1,并执行比较设置,设置失败,则重新执行for循环直到成功
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 {
//firstTask为null是,工作线程从任务队列中获取任务执行
//否则先执行firstTask再从任务队列中获取任务执行
w = new Worker(firstTask);
//Worker包含一个Thread实例用于执行任务,Worker其实是一个任务包装器
//thread运行任务包装器,包装器负责取任务执行
final Thread t = w.thread;
if (t != null) {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
int rs = runStateOf(ctl.get());
if (rs < SHUTDOWN ||//状态是RUNNING或
//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();//Worker实例成功添加到集合中,启动Worker的线程
workerStarted = true;
}
}
} finally {
if (! workerStarted)
//线程未启动成功,那么从集合中删除worker实例并递减工作线程数
addWorkerFailed(w);
}
return workerStarted;
}
//移除已添加的工作线程,必定是先添加成功才会执行此方法回退
//如:提交任务并创建工作线程,在此工作线程启动前另一个线程调用了shutdown方法
private void addWorkerFailed(Worker w) {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
if (w != null)
workers.remove(w);//(如果已存在)从集合中移除
decrementWorkerCount();//递减计数
//检查状态是否处于非RUNNING并帮助其尽早结束
tryTerminate();
} finally {
mainLock.unlock();
}
}
//当工作者线程运行结束后调用此方法,情形:
//1.正常结束:
// a.当前工作线程数超过了corePoolSize,那么会终止超时(keepAliveTime)的空闲线程.
// b.启用了allowCoreThreadTimeOut,那么会终止所有超时的空闲线程
//2.中断结束,调用了shutdownNow()
//3.异常结束,运行了抛异常的任务
private void processWorkerExit(Worker w, boolean completedAbruptly) {
//线程退出将worker从集合中删除,completedAbruptly代表是否是异常结束
//true,那么方法内部需要调用decrementWorkerCount递减工作线程数,
//false正常通过getTask返回null退出的,无需递减,因为在
//getTask方法中返回null前已经做了递减操作
if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
decrementWorkerCount();
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
//保存完成总数
completedTaskCount += w.completedTasks;
workers.remove(w);//从集合中移除worker
} finally {
mainLock.unlock();
}
//检查状态是否处于非RUNNING并帮助其尽早结束
tryTerminate();
int c = ctl.get();
//如果线程池状态是RUNNING或SHUTDOWN(此状态可以继续运行任务队列里的任务)
//如果工作线程是异常结束,那么调用addWorker替补一个工作线程
if (runStateLessThan(c, STOP)) {
if (!completedAbruptly) {
int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
if (min == 0 && ! workQueue.isEmpty())
min = 1;//如果任务队列还有任务要执行,至少保证有一个工作者线程
if (workerCountOf(c) >= min)
//如果已有的工作做线程数大于等于min则返回,否则需要增加工作者线程
return;
}
addWorker(null, false);
}
}
//从队列中获取任务给工作线程执行,如果队列为空则等待直到超时(如果有),
//当getTask返回null时工作线程将退出并销毁
private Runnable getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
//1.状态是SHUTDOWN(调用了shutdown方法)且任务队列是空(没有任务要执行)
//2.状态是STOP(调用了shutdownNow方法),所有线程都执行中断不在运行任务队列的任务
//这两种情况下线程可以退出了(返回null即可)
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
int wc = workerCountOf(c);
//允许超时包括设置了allowCoreThreadTimeOut或总线程数超过核心线程数
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
//由于调用setMaximumPoolSize方法导致wc大于maximumPoolSize
//或者允许超时,则需要清除一部分工作线程
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))//这里只是将ctl标识值递减
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;
}
}
}
//执行Worker中Thread实例的start方法后,调用Worker的run方法后将自身Worker传给ThreadPoolExecutor的runWorker方法
//一旦runWorker从方法返回,则说明Worker对应的Thread将终止
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
//取消锁,一旦运行到这里那么此工作线程可以中断,
//在这之前不允许
w.unlock();
boolean completedAbruptly = true;
try {
//先执行当前worker的任务再从队列中取任务
//如果队列没有任务调用getTask会阻塞直到超时(如果有)后返回null,最后线程退出
while (task != null || (task = getTask()) != null) {
w.lock();//外部可以通过tryLock来判断当前线程是否正在执行任务
//shutdownNow方法中也会执行中断,但多线程竞争不能保证谁先执行,安全起见这里也尝试中断
if ((runStateAtLeast(ctl.get(), STOP) ||
//interrupted方法返回前会清除中断标识,后面发现runStateAtLeast(ctl.get(), STOP)返回false
//将不设置中断标识(也就是这种情况下,对已中断的线程进行了恢复)
(Thread.interrupted() && runStateAtLeast(ctl.get(), STOP))
) &&
!wt.isInterrupted())
//设置中断标识,其实这个标识是传给用户任务的
//这里的中断并不一定会导致工作线程退出,
//查看getTask,对中断异常做了捕获,除非状态是STOP
//或SHUTDOWN且任务队列为空
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 {
//线程退出将worker从集合中删除,completedAbruptly代表是否发生意外
//true意外退出,那么方法内部需要调用decrementWorkerCount递减工作线程数,
//false正常通过getTask返回null退出的,无需递减,因为在
//getTask方法中返回null前已经做了递减操作
processWorkerExit(w, completedAbruptly);
}
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
threadFactory, defaultHandler);
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), handler);
}
/**
*向线程池添加一个任务时,如果工作线程小于corePoolSize则创建一个工作线程并执行任务,
*如果工作线程数已经大于或等于corePoolSize则将当前任务放入工作队列workQueue中,
*如果工作队列已满或入队失败则继续创建工作线程,直到线程数达到maximumPoolSize为止。
*当线程池关闭或线程池已饱和(队列已满且工作线程数达到maximumPoolSize)则拒绝任务
*/
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;
}
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
//第一次运行状态是1110 [0000,00000000,00000000,00000000]RUNNING
int c = ctl.get();
//如果工作者线程数小于核心线程数,则使用指定的任务创建一个线程并启动
if (workerCountOf(c) < corePoolSize) {
//尝试创建一个工作线程(true表示总线程数不能超过corePoolSize)
if (addWorker(command, true))
return;
//创建失败(其它线程抢先执行,导致线程数达到corePoolSize)
//重新获取控制标识继续后面流程
c = ctl.get();
}
//核心线程数已满,则将任务放到队列中(调用队列的非阻塞版的入队方法offer)
//队列已满则继续尝试创建工作线程,但总线程数不能超过maximumPoolSize,否则将拒绝任务
if (isRunning(c) && workQueue.offer(command)) {
//状态是RUNNING并且任务放入队列成功
int recheck = ctl.get();
//如果已经关闭那么从队列中移除任务
if (! isRunning(recheck) && remove(command))
reject(command);
//如果工作线程是0那么创建一个工作线程(corePoolSize为0)
else if (workerCountOf(recheck) == 0)
//firstTask是null会重任务队列获取任务执行
addWorker(null, false);
}
//如果线程入队失败,尝试创建一个工作线程(false表示总线程数不能超过maximumPoolSize)
//如果依旧失败说明线程池已关闭或已饱和
else if (!addWorker(command, false))
reject(command);
}
//关闭线程池,不允许提交新任务,但可以继续执行已提交的任务
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
//设置状态标识是SHUTDOWN,如果已有的状态标识
//值大于SHUTDOWN则不设置
advanceRunState(SHUTDOWN);
//这里只是尝试向空闲线程发送中断信号,
//实际是否可以真的退出线程还得看中断时刻
//队列是否有任务,看getTask方法
interruptIdleWorkers();
//钩子方法ScheduledThreadPoolExecutor里重写
onShutdown();
} finally {
mainLock.unlock();
}
//检查状态是否处于非RUNNING并帮助其尽早结束
tryTerminate();
}
//关闭线程池,不允许提交新任务,发送中断信号给正在运行的任务,
//以及删除任务队列排队的任务并返回
public List<Runnable> shutdownNow() {
List<Runnable> tasks;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
//设置状态标识是STOP,如果已有的状态标识
//值大于STOP则不设置
advanceRunState(STOP);
//给所有正在运行的工作线程发送中断
interruptWorkers();
//清空任务队列中未执行的任务并返回
tasks = drainQueue();
} finally {
mainLock.unlock();
}
//检查状态是否处于非RUNNING并帮助其尽早结束
tryTerminate();
return tasks;
}
//只要状态不是RUNNING就标有线程池关闭或已关闭
public boolean isShutdown() {
return ! isRunning(ctl.get());
}
//线程池是否正在结束中:状态非RUNNING并且还没到达TERMINATED
//即SHUTDOWN,STOP,TIDYING之一
public boolean isTerminating() {
int c = ctl.get();
return ! isRunning(c) && runStateLessThan(c, TERMINATED);
}
//线程池是否已经结束:即状态是TERMINATED
public boolean isTerminated() {
return runStateAtLeast(ctl.get(), TERMINATED);
}
//调用shutdownNow方法后等待线程池结束
public boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
for (;;) {
//检查运行状态必须是TERMINATED后才返回true
if (runStateAtLeast(ctl.get(), TERMINATED))
return true;
if (nanos <= 0)
return false;
//否则等待其完成
nanos = termination.awaitNanos(nanos);
}
} finally {
mainLock.unlock();
}
}
protected void finalize() {
SecurityManager sm = System.getSecurityManager();
if (sm == null || acc == null) {
shutdown();
} else {
PrivilegedAction<Void> pa = () -> { shutdown(); return null; };
AccessController.doPrivileged(pa, acc);
}
}
//这种线程创建工厂,默认工厂创建的是非后台线程,如果需要
//创建后台线程或自定义线程名称可以自定义工厂
public void setThreadFactory(ThreadFactory threadFactory) {
if (threadFactory == null)
throw new NullPointerException();
this.threadFactory = threadFactory;
}
public ThreadFactory getThreadFactory() {
return threadFactory;
}
//设置拒绝任务的回调器,默认回调器手动抛异常
public void setRejectedExecutionHandler(RejectedExecutionHandler handler) {
if (handler == null)
throw new NullPointerException();
this.handler = handler;
}
public RejectedExecutionHandler getRejectedExecutionHandler() {
return handler;
}
//调整线程池核心线程的最大数量
public void setCorePoolSize(int corePoolSize) {
if (corePoolSize < 0)
throw new IllegalArgumentException();
int delta = corePoolSize - this.corePoolSize;
this.corePoolSize = corePoolSize;
if (workerCountOf(ctl.get()) > corePoolSize)
//当前工作线程超过了指定的核心数,中断空闲线程
interruptIdleWorkers();
else if (delta > 0) {
int k = Math.min(delta, workQueue.size());
//如果delta大于0那么新增工作者线程
while (k-- > 0 && addWorker(null, true)) {
//如果任务队列无任务无需创建过多的工作线程,直接退出
if (workQueue.isEmpty())
break;
}
}
}
public int getCorePoolSize() {
return corePoolSize;
}
//预创建一个工作线程(如果线程数还没到达corePoolSize)
//如果要多个需要置于循环中,如prestartAllCoreThreads()方法
public boolean prestartCoreThread() {
return workerCountOf(ctl.get()) < corePoolSize &&
addWorker(null, true);
}
void ensurePrestart() {
int wc = workerCountOf(ctl.get());
if (wc < corePoolSize)
addWorker(null, true);
else if (wc == 0)
addWorker(null, false);
}
public int prestartAllCoreThreads() {
int n = 0;
while (addWorker(null, true))
++n;
return n;
}
//是否允许所有工作线程超时,默认false,也就是对超过corePoolSize的线程允许超时
public boolean allowsCoreThreadTimeOut() {
return allowCoreThreadTimeOut;
}
//允许线程超时设置
public void allowCoreThreadTimeOut(boolean value) {
if (value && keepAliveTime <= 0)
//允许超时的前提是keepAliveTime必须大于0
throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
if (value != allowCoreThreadTimeOut) {
allowCoreThreadTimeOut = value;
if (value)
//尝试中断空闲线程
interruptIdleWorkers();
}
}
public void setMaximumPoolSize(int maximumPoolSize) {
if (maximumPoolSize <= 0 || maximumPoolSize < corePoolSize)
throw new IllegalArgumentException();
this.maximumPoolSize = maximumPoolSize;
if (workerCountOf(ctl.get()) > maximumPoolSize)
//总线程数超过了预设的最大值,则中断空闲线程
interruptIdleWorkers();
}
public int getMaximumPoolSize() {
return maximumPoolSize;
}
public void setKeepAliveTime(long time, TimeUnit unit) {
if (time < 0)
throw new IllegalArgumentException();
if (time == 0 && allowsCoreThreadTimeOut())
throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
long keepAliveTime = unit.toNanos(time);
long delta = keepAliveTime - this.keepAliveTime;
this.keepAliveTime = keepAliveTime;
if (delta < 0)
//如果空闲时间缩减尝试中断空闲线程
interruptIdleWorkers();
}
public long getKeepAliveTime(TimeUnit unit) {
return unit.convert(keepAliveTime, TimeUnit.NANOSECONDS);
}
public BlockingQueue<Runnable> getQueue() {
return workQueue;
}
//从任务队列删除一个任务(也就是任务还没有运行才可以删除)
public boolean remove(Runnable task) {
boolean removed = workQueue.remove(task);
tryTerminate(); // In case SHUTDOWN and now empty
return removed;
}
//清除此刻任务队列的任务
public void purge() {
final BlockingQueue<Runnable> q = workQueue;
try {
Iterator<Runnable> it = q.iterator();
while (it.hasNext()) {
Runnable r = it.next();
//通过submit提交的任务会包装成Future任务
if (r instanceof Future<?> && ((Future<?>)r).isCancelled())
it.remove();
}
} catch (ConcurrentModificationException fallThrough) {
// Take slow path if we encounter interference during traversal.
// Make copy for traversal and call remove for cancelled entries.
// The slow path is more likely to be O(N*N).
for (Object r : q.toArray())
if (r instanceof Future<?> && ((Future<?>)r).isCancelled())
q.remove(r);
}
tryTerminate(); // In case SHUTDOWN and now empty
}
//返回工作线程数
public int getPoolSize() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
//如果状态是TIDYING,说明趋于结束返回0,否则返回线程集合长度
return runStateAtLeast(ctl.get(), TIDYING) ? 0
: workers.size();
} finally {
mainLock.unlock();
}
}
//返回正在运行任务的工作线程数(不包含空闲线程)
public int getActiveCount() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
int n = 0;
for (Worker w : workers)
//通过是否持有锁来判断线程是否在运行任务
//工作线程正在运行任务时总是持有锁的
if (w.isLocked())
++n;
return n;
} finally {
mainLock.unlock();
}
}
public int getLargestPoolSize() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
return largestPoolSize;
} finally {
mainLock.unlock();
}
}
//返回总的任务数:包括过去已运行的和正在运行的和等待运行的
public long getTaskCount() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
long n = completedTaskCount;
for (Worker w : workers) {
n += w.completedTasks;//过去已运行的
if (w.isLocked())//正在运行的
++n;
}
return n + workQueue.size();//等待运行的
} finally {
mainLock.unlock();
}
}
//返回以往已运行结束的任务数
public long getCompletedTaskCount() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
long n = completedTaskCount;
for (Worker w : workers)
n += w.completedTasks;
return n;
} finally {
mainLock.unlock();
}
}
public String toString() {
long ncompleted;
int nworkers, nactive;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
ncompleted = completedTaskCount;
nactive = 0;
nworkers = workers.size();
for (Worker w : workers) {
ncompleted += w.completedTasks;
if (w.isLocked())
++nactive;
}
} finally {
mainLock.unlock();
}
int c = ctl.get();
String rs = (runStateLessThan(c, SHUTDOWN) ? "Running" :
(runStateAtLeast(c, TERMINATED) ? "Terminated" :
"Shutting down"));
return super.toString() +
"[" + rs +
", pool size = " + nworkers +
", active threads = " + nactive +
", queued tasks = " + workQueue.size() +
", completed tasks = " + ncompleted +
"]";
}
protected void beforeExecute(Thread t, Runnable r) { }
protected void afterExecute(Runnable r, Throwable t) { }
protected void terminated() { }
public static class CallerRunsPolicy implements RejectedExecutionHandler {
public CallerRunsPolicy() { }
//非默认拒绝策略,如果提交时任务被拒,则当前线程直接执行此任务
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
public static class AbortPolicy implements RejectedExecutionHandler {
public AbortPolicy() { }
//默认拒绝策略,抛异常
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
public static class DiscardPolicy implements RejectedExecutionHandler {
public DiscardPolicy() { }
//非默认拒绝策略,安静地抛弃任务
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
public DiscardOldestPolicy() { }
//非默认拒绝策略, 从任务队列删除一个在添加
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}
}
}
AbstractExecutorService
package java.util.concurrent;
import java.util.*;
public abstract class AbstractExecutorService implements ExecutorService {
//使用submit提交的任务会返回一个FutureTask实例保存执行任务的结果
//由于runnable是void方法,因此任务执行结束后需要在此指定返回值value
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
return new FutureTask<T>(runnable, value);
}
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return new FutureTask<T>(callable);
}
//submit与execute区别是submit会将task包装成
//FutureTask并执行execute
public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException();
RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask);
return ftask;
}
public <T> Future<T> submit(Runnable task, T result) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task, result);
execute(ftask);
return ftask;
}
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task);
execute(ftask);
return ftask;
}
//任务集合中只要有一个任务执行完毕返回就返回
private <T> T doInvokeAny(Collection<? extends Callable<T>> tasks,
boolean timed, long nanos)
throws InterruptedException, ExecutionException, TimeoutException {
if (tasks == null)
throw new NullPointerException();
int ntasks = tasks.size();
if (ntasks == 0)
throw new IllegalArgumentException();
ArrayList<Future<T>> futures = new ArrayList<Future<T>>(ntasks);
//对当前连接池进行代理,此代理类维护了一个completionQueue队列
//当一个FutureTask执行完毕后会将此FutureTask放入completionQueue队列
ExecutorCompletionService<T> ecs =
new ExecutorCompletionService<T>(this);
try {
ExecutionException ee = null;
final long deadline = timed ? System.nanoTime() + nanos : 0L;
Iterator<? extends Callable<T>> it = tasks.iterator();
//先提交一个任务
futures.add(ecs.submit(it.next()));
--ntasks;
//active代表正在执行的任务数
int active = 1;
for (;;) {
//从completionQueue中弹出一个已完成的任务
Future<T> f = ecs.poll();
//在提交更多任务前,先判断第一个任务是否执行完毕
//如果第上个任务执行完毕(f不为null)那么当前方法返回
//否则继续提交剩余任务
if (f == null) {
if (ntasks > 0) {//for循环连续提交剩余任务
--ntasks;
futures.add(ecs.submit(it.next()));
++active;
}
else if (active == 0)//说明所有任务都提交完成
break;
else if (timed) {
f = ecs.poll(nanos, TimeUnit.NANOSECONDS);
if (f == null)
throw new TimeoutException();
nanos = deadline - System.nanoTime();
}
else
f = ecs.take();
}
if (f != null) {
--active;
try {
return f.get();
} catch (ExecutionException eex) {
ee = eex;
} catch (RuntimeException rex) {
ee = new ExecutionException(rex);
}
}
}
if (ee == null)
ee = new ExecutionException();
throw ee;
} finally {
for (int i = 0, size = futures.size(); i < size; i++)
//有一个返回即可,取消其它任务的执行
futures.get(i).cancel(true);
}
}
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException {
try {
return doInvokeAny(tasks, false, 0);
} catch (TimeoutException cannotHappen) {
assert false;
return null;
}
}
public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
return doInvokeAny(tasks, true, unit.toNanos(timeout));
}
//执行所有任务,并等待返回
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException {
if (tasks == null)
throw new NullPointerException();
ArrayList<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
boolean done = false;
try {
for (Callable<T> t : tasks) {
RunnableFuture<T> f = newTaskFor(t);
futures.add(f);
execute(f);
}
for (int i = 0, size = futures.size(); i < size; i++) {
Future<T> f = futures.get(i);
if (!f.isDone()) {
try {
f.get();//等待任务执行完毕
} catch (CancellationException ignore) {
} catch (ExecutionException ignore) {
}
}
}
done = true;
return futures;
} finally {
if (!done)
//只要有一个任务异常则取消所有执行中的任务
for (int i = 0, size = futures.size(); i < size; i++)
futures.get(i).cancel(true);
}
}
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException {
if (tasks == null)
throw new NullPointerException();
long nanos = unit.toNanos(timeout);
ArrayList<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
boolean done = false;
try {
for (Callable<T> t : tasks)
futures.add(newTaskFor(t));
final long deadline = System.nanoTime() + nanos;
final int size = futures.size();
// Interleave time checks and calls to execute in case
// executor doesn't have any/much parallelism.
for (int i = 0; i < size; i++) {
execute((Runnable)futures.get(i));
nanos = deadline - System.nanoTime();
if (nanos <= 0L)
return futures;
}
for (int i = 0; i < size; i++) {
Future<T> f = futures.get(i);
if (!f.isDone()) {
if (nanos <= 0L)
return futures;
try {
f.get(nanos, TimeUnit.NANOSECONDS);
} catch (CancellationException ignore) {
} catch (ExecutionException ignore) {
} catch (TimeoutException toe) {
return futures;
}
nanos = deadline - System.nanoTime();
}
}
done = true;
return futures;
} finally {
if (!done)
for (int i = 0, size = futures.size(); i < size; i++)
futures.get(i).cancel(true);
}
}
}