一、基础信息
//标记线程池状态,同时记录线程个数,默认running,然后线程个数0,高三位用来记录线程池状态,低29位存放线程个数
//直接使用位运算来进行计算,32位的情况下能保证操作的原子性
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
//个数掩码
private static final int COUNT_BITS = Integer.SIZE - 3;
//线程最大个数,低29位
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
//主锁
private final ReentrantLock mainLock = new ReentrantLock();
//线程池里面的线程
private final HashSet<Worker> workers = new HashSet<Worker>();
//
private final Condition termination = mainLock.newCondition();
//接受新任务,并且处理任务队列中的数据 对应 111
private static final int RUNNING = -1 << COUNT_BITS;
//拒绝新任务但是能能处理任务队列中的数据 对应 000
private static final int SHUTDOWN = 0 << COUNT_BITS;
//拒绝任务,抛弃阻塞队列任务,中断正在处理的任务 001
private static final int STOP = 1 << COUNT_BITS;
//将要调用terminated方法,线程池和任务队列都为空 010
private static final int TIDYING = 2 << COUNT_BITS;
//terminated方法完后进入该状态 011
private static final int TERMINATED = 3 << COUNT_BITS;
线程池状态的相互转换
//重要参数,工作队列,用于保存等待执行的阻塞队列,
//1、基于数组的有界队列ArrayBlockingQueue
//2、基于链表的无界队列LinkedBlockingQueue
//3、最多只有一个元素的同步队列SynchronousQueue
//4、优先级队列PriorityBlockingQueue
private final BlockingQueue<Runnable> workQueue;
//创建线程的工厂
private volatile ThreadFactory threadFactory;
//拒绝策略
private volatile RejectedExecutionHandler handler;
//最大生存时间
private volatile long keepAliveTime;
//核心线程数量
private volatile int corePoolSize;
//最大线程数量
private volatile int maximumPoolSize;
//默认拒绝策略,可以看到默认的是丢弃任务并且抛弃异常
private static final RejectedExecutionHandler defaultHandler =
new AbortPolicy();
二、构造函数
corePoolSize:核心线程池的大小
maximumPoolSize:最大线程池的大小
keepAliveTime:最大线程空闲时间
unit:时间单位
workQueue:阻塞队列,当核心线程数无空闲时,将新任务放入阻塞队列
threadFactory:线程工厂,自定义线程
handler:拒绝策略,当最大线程数无空闲时,执行拒绝策略
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);
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
//核心线程数不能小于0,最大线程数不能小于1
//最大线程数不能小于核心线程数
//存活时间不能小于0
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;
}
三、execute()
public void execute(Runnable command) {
//不能提交空任务
if (command == null)
throw new NullPointerException();
//获取到我们的线程ctl,因为包含线程状态以及线程的数量
int c = ctl.get();
//判断线程池个数是否小于corePoolSize,小于就会新建一个核心线程
if (workerCountOf(c) < corePoolSize) {
//添加一个Worker
if (addWorker(command, true))
//直接返回了
return;
//添加失败了,更新我们的ctl
c = ctl.get();
}
//如果线程池处于running并且往工作队列里面添加成功
if (isRunning(c) && workQueue.offer(command)) {
//重新检查
/* **
* 为什么需要进行双重检查?
* 因为在多线程下,ctl方法不是线程安全的,可能会出现获取了以后就改变了
* 所以需要判断加完以后的状态,是不是在加的过程中发生了改变
*/
int recheck = ctl.get();
//如果不是running状态并且删除成功,开始使用拒绝策略来执行,进行回滚
if (! isRunning(recheck) && remove(command))
reject(command);
//说明是running状态,同时线程池个数是空的
else if (workerCountOf(recheck) == 0)
addWorker(null, false);//如果阻塞队列里面有任务,但是线程池没有线程去执行,这就是新建个线程去执行队列里面的任务
}
//如果队列是满的,尝试添加一个新的线程进去,
else if (!addWorker(command, false))
//失败的话就会进行拒绝策略
reject(command);
}
当我们提交任务后,会有以下的流程,我们先看文字版,然后对照图片,最后看源码,帮助理解
1、首先检测线程池运行状态,如果不是RUNNING,则直接拒绝,线程池要保证在RUNNING的状态下执行任务。
2、如果workerCount < corePoolSize,则创建并启动一个线程来执行新提交的任务。
3、如果workerCount >= corePoolSize,且线程池内的阻塞队列未满,则将任务添加到该阻塞队列中。
4、如果workerCount >= corePoolSize && workerCount < maximumPoolSize,且线程池内的阻塞队列已满,则创建并启动一个线程来执行新提交的任务。
5、如果workerCount >= maximumPoolSize,并且线程池内的阻塞队列已满, 则根据拒绝策略来处理该任务, 默认的处理方式是直接抛异常
四、addWorker()
/* **
* 主要负责创建新的线程并执行任务
* 1.判断线程池状态
* 2.通过CAS来是ctl+1,也就是线程数+1
* 3.构造一个Worker,会使用ThreadFactory来new Thread
* 4.执行start,执行worker.run方法,然后由线程不断去执行我们的任务
*/
private boolean addWorker(Runnable firstTask, boolean core) {
//CAS更新线程数量
retry:
for (;;) {
//获取到ctl
int c = ctl.get();
//获取到线程池的状态
int rs = runStateOf(c);
/* **
* 1.当前线程池状态为Stop,Tidying,terminated这些状态
* 2.线程池状态为shutdown并且有了第一个任务
* 3.线程池状态为shutdown并且队列为空
* 这些都会返回false,新增失败
*/
//此段代码主要是根据线程池的状态,判断是否要接受新的任务,以及线程池状态为SHUTDOWN的时候,不在接受新的任务,如果任务队列里面还有任务则继续执行,如果状态高于SHUTDOWN则直接返回false
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
//自旋
for (;;) {
//计算线程数量
int wc = workerCountOf(c);
//如果超过了最大,就会创建失败
//根据core判断是核心线程还是非核心,如果说是核心线程,线程数量大于核心线程的数量,如果是非核心线程,线程数量大于最大线程的数量就会返回false
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
//如果CAS线程数量失败,会重新开始
if (compareAndIncrementWorkerCount(c))
break retry;
//进行双重检查
c = ctl.get(); // Re-read ctl
//如果数量改变了,就会重新开始
if (runStateOf(c) != rs)
continue retry;
}
}
//到这里CAS我们的ctl成功
boolean workerStarted = false;
boolean workerAdded = false;
Worker w = null;
try {
//构建一个worker
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
//获取到主锁,为了保证workers同步,因为可能多个线程调用了线程池的execute方法
//避免其他线程干掉线程池
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
//获取状态,避免在获取锁之前调用了shutdown方法
int rs = runStateOf(ctl.get());
//线程是running状态
//如果是SHUTDOWN 并且任务为null,主要是为了如果阻塞队列还有任务,但是线程池中没有线程去执行,如果会创建一个线程
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)
//如果启动工作线程失败 将word从集合中删除
addWorkerFailed(w);
}
return workerStarted;
}
五、Worker.run()
public void run() {
runWorker(this);
}
六、runWorker()
//真正执行的方法,主要就是一个while循环,然后一直去拿任务,同时还有一个前置后置的接口
final void runWorker(Worker w) {
//获取到线程
Thread wt = Thread.currentThread();
//获取到第一个任务
Runnable task = w.firstTask;
w.firstTask = null;
//将state设置成了0,这是可以进行中断了
w.unlock(); // allow interrupts
//任务执行时,钩子函数中是否抛出异常
boolean completedAbruptly = true;
try {
//获取任务的第一个方式:execute或者submit中提交的任务
//获取任务的第二个方式:从阻塞队列中获取任务
while (task != null || (task = getTask()) != null) {
w.lock();
//如果线程状态为stop,必须中断当前线程
//第一个判断:判断当前线程是不是stop状态
//第二个判断:查看线程中断位是否为false
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);
}
}
七、getTask()
/* **
* 主要就是获取一个任务,涉及到线程池的状态判断,
* 然后判断线程是否过期
*/
private Runnable getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// 如果现在是shutdown状态,那么除非queue为空,否则不会返回null
// 如果现在是stop状态,会直接返回null
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
int wc = workerCountOf(c);
//allowCoreThreadTimeOut:是否允许核心线程数超时,默认为false
//判断工作线>核心线程
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
//判断工作线>最大线程
//判断如果工作线>核心线程并且当前线程过期
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;
}
}
}
八、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;
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);
}
}