戳蓝字「TopCoder」关注咱们哦!node
编者注:FutureTask用于在异步操做场景中,FutureTask做为生产者(执行FutureTask的线程)和消费者(获取FutureTask结果的线程)的桥梁,若是生产者先生产出了数据,那么消费者get时能会直接拿到结果;若是生产者还未产生数据,那么get时会一直阻塞或者超时阻塞,一直到生产者产生数据唤醒阻塞的消费者为止。话很少说,下来开始FutureTask的分析~web
Future接口和实现Future接口的FutureTask,表明异步计算的结果,Future使用示例以下:安全
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10,
60, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
Future future = executor.submit(() -> {
System.out.println("hello world");
return "hello world";
});
System.out.println(future.get());
Future接口声明以下:微信
FutureTask除了实现Future接口外,还实现了Runnable接口。所以,FutureTask能够交给Executor执行,也能够由调用线程直接执行(FutureTask.run())。根据FutureTask.run()方法被执行的时机,FutureTask能够处于如下3种状态:未启动、运行中、已完成。app
当FutureTask处于未启动或已启动状态时,执行FutureTask.get()方法将致使调用线程阻塞;当FutureTask处于已完成状态时,执行FutureTask.get()方法将致使调用线程当即返回结果或抛出异常。异步
当FutureTask处于未启动状态时,执行FutureTask.cancel()方法将致使此任务永远不会被执行;this
当FutureTask处于已启动状态时,执行FutureTask.cancel(true)方法将以中断执行此任务线程的方式来试图中止任务;spa
当FutureTask处于已启动状态时,执行FutureTask.cancel(false)方法将不会对正在执行此任务的线程产生影响(让正在执行的任务运行完成);.net
当FutureTask处于已完成状态时,执行FutureTask.cancel(…)方法将返回false。线程
FutureTask的生命周期以下:
Future.get() 阻塞/唤醒原理
执行future.get()时,若是对应线程还未执行完,则会阻塞当前线程,以FutureTask为例,FutureTask中有一个int型的状态标志,表示future对应线程的运行状态。
/**
* Possible state transitions:
* NEW -> COMPLETING -> NORMAL
* NEW -> COMPLETING -> EXCEPTIONAL
* NEW -> CANCELLED
* NEW -> INTERRUPTING -> INTERRUPTED
*/
private volatile int state;
private static final int NEW = 0;
private static final int COMPLETING = 1;
private static final int NORMAL = 2;
private static final int EXCEPTIONAL = 3;
private static final int CANCELLED = 4;
private static final int INTERRUPTING = 5;
private static final int INTERRUPTED = 6;
当调用FutureTask.get()时,若是Future对应的任务已完成(正常执行完成或者抛出异常),执行返回;若是Future对应的任务未执行完成,则会将当前线程封装成一个NodeWait,以CAS方式添加到FutureTask.waiters
链表上(单向链表,新节点都会做为head node添加上),而后会阻塞当前线程(包括超时阻塞)。
public V get() throws InterruptedException, ExecutionException {
int s = state;
if (s <= COMPLETING) // 线程未执行完成
s = awaitDone(false, 0L);
return report(s);
}
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(); // future task已完成,正在赋值outcome,get()返回的值就是outcome,这时不用加入WaitNode便可
else if (q == null)
q = new WaitNode(); // 生成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);
}
}
private V report(int s) throws ExecutionException {
Object x = outcome;
if (s == NORMAL) // 正常执行结束
return (V)x;
if (s >= CANCELLED) // 已取消
throw new CancellationException();
throw new ExecutionException((Throwable)x); // 抛出异常
}
在任务执行(run()方法)中,调用result = callable.call
方法,正常执行完毕后调用set(result)设置Future结果;出现异常则调用setException(ex)。最后会调用finishCompletion()来唤醒阻塞在Future的全部线程。
设置完数据以后(无论是正常数据仍是对应异常),当等待数据的线程来get
时,就会返回或者直接给它抛异常;若是当线程已经get过并阻塞在这里时,FutureTask须要将这些线程唤醒起来。
public void run() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
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);
}
}
protected void set(V v) {
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
outcome = v;
UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
finishCompletion();
}
}
protected void setException(Throwable t) {
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
outcome = t;
UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final state
finishCompletion();
}
}
// 唤醒全部等待线程
private void finishCompletion() {
// assert state > COMPLETING;
for (WaitNode q; (q = waiters) != null;) {
if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) {
for (;;) {
Thread t = q.thread;
if (t != null) {
q.thread = null;
LockSupport.unpark(t);
}
WaitNode next = q.next;
if (next == null)
break;
q.next = null; // unlink to help gc
q = next;
}
break;
}
}
done();
callable = null; // to reduce footprint
}
小结
FutureTask中的waiters是一个单向链表,若是多个线程阻塞在该Future上,最新阻塞的线程排列在链表前面,唤醒线程时依次从前到后遍历链表唤醒线程,这样处理貌似对最开始阻塞在Future上的线程不太公平哈,由于最开始阻塞的线程是到最后才被唤醒的。
推荐阅读
本文分享自微信公众号 - TopCoder(gh_12e4a74a5c9c)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。