无论是直接还是间接的创建线程池,归根结底都是通过ThreadPoolExecutor来创建线程池并且配置线程池特性的,需要执行新任务时,通过ThreadPoolExecutor的execute方法提交任务,具体如下所示。
//Executors.java
public class Executors {
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
}
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
//ScheduledThreadPoolExecutor extends ThreadPoolExecutor
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
//......
}
//自定义线程池
ThreadPoolExecutor threadPoolExecutor;
threadPoolExecutor = ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,
TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.DiscardOldestPolicy());
//提交新任务
threadPoolExecutor.execute(task);
//新任务
Runnable task = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
Log.e("ThreadPoolExecutor","run task:"+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
那么ThreadPoolExecutor内部是如何提交新任务,如何执行任务的呢?对于这个流程就是我们这里要进行解析的。
ThreadPoolExecutor的构造及参数含义
在进行解析之前,我们先看看ThreadPoolExecutor的构造以及构造中用来配置线程池的参数含义。
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
(1)corePoolSize:线程的核心线程数,默认一直存活,即使处于闲置状态。若将allowCoreThreadTimeOut设置为true,闲置的核心线程在等待新任务时会有超时策略(即受到keepAliveTime限制)。
(2)maximumPoolSize:线程池能容纳的最大线程数。
(3)keepAliveTime:非核心线程闲置时的超时时长,超过这个时间,非核心线程就会被回收。
(4)unit:keepAliveTime的时间单位
(5)workQueue:线程池中的任务队列,通过线程池的execute方法提交的Runnable对象会存储在这个参数中。常用任务队列:
LinkedBlockingQueue(无界队列):基于链表的阻塞队列,按照 FIFO 原则对元素进行排序。
ArrayBlockingQueue(有界队列):初始时需要指定队列大小,基于数组的阻塞队列,按照 FIFO 原则对元素进行排序。
SynchronousQueue(同步队列):不存储元素,每个插入操作必须等待另一个线程调用移除操作,否则插入操作会一直阻塞。
DelayedWorkQueue(有序队列):会通过每个任务按照距离下次执行时间间隔的大小来排序。
PriorityBlockingQueue(优先级队列):具有优先级的阻塞队列。
(6)threadFactory:线程工厂,为线程池提供创建线程的功能。默认情况下,线程池使用Executors.defaultThreadFactory()方法返回的线程工厂实现类。
(7)handler:拒绝策略。当线程池中线程达到或者超过最大线程数并且任务队列已满,这是再提交新任务时,就会拒绝执行此任务。默认是AbortPolicy,抛出异常RejectedExecutionException。
AbortPolicy:丢弃新任务,并抛出 RejectedExecutionException
DiscardPolicy:不做任何操作,直接丢弃新任务
DiscardOldestPolicy:丢弃队列队首的元素,并执行新任务
CallerRunsPolicy:由调用线程执行新任务
任务提交
了解了ThreadPoolExecutor的构造后,我们回到正题。从任务提交的方法execute入手。从源码或者源码的注解可知,任务提交会经过3个步骤,大致如下:
(1)若线程池有效线程数小于核心线程数量(corePoolSize),调用addWorker创建一个核心线程来执行任务。若线程池有效线程数大于等于核心线程数或者创建核心线程失败,则执行步骤(2)。
(2)若线程池处于RUNNING状态,尝试将任务加入任务队列中,若加入队列成功,则尝试进行double-check,若加入失败,则执行步骤(3)。
(3)若线程池不是RUNNING状态或者加入队列失败,尝试创建非核心新线程直到有效线程达到maximumPoolSize,如果失败,则调用reject()方法执行拒绝策略。
//ThreadPoolExecutor.java
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.
*/
//ctl(AtomicInteger原子操作,线程安全) 包含32位数据,高3位存放线程池状态,低29位存线程数
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
//当前线程池中有效的线程数量小于核心线程数(corePoolSize)时,创建一个新的核心线程来执行任务
if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
//线程池处于RUNNING状态,并且将新任务加入任务队列中
int recheck = ctl.get();
//再次检查线程池状态
if (! isRunning(recheck) && remove(command))
//不处于RUNNING,并且成功将新任务从任务队列中移除,执行拒绝策略
reject(command);
else if (workerCountOf(recheck) == 0)
//处于SHUTDOWN,有效线程数为0时,任务队列里可能还有任务没执行的任务,创建新的非核心线程
addWorker(null, false);
}
else if (!addWorker(command, false))
//线程池不是RUNNING状态或者加入队列失败,尝试创建新的非核心线程失败,线程池已经shutdown或者已经饱和了,所以拒绝任务
reject(command);
}
从execute方法中,我们大致了解了一个任务提交,要么直接创建一个线程来处理任务,要么就是放到任务队列里面进行等待处理,要么就是拒绝处理。
创建工作线程,启动线程执行任务
那么是如何创建线程来执行任务的呢?从addWorker方法,可以推出大致流程:
(1)判断线程池状态,是否需要创建工作线程,若需要,则原子性的增加当前有效工作线程数量。
(2)将任务封装成一个Worker对象,Worker实现了Runnable接口,并在构造中通过ThreadFactory创建与Worker对象对应的线程,最后将这个Worker对象添加进workers这个HashSet集合中。
(3)启动Workerd对象中对应的线程,运行Worker中的Run方法,从而间接调用runWorker方法。在runWorker方法中会去调用任务(即在execute中提交的Runnable对象)的run方法,除此之外,当本次任务完成后,线程还会继续从阻塞任务队列中取任务执行,直到阻塞队列为空(即任务全部完成)。
(4)如果工作线程创建失败了,调用addWorkerFailed方法,回滚工作线程的创建,将worker从workers集合中删除,并原子性的减少工作线程数量。
//ThreadPoolExecutor.java
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 (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
for (;;) {
//有效工作线程数
int wc = workerCountOf(c);
//有效工作线程数量大于等于最大容量
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
//递增有效工作线程的数量
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 {
//将任务封装到Worker中
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());
// 如果线程池处于RUNNING状态执行添加任务操作,或线程池处于SHUTDOWN 状态,firstTask 为空(任务队列不为空,需要添加工作线程来执行任务)
if (rs < SHUTDOWN ||
(rs == SHUTDOWN && firstTask == null)) {
if (t.isAlive()) // precheck that t is startable
throw new IllegalThreadStateException();
//将worker添加到workers集合(workers:HashSet<Worker>)
workers.add(w);
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock();
}
if (workerAdded) {
//启动线程,执行任务,这里会调用Worker中的Run方法,从而间接调用runWorker方法
t.start();
workerStarted = true;
}
}
} finally {
//工作线程创建失败
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
封装任务
将初始任务封装到Worker中,并创建与Worker对应的工作线程。
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. */
final Thread thread;//worker持有的工作线程
/** Initial task to run. Possibly null. */
Runnable firstTask;//初始化时的第一个具体任务,可能是null
/** 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(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
//创建线程,并传入将当前实现了Runnable接口的Worker对象
this.thread = getThreadFactory().newThread(this);
}
/** Delegates main run loop to outer runWorker. */
public void run() {
//执行任务
runWorker(this);
}
//......
}
处理任务
工作线程在runWorker方法中处理任务,线程完成本次任务后,继续从阻塞队列中取任务继续执行,直到阻塞队列中任务全部被完成。
//ThreadPoolExecutor.java
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
//任务不为null或者阻塞队列还存在任务
//getTask()从阻塞队列中获取任务
while (task != null || (task = getTask()) != null) {
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线程(当前线程)
wt.interrupt();
try {
//beforeExecute没有具体实现,可以根据需要重写这个方法,在任务执行之前调用
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没有具体实现,可以根据需要重写这个方法,在任务完成之后调用
afterExecute(task, thrown);
}
} finally {
task = null;
//增加worker对应的线程完成的任务数量
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
//工作线程退出
processWorkerExit(w, completedAbruptly);
}
}
从阻塞队列中获取任务。
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.
//当线程池处于STOP及以上状态时,释放该线程。
//当线程处于SHUTDOWN 状态时,并且workQueue请求队列为空,释放该线程。
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;//返回null时,runWorker中会执行processWorkerExit将工作线程退出
}
int wc = workerCountOf(c);
// Are workers subject to culling?
//如果调用allowCoreThreadTimeOut方法设置为true,则所有线程都有超时时间。
//如果当前线程数大于核心线程数则该线程有超时时间。
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;
}
}
}
总结:
(1)ThreadPoolExecutor是线程池的真正实现。
(2)从任务的提交到执行大致流程是:execute提交任务->addWorker创建工作线程(通过Worker分装初始任务以及创建工作线程)->runWorker执行任务(通过getTask从阻塞队列中获取任务,任务完成后好会继续从队列中获取任务,直到阻塞队列为空即任务全部被完成)。
(3)ThreadPoolExecutor执行任务大致遵循的规则:a.如果线程池中的线程数未达到核心线的程数量,则创建一个核心线程来执行任务;b.若线程池中的线程数达到或者超过了核心线程数,阻塞队列未满,这时任务会被插入阻塞队列进行排队等待。c.若b中阻塞队列已满,但是线程池中的线程数未超过线程池规定的最大容量,这时会创建一个非核心线程来执行任务。d.若c中线程数已经达到规定的最大容量,这是若有新任务提交,就会拒绝执行此任务。
参考资料:
https://blog.csdn.net/u014634338/article/details/79098437
https://cloud.tencent.com/developer/article/1109643
《Android 开发艺术探索》