本文转自此博客
/**
* @param corePoolSize 维护的corePool核心池大小
* @param maximumPoolSize 维护的最大线程池大小
* @param keepAliveTime 空闲线程的存活时间
* @param unit keepAliveTime的单位
* @param workQueue 任务队列,保存已提交但尚未被执行的线程
* @param threadFacyory 线程工厂-用于创建线程
* @param 拒绝策略(任务太多时的处理策略)
*/
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;
}
ThreadPoolExecutor内部定义了一个AtomicInteger变量——ctl,通过按位划分的方式,在一个变量中记录线程池状态和工作线程数——低29位保存线程数,高3位保存线程池状态
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;
// runState is stored in the high-order bits
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;
//获取线程池状态
private static int runStateOf(int c) { return c & ~CAPACITY; }
//获取当前正在工作的worker数量
private static int workerCountOf(int c) { return c & CAPACITY; }
//获取ctl
private static int ctlOf(int rs, int wc) { return rs | wc; }
线程池状态:
//工作线程的集合
private final HashSet<Worker> workers = new HashSet<Worker>();
//工作线程
/**
*Worker实现Runnable接口,继承自AQS
*同步状态值定义state(独占模式下)
*-1表示初始状态
*0表示无锁状态
*1表示加锁状态
*/
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. */
//每个Worker都会维护一个Thread线程对象
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构造器
Worker(Runnable firstTask) {
//初始化同步状态为-1
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
//调用工厂生产thread
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) {
//CAS操作,若当前锁没有线程持有,则直接加锁
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) {
}
}
}
}
每个Worker对象维护着一个Thread对象,当任务需要执行时,实际是调用Thread的start方法,而Thread对象是在Worker的构造器中通过工厂方法创建的,创建的Thread将Worker自身作为任务,所以当调用Thread的start方法,最终还是调用了Worker的run方法,该方法内部委托给runWorker方法执行任务。
//线程工厂接口
public interface ThreadFactory {
//传入实现Runnable的类,返回Thread
Thread newThread(Runnable r);
}
ThreadFactory在ThreadPoolExecutor下的实现
//ThreadPoolExecutor下维护着一个线程工厂
private volatile ThreadFactory threadFactory;//需要被用户传入
//Worker类中调用getThreadFactory。仅仅是返回ThreadPoolExcutor下维护的ThreadFactory
public ThreadFactory getThreadFactory() {
return threadFactory;
}
ThreadFactory需要由用户传入,如果用户不传入,默认使用:DefaultThreadFactory
/**
* The default thread factory
*/
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
//默认构造,进行线程组和线程名前缀的配置
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
//创建线程
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
Worker类中使用线程工厂实现线程的构造目的在于解耦线程创建与线程任务执行。
ExecutorService的核心方法是submit方法——用于提交一个待执行的任务,它并没有覆写submit方法,而是沿用了父类AbstractExecutorService的模板,然后自己实现了execute方法:
// AbstractExecutorService下的submit
//采用模板设计模式,子类完成execute方法
public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException();
RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask);
return ftask;
}
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.
*/
/**
*1.如果核心池中的线程数小于核心池规定的大小,则创建新线程,并将此任务作为该线程的firsttask
*2.如果工作线程数超过核心池大小,将任务插入至队列
*3.工作队列满了,如果工作线程数小于最大线程数规定的大小则新建线程
*/
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
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))
reject(command);
}
这里需要特别注意的是 CASE2中的addWorker(null, false),当将任务成功添加到队列后,如果此时的工作线程数为0,就会执行这段代码。
一般来讲每个工作线程(Worker)都有一个Runnable任务和一个对应的执行线程Thread,当我们调用addWorker方法时,如果不传入相应的任务,那么就只是新建了一个没有任务的工作线程(Worker),该Worker就会从工作队列中取任务来执行(因为自己没有绑定任务)。如果传入了任务,新建的工作线程就会执行该任务。
所以execute方法的CASE2中,将任务添加到队列后,需要判断工作线程数是否为0,如果是0那么就必须新建一个空任务的工作线程,将来在某一时刻它会去队列取任务执行,否则没有工作线程的话,该队列中的任务永远不会被执行
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主要判断哪些情况下,线程池不再接受新任务的执行,而是直接返回
//1.线程池状态为STOP或TIDYING或TERMINETED
//2.线程池状态为SHUTDOWN且firstTask不为空
//3,线程池状态为SHUTDOWN且队列为空
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
for (;;) {
//获取工作线程数量
int wc = workerCountOf(c);
//如果工作线程数量已经大于阈值或者核心池数量大于最大池数量,返回false
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
//CAS设置工作线程数量,+1
if (compareAndIncrementWorkerCount(c))
break retry;//跳出最外层循环
c = ctl.get(); // Re-read ctl
//来到这里,说明前面CAS操作失败,线程池状态在CAS操作时发生改变,进行自旋操作
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;//返回Worker维护的线程
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方法分为两部分
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) {//当task为null,会通过getTasj从队列取任务
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的判断作用
//1.保证当前线程状态为STOP/TIDYING/TERMINATED时,当前执行任务的线程wt是中断状态
//2.保证当前线程状态为RUNNING/SHUTDOWN,当前执行任务的线程wt不是中断状态
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);//处理工作线程的退出工作
}
}
processWorkerExit的作用就是将该退出的工作线程清理掉,然后看下线程池是否需要终止。
private void processWorkerExit(Worker w, boolean completedAbruptly) {
if (completedAbruptly) // 工作线程因异常情况而退出
decrementWorkerCount();//工作线程数-1
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
completedTaskCount += w.completedTasks;//记录线程池完成的总任务数
workers.remove(w);
} finally {
mainLock.unlock();
}
tryTerminate();//根据线程池状态,判断是否要终止线程池
int c = ctl.get();
if (runStateLessThan(c, STOP)) { //如果线程池状态为RUNNING/SHUTDOWN,进入if循环
if (!completedAbruptly) { //线程为正常退出,进入if循环
int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
if (min == 0 && ! workQueue.isEmpty())
min = 1;
if (workerCountOf(c) >= min)
return; // replacement not needed
}
addWorker(null, false);//新建一个工作线程
}
}
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.
//如果线程池状态为SHUTDOWN,同时线程池中任务队列为空或为STOP状态,则减少工作线程数量
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
//获取工作线程数量
int wc = workerCountOf(c);
// Are workers subject to culling?
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
//当外部setMaximumPoolSize重新设置最大线程数,需要回收多出的工作线程数
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
//默认timed为0,执行take(),从任务队列获取任务
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;//超时仍未获取任务
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
getTask方法的主要作用就是:通过自旋,不断地尝试从阻塞队列中获取一个任务,如果获取失败则返回null
阻塞队列就是在我们构建ThreadPoolExecutor对象时,在构造器中指定的。由于队列是外部指定的,所以根据阻塞队列的特性不同,getTask方法的执行情况也不同。
ThreadPoolExecutor在以下两种情况下会执行拒绝策略:
所谓拒绝策略,就是在构造ThreadPoolExecutor时,传入的RejectedExecutionHandler对象
//拒绝策略接口
public interface RejectedExecutionHandler {
void rejectedExecution(Runnable r, ThreadPoolExecutor executor);
}
ThreadPoolExecutor一共提供了4种拒绝策略
AbortPolicy(默认)
AbortPolicy策略其实就是抛出一个RejectedExecutionException异常:
public static class AbortPolicy implements RejectedExecutionHandler {
/**
* Creates an {@code AbortPolicy}.
*/
public AbortPolicy() { }
//抛出异常
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
DiscardPolicy
DiscardPolicy策略其实就是无为而治,什么都不做,等任务自己被回收:
public static class CallerRunsPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code CallerRunsPolicy}.
*/
public CallerRunsPolicy() { }
//无作为的拒绝策略
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
CallerRunsPolicy
CallerRunsPolicy策略相当于以自身线程来执行任务,这样可以减缓新任务提交的速度
public static class CallerRunsPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code CallerRunsPolicy}.
*/
public CallerRunsPolicy() { }
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
//如果线程池未关闭,根据传入的线程来执行任务,缓解压力
if (!e.isShutdown()) {
r.run();
}
}
}
DiscardOldestPolicy
DiscardOldestPolicy策略是丢弃任务队列中的最近一个任务,并执行当前任务:
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
public DiscardOldestPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) { // 线程池未关闭(RUNNING)
e.getQueue().poll(); // 丢弃任务队列中的最近任务
e.execute(r); // 执行当前任务
}
}
}
shutdown方法将线程池切换到SHUTDOWN状态(如果已经停止,则不用切换),并调用interruptIdleWorkers方法中断所有空闲的工作线程,最后调用tryTerminate尝试结束线程池:
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(SHUTDOWN); // 如果线程池为RUNNING状态, 则切换为SHUTDOWN状态
interruptIdleWorkers(); // 中断所有空闲线程
onShutdown(); // 钩子方法, 由子类实现
} finally {
mainLock.unlock();
}
tryTerminate();
}
这里要注意,如果执行Runnable任务的线程本身不响应中断,那么也就没有办法终止任务
shutdownNow方法的主要不同之处就是,它会将线程池的状态至少置为STOP,同时中断所有工作线程(无论该线程是空闲还是运行中),同时返回任务队列中的所有任务
public List<Runnable> shutdownNow() {
List<Runnable> tasks;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(STOP); // 如果线程池为RUNNING或SHUTDOWN状态, 则切换为STOP状态
interruptWorkers(); // 中断所有工作线程
tasks = drainQueue(); // 抽空任务队列中的所有任务
} finally {
mainLock.unlock();
}
tryTerminate();
return tasks;
}
public static ExecutorService buildExcutor() {
ExecutorService executor= new ThreadPoolExecutor(1, 2, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1),new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
new Exception("执行拒绝策略");
}
}) ;
return executor;
}
//测试类
ExecutorService executor=buildExcutor();
executor.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"执行1任务");
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});