一、说明
ThreadPoolExecutor
- 继承
Executor 接口 - 它有多个构造方法来实现自定义创建线程池,以内部线程池的形式对外提供管理任务执行,线程调度,线程池管理等
- 关闭线程池调用
shutdown() 、shutdownNow() 、awaitTermination() 方法
二、理解
shutdown()
- 只关闭了提交通道,停止接收新任务,已提交的任务会继续执行直到完成,此方法不会阻塞,当所有提交任务执行完毕,线程池被关闭
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(SHUTDOWN);
interruptIdleWorkers();
onShutdown();
} finally {
mainLock.unlock();
}
tryTerminate();
}
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;
}
awaitTermination()
- 此方法阻塞,在
shutdown() 调用之后,等待所有已提交的任务完成或者超时,在这之后可以继续提交任务 - 如果
ExecutorService 在超时之前已经关闭,返回true,即所有任务执行完毕;否则返回false,即已超时 - 如果不在之前调用
shutdown() ,即使在超时之前所有任务执行完成,ExecutorService 仍未终止,awaitTermination()依然在等待终止状态,返回false
public boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
while (!runStateAtLeast(ctl.get(), TERMINATED)) {
if (nanos <= 0L)
return false;
nanos = termination.awaitNanos(nanos);
}
return true;
} finally {
mainLock.unlock();
}
}
线程状态转换关系
runState
- 线程池的生命周期
- 线程池的状态(runState)和工作线程数量(workerCount)共同保存在 AtomicInteger 类型的控制变量 ctl 中
- AtomicInteger 类对 int 类型进行了封装,可以对整数进行各种原子操作
- ctl高三位保存运行状态(23=8>5),低29位保存工作线程的数量(229-1)
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;
private static int runStateOf(int c) { return c & ~CAPACITY; }
private static int workerCountOf(int c) { return c & CAPACITY; }
private static int ctlOf(int rs, int wc) { return rs | wc; }
RUNNING 接收新的任务,并且可执行队列里的任务SHUTDOWN 停止接收新任务,但可执行队列里的任务STOP 可执行队列里的任务,不执行队列里的任务,中断正在执行的任务TIDYING 所有任务都已终止,线程数为0,当线程池变为TIDYING状态时,会执行钩子函数terminated(),钩子方法是指使用一个抽象类实现接口, 一个抽象类实现这个接口,需要的方法设置为abstract,其它设置为空方法TERMINATED 终止状态,表示线程池已关闭,已经执行完terminated()钩子方法
判断当前线程池运行状态
private static boolean runStateLessThan(int c, int s) {
return c < s;
}
private static boolean runStateAtLeast(int c, int s) {
return c >= s;
}
private static boolean isRunning(int c) {
return c < SHUTDOWN;
}
public boolean isShutdown() {
return ! isRunning(ctl.get());
}
public boolean isTerminating() {
int c = ctl.get();
return ! isRunning(c) && runStateLessThan(c, TERMINATED);
}
public boolean isTerminated() {
return runStateAtLeast(ctl.get(), TERMINATED);
}
运行状态转换关系
三、实现
1.shutdown()
创建一个ShutdownTest 类,默认使用ThreadPoolExecutor.AbortPolicy 拒绝策略,队列是ArrayBlockingQueue ,设置核心线程数最大值为1,线程池线程数最大值为2,最大等待时间为5秒,等待队列值为2,提交8个任务,在第5个任务的时候执行 shutdown()
public class ShutdownTest {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2, 5,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(2),
Executors.defaultThreadFactory());
for (int i = 1; i <= 8; i++) {
System.out.println("执行第"+i+"个任务");
threadPoolExecutor.execute(new runnable("任务"+i));
Iterator iterator = threadPoolExecutor.getQueue().iterator();
System.out.print("当前等待队列 ");
while (iterator.hasNext()){
runnable thread = (runnable) iterator.next();
System.out.print(thread.name + "\t");
}
System.out.print("\n");
System.out.println("--------");
if (i == 4) {
threadPoolExecutor.shutdown();
System.out.println("线程池已关闭");
}
}
}
static class runnable implements Runnable{
String name;
public runnable(String setName) {
this.name = setName;
}
@Override
public void run() {
try {
System.out.println("线程:"+Thread.currentThread().getName() +" 执行: "+name);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
执行 shutdown() ,此时停止接收新任务,已提交的任务会继续执行直到完成,此方法不会阻塞,抛出RejectedExecutionException
如果捕获RejectedExecutionException ,可以看到任务被拒绝了
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2, 5,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(2),
Executors.defaultThreadFactory());
for (int i = 1; i <= 8; i++) {
System.out.println("执行第"+i+"个任务");
try {
threadPoolExecutor.execute(new runnable("任务"+i));
Iterator iterator = threadPoolExecutor.getQueue().iterator();
System.out.print("当前等待队列 ");
while (iterator.hasNext()){
runnable thread = (runnable) iterator.next();
System.out.print(thread.name + "\t");
}
System.out.print("\n");
System.out.println("--------");
} catch (RejectedExecutionException e) {
System.out.println("拒绝执行第" + i + "个任务");
}
if (i == 4) {
threadPoolExecutor.shutdown();
System.out.println("线程池已关闭");
}
}
}
2.shutdownNow()
立即停止线程池,停止接收新任务,中断所有正在执行的任务,停止对等待队列的处理
if (i == 4) {
threadPoolExecutor.shutdownNow();
System.out.println("线程池已关闭");
}
3.awaitTermination()
此方法阻塞,在shutdown() 调用之后,等待所有已提交的任务完成或者超时,在这之后可以继续提交任务
如果 shutdown 在 awaitTermination后调用的话,在awaitTermination 未超时前,它不会释放锁;而 shutdown 也无法得到锁去让线程池停止。这就形成了死锁
|