1.ThreadPoolExecutor源码解析
ctl 代表线程池当前状态和线程数量. 32位,前三位代表状态,后29位为线程最大的线程数量
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; } //构造ctl值
private final HashSet workers = new HashSet(); //线程池线程
private final ReentrantLock mainLock = new ReentrantLock(); //在add to workes的时候用的全局锁
构造方法: 默认handler是AbortPolicy,ThreadFactory默认是DefaultThreadFactory
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
将一个Runnable任务扔进线程池的关键代码
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) { //小于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) //corePoolSize=0
addWorker(null, false);
}
else if (!addWorker(command, false)) //如果队列满了,创建新线程也失败
reject(command); // 实施拒绝策略
}
addWorker, 新增worker到线程池内部的workers属性中,并且让当前新增的worker自旋起来,firtstTask执行起来.
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 {
//这两句很重要,new Worker时会生成一个线程,线程内部的任务就是当前创建的Worker,即t就是包含了当前worker的线程,
//当后面执行t.start的时候就会执行Worker的run方法,最终执行当前worker的runWorker方法.
//让线程执行了任务之后,不断的自旋,不懂可以继续看后面的runWorker方法
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock(); // workers.add非线程安全,所以需要锁
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); //添加到workers属性中,即当前线程池所有存活的workers
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock(); // 完了后解锁
}
if (workerAdded) {
t.start(); //最终执行了runWorker方法
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
2.Worker
类定义:
private final class Worker
extends AbstractQueuedSynchronizer
implements Runnable --实现了Runnable,继承AQS
属性:
final Thread thread; 在worker上运行的线程
Runnable firstTask; 生成worker时的第一个runnable task.
run方法:
public void run() {
runWorker(this);
}
runWorker方法就是执行firstTask任务,然后自旋,循环从队列中获取待执行的任务执行的过程.
之前一直思考runWorker是何时执行的,可以回到addWorker那段代码的注释中就知道,在add方法创建worker同时就已经开始让worker的run方法执行了.产生了自旋.
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 (task != null || (task = getTask()) != null) { //如果有执行的任务就执行,没有循环就从队列中获取,
//没有的话就结束线程.当设置了allowCoreThreadTimeOut,keepAliveTime时,超时的情况就会
//getTask=null,就会结束线程执行
w.lock(); //执行任务时,锁住当前worker,一个线程只能同时执行一个任务,你懂得
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++; //当前worker执行的线程数+1
w.unlock(); //worker解锁
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
getTask比较重要的一段:,就是判断线程是否需要回收:
//当设置了allowCoreThreadTimeOut = true || 当前线程数 > corePoolSize 就要考虑是否回收线程
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
//当超时后就需要回收, poll方法就是在keepAliveTime时间后还没获取到任务就会返回null,
//不需要判断超时的话就采用take方法,take会阻塞一直一定拿到任务.
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
所以总结一下.当一个Runnable扔进线程后:
上面是Runnable在线程池中的执行,如果是Callable方法.使用submit时.会不一样.继续看代码,先看使用方法方式:
ExecutorService runnableService = Executors.newFixedThreadPool(3);
Future r1 = runnableService.submit(new TestCallable(1));
r1.get() 就能获取到result
那它是怎么实现的呢. submit提交callable时会包装返回一个FutureTask实例.
FutureTask实现了RunnableFuture、Runnable、Future.简而言之,它就是一个可以获取结果的Runnable而已
public Future submit(Callable task) {
if (task == null) throw new NullPointerException();
RunnableFuture ftask = newTaskFor(task);
execute(ftask);
return ftask;
}
protected RunnableFuture newTaskFor(Callable callable) {
return new FutureTask(callable);
}
submit一个Callable任务之后,执行了execute方法,和之前执行Runnable步骤上没有任何差别,但是在执行当前任务时,FutureTask执行的是自己的run方法.下面看代码
3.FutureTask
重要属性
/** The underlying callable; nulled out after running */ callable方法
private Callable callable;
/** The result to return or exception to throw from get() */ //callable方法返回值
private Object outcome; // non-volatile, protected by state reads/writes
/** The thread running the callable; CASed during run() */
private volatile Thread runner; // 执行的线程
/** Treiber stack of waiting threads */
private volatile WaitNode waiters; //get方法阻塞队列
run方法源码如下.跟runnable实例方法不同的在于会保存任务执行后的结果.
public void run() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable c = callable; //拿到callble实例
if (c != null && state == NEW) {
V result;
boolean ran;
try {
result = c.call(); //执行callable的call方法,并返回result
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)
set(result); //将当前task设置成已完成状态,并且将result设置到outcome属性,并唤起线程.
FutureTask的get方法会返回这个result
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
get方法源码如下:
如果当前task状态是未完成,也就是result还没计算出来,就有人来get了.它会将线程阻塞起来,直到状态已完成会被唤醒,然后获取到result返回.
public V get() throws InterruptedException, ExecutionException {
int s = state;
if (s <= COMPLETING)
s = awaitDone(false, 0L);
return report(s);
}
get方法里面的awaitDone阻塞线程,将需要唤起的线程串成队列
run方法里面的set唤起线程,依次唤起队列中的等待线程.并将节点=null,以便gc.
这两个方法的代码可以自行观看.