d)执行,这就回答了本文开头提出的第二个问题。
//java.util.concurrent.RunnableFuture
public interface RunnableFuture<V> extends Runnable, Future<V> {
/**
* Sets this Future to the result of its computation
* unless it has been cancelled.
*/
void run();
}
用一张图表示就是:
Callable任务的设置与获取,则都是在FutureTask这个层面上完成,把Callable封装到FutureTask中,而FutureTask implements Runnable,从而转化成ThreadPoolExecutor#execute执行Runnable任务。
Callable任务的执行结果又是怎么获取的?Future.get为什么会阻塞?
java.util.concurrent.FutureTask 的private volatile int state;
变量:
//java.util.concurrent.FutureTask#run
public void run() {
if (state != NEW ||
!RUNNER.compareAndSet(this, null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
//Callable#call执行成功, ran=true
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
//ran=true,才会设置Callable任务的执行结果
if (ran)
set(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);
}
}
set方法设置Callable任务的执行结果时,会修改 FutureTask的 state 实例变量的值!
//java.util.concurrent.FutureTask#set
protected void set(V v) {
if (STATE.compareAndSet(this, NEW, COMPLETING)) {
outcome = v;
STATE.setRelease(this, NORMAL); // final state
finishCompletion();
}
}
而java.util.concurrent.FutureTask#get()
方法,也正是通过检查 state 的值,来确定是否能够拿到Callable任务的执行结果。
//java.util.concurrent.FutureTask#get()
public V get() throws InterruptedException, ExecutionException {
int s = state;
if (s <= COMPLETING)
//如果 state 不是在 NORMAL 状态,FutureTask#get()就会阻塞
//这就是 java.util.concurrent.Future#get() 阻塞的原因
s = awaitDone(false, 0L);//这里面会调用:Thread.yield()、LockSupport.park(this)
return report(s);
}
java.util.concurrent.FutureTask#awaitDone
//java.util.concurrent.FutureTask#awaitDone
private int awaitDone(boolean timed, long nanos)
throws InterruptedException {
WaitNode q = null;
//省略一些无关代码...
for (;;) {//for循环一直检查任务的运行状态....直到可以"结束"
int s = state;
//state的值大于 COMPLETING 说明已经有Callable任务的结果了
//java.util.concurrent.FutureTask#set 设置了Callable任务的结果,修改了state的值
if (s > COMPLETING) {
if (q != null)
q.thread = null;
return s;
}
//COMPLETING 任务的运行状态是:正在执行中
else if (s == COMPLETING)
// We may have already promised (via isDone) that we are done
// so never return empty-handed or throw InterruptedException
Thread.yield();//挂起获取执行结果的线程(这就是Futur#get阻塞的原因)
else if (Thread.interrupted()) {
removeWaiter(q);//任务可能被中断了,当然就不需要等待获取执行结果了
throw new InterruptedException();
}
else if (q == null) {
if (timed && nanos <= 0L)
return s;
q = new WaitNode();
}
else if (!queued)
queued = WAITERS.weakCompareAndSet(this, q.next = waiters, q);
//java.util.concurrent.Future#get(long, java.util.concurrent.TimeUnit)超时阻塞的实现原理
else if (timed) {
final long parkNanos;
if (startTime == 0L) { // first time
startTime = System.nanoTime();
if (startTime == 0L)
startTime = 1L;
parkNanos = nanos;
} else {
long elapsed = System.nanoTime() - startTime;
if (elapsed >= nanos) {
removeWaiter(q);
return state;
}
parkNanos = nanos - elapsed;
}
// nanoTime may be slow; recheck be