FutureTask 能够接收 Callable 类型的参数,用来处理有返回结果的情况。
// 创建任务对象
FutureTask<Integer> task = new FutureTask<>(() -> {
log.debug("running");
Thread.sleep(1000);
return 200;
});
new Thread(task).start();
// 主线程阻塞,同步等待 task 执行完毕的结果
Integer value = task.get();
System.out.println("value = " + value);
private Callable callable;
private Object outcome;
private Runnable target;
for (;;)
造成当前线程阻塞,直到 call 方法执行结束可以获取到 outcome 的值,并将 outcome 作为 get() 方法返回值。Thread 和 FutureTask 类均实现了 Runnable 接口并重写了其 run 方法,Thread 将 FutureTask 进行聚合赋给 private Runnable target
。
@FunctionalInterface
public interface Callable<V> {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
V call() throws Exception;
}
public interface RunnableFuture<V> extends Runnable, Future<V> {
/**
* Sets this Future to the result of its computation
* unless it has been cancelled.
*/
void run();
}
public class FutureTask<V> implements RunnableFuture<V> {
/** The underlying callable; nulled out after running */
private Callable<V> callable;
// 存储 callable 接口的 call 方法的返回值
/** The result to return or exception to throw from get() */
private Object outcome; // non-volatile, protected by state reads/writes
/*
() -> {
log.debug("running");
Thread.sleep(1000);
return 200;
}
这实际上是对函数式接口 callable 的 V call() 方法进行实现
*/
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}
public void run() {
// ...
Callable<V> c = callable;
// 重写了 Runnable 函数式接口的 run 方法
result = c.call();
// ...
// 赋值
set(result);
// ...
}
protected void set(V v) {
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
// 将 callable 的 call 方法返回值,即我们自定义的 200 赋给 outcome
outcome = v;
UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
finishCompletion();
}
}
// 获取 callable 的 call 方法的返回结果
public V get() throws InterruptedException, ExecutionException {
int s = state;
if (s <= COMPLETING)
// 获取到结果成功的标识,实际是在 awaitDone 方法中用了死循环不断判断是否生成返回结果,造成了线程阻塞
s = awaitDone(false, 0L);
// 获取结果
return report(s);
}
// timed-是否计时等待,即是否设置等待超时,false表示不设置,true表示设置
private int awaitDone(boolean timed, long nanos)
throws InterruptedException {
final long deadline = timed ? System.nanoTime() + nanos : 0L;
WaitNode q = null;
boolean queued = false;
// 死循环
for (;;) {
if (Thread.interrupted()) {
removeWaiter(q);
throw new InterruptedException();
}
int s = state;
if (s > COMPLETING) {
if (q != null)
q.thread = null;
return s;
}
else if (s == COMPLETING) // cannot time out yet
Thread.yield();
else if (q == null)
q = new WaitNode();
else if (!queued)
queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
q.next = waiters, q);
else if (timed) {
nanos = deadline - System.nanoTime();
if (nanos <= 0L) {
removeWaiter(q);
return state;
}
LockSupport.parkNanos(this, nanos);
}
else
LockSupport.park(this);
}
}
}
public class Thread implements Runnable {
/* What will be run. */
private Runnable target;
// 构造器,将间接实现了 Runnable 接口的 FutureTask 对象传进来
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
private void init(ThreadGroup g, Runnable target, String name, long stackSize) {
init(g, target, name, stackSize, null, true);
}
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals) {
// ...
// 将 FutureTask 对象赋给 Thread 对象的属性 target
this.target = target;
}
@Override
public void run() {
if (target != null) {
// 实际调用的 FutureTask 对象重写的 run 方法,重写的 run 方法中又会调用 callable 接口的 call 方法,并将 call 方法的返回值赋给 FutureTask 对象的属性 outcome
target.run();
}
}
}