futreTask实现了 Future<V>与Runnable接口;
今天主要是我想学习与了解一下关于future的使用;
future的作用是异步获取线程的执行结果;看他是怎么实现的;
/**
* @throws CancellationException {@inheritDoc}
*/
public V get() throws InterruptedException, ExecutionException {
1 int s = state;
2 if (s <= COMPLETING)
3 s = awaitDone(false, 0L);
4 return report(s);
}
2判断线程的状态没有完成时进入awaitDone()方法;
完成后,直接report(s);先分析report()方法,源码如下:
@SuppressWarnings("unchecked")
private V report(int s) throws ExecutionException {
5 Object x = outcome;
6 if (s == NORMAL)
7 return (V)x;
8 if (s >= CANCELLED)
9 throw new CancellationException();
10 throw new ExecutionException((Throwable)x);
}
第6行代码判断当状态炒normal时(备注:normal代码线程体正常执行完)时返回outcome;否则,抛出异常;
现在在查看awaitDone()方法,按字面意思可解释为等待完成;
private int awaitDone(boolean timed, long nanos)
throws InterruptedException {
11 final long deadline = timed ? System.nanoTime() + nanos : 0L;
12 WaitNode q = null;
13 boolean queued = false;
14 for (;;) {
15 if (Thread.interrupted()) {
16 removeWaiter(q);
17 throw new InterruptedException();
18 }
19 int s = state;
20 if (s > COMPLETING) {
21 if (q != null)
22 q.thread = null;
23 return s;
24 }
25 else if (s == COMPLETING) // cannot time out yet
26 Thread.yield();
27 else if (q == null)
28 q = new WaitNode();
29 else if (!queued)
30 queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
q.next = waiters, q);
31 else if (timed) {
32 nanos = deadline - System.nanoTime();
33 if (nanos <= 0L) {
34 removeWaiter(q);
35 return state;
36 }
37 LockSupport.parkNanos(this, nanos);
}
38 else
39 LockSupport.park(this);
}
}
第15-18行,如果中断线程,抛出异常;
;第20-24行,如果正常执行完直接返回state s;
第25-26行,如果状态正在完成中,只需通过yield方法,让出cpu资源,等待其变成normal状态;
第29-30将当前线程放在等待线程队列中;
第39行,是阻塞当前线程;
总结:get的作用主要获取得线程的执行结果,根据线程状态state的值来进行判断,如果正在进行中,就继续阻塞;
有几个方法要注意像lockSupport.park();(线程阻塞),创建等待线程队列 UNSAFE.compareAndSwapObject;下次再继续学习