ThreadPoolExecutor
线程池执行器,内部主要封装了一个线程池和拒绝策略,重点看看它默认的线程工厂和拒绝策略。
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
defaultThreadFactory
这是一个内部的静态类
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);// 设置优先级为5
return t;
}
}
defaultHandler
jdk自带了四大拒绝策略,AbortPolicy是默认的拒绝策略,直接抛出异常。
public static class AbortPolicy implements RejectedExecutionHandler {
/**
* Creates an {@code AbortPolicy}.
*/
public AbortPolicy() { }
/**
* Always throws RejectedExecutionException.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
* @throws RejectedExecutionException always
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());// 直接抛出异常,并使用默认的提示信息
}
}
其他拒绝策略
CallerRunsPolicy
如果触发了拒绝策略机制,将多余的线程交给caller线程执行,即创建这个线程池执行器的线程执行多余的任务。
public static class CallerRunsPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code CallerRunsPolicy}.
*/
public CallerRunsPolicy() { }
/**
* Executes task r in the caller's thread, unless the executor
* has been shut down, in which case the task is discarded.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {// 先判断线程池执行器是否关闭
r.run();
}
}
}
DiscardPolicy
啥都不做,默默丢弃多余的任务,这种不建议使用,没有留下任何信息。
public static class DiscardPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code DiscardPolicy}.
*/
public DiscardPolicy() { }
/**
* Does nothing, which has the effect of discarding task r.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
DiscardOldestPolicy
抛弃最老的线程任务,执行新的任务。
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code DiscardOldestPolicy} for the given executor.
*/
public DiscardOldestPolicy() { }
/**
* Obtains and ignores the next task that the executor
* would otherwise execute, if one is immediately available,
* and then retries execution of task r, unless the executor
* is shut down, in which case task r is instead discarded.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();// 丢弃最老的任务
e.execute(r);// 执行当前最新任务
}
}
}
execute(Runnable command)
线程池执行器最核心的方法就是实现了接口的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();
}
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);// 如果达到最大线程数,执行拒绝策略
}
addWorker(Runnable firstTask, boolean core)
添加一个任务给线程池执行。任务可以来自于外部调用(firstTask != null),也可以来自内部队列(firstTask == null)。
先判断条件,
private boolean addWorker(Runnable firstTask, boolean core) {
retry: // doug lee骚操作
for (;;) {
int c = ctl.get(); // 得到线程池状态的一个快照
int rs = runStateOf(c);
// Check if queue empty only if necessary.
// 这个if总结来说就是,线程池处于关闭状态时,不继续接收外部任务,但是会继续接收内部任务。
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN && //线程池状态为shutdown说明不接收新任务但是要执行完线程池内部任务
firstTask == null && // 判断是否是外部任务,这里只接受内部任务。如果使用submit(task, core)进来的就是外部任务,这样的话task不可能为null
! workQueue.isEmpty()))// 任务队列为空
return false; // 返回添加执行新任务失败
for (;;) {
int wc = workerCountOf(c);// 判断当前正在工作的线程数量是否满足要求
if (wc >= CAPACITY ||// 工作线程不允许大于最大允许的线程容量
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
if (compareAndIncrementWorkerCount(c))
break retry;// 添加任务成功,线程数+1,跳出这个大的for循环继续向下执行
c = ctl.get(); // Re-read ctl
if (runStateOf(c) != rs)
continue retry;// 如果上述compareAndIncrementWorkerCount(c)失败了,会再走一次,因为c的值改变了,重新走一次就行
// 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;
}
runWorker(Worker w)
创建线程并启动线程后,执行任务。
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循环开始可能执行自己execut的任务,后面会自动去队列拿任务,除非钩子方法抛出异常,跳出循环
while (task != null || (task = getTask()) != null) {// 如果task == null,说明是来自队列的任务,如果队列没有任务,task = getTask())会一直阻塞,因为使用的是blockingQueue。
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);// 钩子方法,执行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);// 钩子方法,执行task任务后执行
}
} finally {
task = null;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;// 走到这里说明上面的while正常结束。只有钩子方法发生异常时才不会走这里,这个时候completedAbruptly=true。所以completedAbruptly表示钩子方法是否发生异常
} finally {
processWorkerExit(w, completedAbruptly);// 任务结束后,分析任务是否正常结束,如果不是正常结束,处理失败的任务
}
}
processWorkerExit(Worker w, boolean completedAbruptly)
对任务结果做处理。保证线程池至少有一个线程存活。
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;
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; // 如果有任务在执行,说明有线程在线程池,返回。如果不进来这里,说明没有线程存活在线程池,走下面的addWorker,新起一个线程给线程池
}
addWorker(null, false);// 如果是因为用户钩子方法引起的线程中断,或者线程池没有线程了,新补充一个线程给线程池
}
}
ctl细节解析
ctl代表了一种状态,初始状态为RUNNING。
Doug lee使用这种位运算存储状态的好处是,只有RUNNING状态小于0,所以ctl < 0就可以快速判断是RUNNING状态。
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
private static int ctlOf(int rs, int wc) { return rs | wc; }
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;// 111 0000...
private static final int SHUTDOWN = 0 << COUNT_BITS;// 000 0000...
private static final int STOP = 1 << COUNT_BITS;// 001 0000...
private static final int TIDYING = 2 << COUNT_BITS;// 010 0000...
private static final int TERMINATED = 3 << COUNT_BITS;// 011 0000....
状态转换过程
以下是常用的线程池中止习惯
try {
ExecutorService esc = Executors.newFixedThreadPool(1);
esc.shutdown();// 拒绝接受新任务
esc.awaitTermination(1, TimeUnit.MINUTES);// 等待一分钟,让剩余线程执行完成退出
} catch (InterruptedException e) {
e.printStackTrace();
}
}