一. ThreadPoolExecutor源码改造(简洁版)
为了便于理解分析,对ThreadPoolExecutor源码进行改造,这里没有考虑线程池状态转换对运行带来的影响,只为了解线程池的运行流程,改造后的代码如下:
public class ThreadPoolExecutorCustomDemo {
private static final HashSet<Worker> workers = new HashSet<Worker>();
private static BlockingQueue<Runnable> workQueue = null;
private static int largestPoolSize;
private static volatile int corePoolSize;
private static volatile int maximumPoolSize;
private static long completedTaskCount;
private static volatile long keepAliveTime;
private static volatile boolean allowCoreThreadTimeOut;
private static volatile ThreadFactory threadFactory;
private static AtomicInteger workerCount;
public static void init() {
corePoolSize = 3;
maximumPoolSize = 10;
keepAliveTime = TimeUnit.SECONDS.toNanos(60);
workQueue = new LinkedBlockingQueue<Runnable>(50);
threadFactory = Executors.defaultThreadFactory();
workerCount = new AtomicInteger(0);
allowCoreThreadTimeOut = false;
largestPoolSize = 0;
completedTaskCount = 0;
}
private static final class Worker implements Runnable{
final Thread thread;
Runnable firstTask;
volatile long completedTasks;
Worker(Runnable firstTask) {
this.firstTask = firstTask;
this.thread = threadFactory.newThread(this);
}
@Override
public void run() {
runWorker(this);
}
}
private static Runnable getTask() {
boolean timedOut = false;
for (;;) {
boolean timed = false;
for (;;) {
int wc = workerCount.get();
//允许核心线程超时 或 当前工作线程数量大于核心线程数
timed = allowCoreThreadTimeOut || wc > corePoolSize;
//如果当前工作线程数不大于线程池最大线程数量 并且 timedOut和timed不全为true时,跳出for循环
if (wc <= maximumPoolSize && ! (timedOut && timed))
break;
//如果执行当前工作线程数量减一操作成功,则返回null
if ((wc = workerCount.decrementAndGet()) > 0)
return null;
}
if(workQueue.isEmpty())
return null;
try {
Runnable r = timed ?
//poll取任务,keepAliveTime时间内取不到,则返回null
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
static final void runWorker(Worker w) {
Runnable task = w.firstTask;
w.firstTask = null;
try {
while(task != null || (task = getTask()) != null) {
try {
task.run();
} catch (RuntimeException x) {
throw x;
} finally {
task = null;
w.completedTasks++;
}
}
} finally {
completedTaskCount += w.completedTasks;
workers.remove(w);
}
}
private static boolean addWorker(Runnable firstTask, boolean core) {
//当前工作的线程数量
int wc = workerCount.get();
if( wc > (core ? corePoolSize : maximumPoolSize) ){
return false;
}
workerCount.incrementAndGet();
boolean workerStarted = false;
boolean workerAdded = false;
Worker w = null;
try {
final ReentrantLock mainLock = new ReentrantLock();
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
mainLock.lock();
try {
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) {
if(w != null) {
workers.remove(w);
}
workerCount.decrementAndGet();
}
}
return workerStarted;
}
public static void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
int wc = workerCount.get();
// a.
if(wc < corePoolSize) {
if(addWorker(command, true))
return;
}
// b.
if(workQueue.offer(command)) {
if(workerCount.get() == 0) {
addWorker(null, false);
}
} else if(! addWorker(command, false)) { // c.
// d.
new ThreadPoolExecutor.AbortPolicy().rejectedExecution(command, null);
}
}
public static void main(String[] args) {
//初始化
init();
for(int i=0; i<10; i++) {
execute(new Runnable() {
@Override
public void run() {
System.out.println("task run.["+Thread.currentThread().getName()+"]");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
运行结果如下:
task run.[pool-1-thread-1]
task run.[pool-1-thread-2]
task run.[pool-1-thread-3]
task run.[pool-1-thread-1]
task run.[pool-1-thread-3]
task run.[pool-1-thread-2]
task run.[pool-1-thread-1]
task run.[pool-1-thread-2]
task run.[pool-1-thread-3]
task run.[pool-1-thread-1]
二. 运行流程分析
1. execute(Runnable command)执行流程
首先,获取当前线程池工作的线程数量wc(workCount),然后进行判断,若wc < corePoolSize,则调用addWorker(command, true)创建线程执行任务;若wc >= corePoolSize,则执行任务入队workQueue.offer(command)操作,若入队失败,说明此时队列已满,则调用addWorker(command, false)去创建线程执行任务;若上一步addWorker(command, false)执行失败,说明此时线程池中工作线程的数量以及达到maximumPoolSize,因此无法再去创建新的线程去执行任务,从而拒绝任务(线程池拒绝策略有四种:DiscardPolicy、DiscardOldestPolicy、AbortPolicy、CallerRunsPolicy,默认是采用AbortPolicy,不多说了)。
2. addWorker(Runnable firstTask, boolean core)执行流程
首先,判断当前线程池是否允许再创建新的线程,若不允许创建新的线程则返回增加任务失败(其中不允许创建新线程的情况是:a. 当core为true时,且wc > corePoolSize,b. 当core为false时,且wc > maximumPoolSize),否则表示允许,那么接着处理,先把当前线程池工作的线程数量加一,然后创建一个工作者w=new Worker(firstTask)
说到这里,我们先不看addWorker(Runnable firstTask, boolean core)下面流程,首先看一下Worker类:Worker implements Runnable,那么Worker类会实现一个run()方法,另外构造一个Worker对象就是指定一个首先要完成的任务firstTask以及创建一个新的线程thread,这里要注意创建新线程newThread(this)(传入的参数是this,即Worker类对象),那么当这个新线程thread执行start()方法时,执行的就是Worker类的run()方法。
接着看addWorker(Runnable firstTask, boolean core)下面的流程,把刚刚创建的工作者w添加到工作集合workers,若添加失败,则进行回滚(任务从工作集合workers中移除,线程池当前工作的线程数量减一),若添加成功,则调用w.thread.start()启动线程,那么前面已经分析过了,会执行Worker类中的run()方法,因run()里面调用runWorker(this)方法,所以接下来看看runWorker(Worker w)。
3. runWorker(Worker w)执行流程
runWorker的思想是:首先执行firstTask,然后接着调用getTask()获取任务(getTask()是从阻塞队列里面取任务,下面会介绍)继续执行,直到获取不到任务时执行完毕。
其中,执行任务调用task.run(),那么这个run()方法执行的就是任务的run(),就是向execute(Runnable command)里面投放Runnable对象的run()方法。
接下来看看getTask()是如何取任务的
4. getTask()执行流程
首先看看什么情况下调用getTask()是取不到任务的,第一种情况是:如果线程池中当前工作的线程数量大于允许的最大线程数,那么getTask()返回null;第二种情况是:timedOut和timed同时为true时,那么一个一个看,timed什么时候为true? 当《条件1:创建线程池的时候设置allowCoreThreadTimeOut为true》或者《条件2:线程池当前工作的线程数量大于corePoolSize时》两个条件满足至少满足一个的时候timed为true;timedOut什么时候为true?阻塞队列里面取不到任务时(下面会说);第三种情况时:阻塞队列为空时。
接下了,取任务操作,根据timed的值分两种情况:当timed为true时,调用workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS),此方法若在keepAliveTime内在阻塞队列中取不到任务时返回null,此时timedOut设为true;当timed为false时,调用workQueue.take(),这个方法会取到任务的。
线程池的运行过程大致就是这样,另外有一块是线程池的状态维护和线程数量控制,这部分是通过维护一个32位Integer变量来实现的,其中高3位表示线程池的状态(RUNNING、SHUTDOWN、STOP、TIDYING、TERMINATED),低29位表示当前线程池的线程数量,这里就不介绍了,可以查看源码了解... ...