类关系图

FutureTask实现了RunableFuture接口。间接实现了Runable接口和Funture。所以他也是可以丢给线程去执行
构造器
public FutureTask(Callable<V> callable) {if (callable == null)throw new NullPointerException();this.callable = callable;//真正执行的方法this.state = NEW; // ensure visibility of callable 确保callable的可见性}
构造器初始化的时候要传一个Callable,这个是我们程序真正要执行的方法
Callable接口
@FunctionalInterfacepublic interface Callable<V> {V call() throws Exception;}
run()线程调度的方法
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 interruptsint s = state;if (s >= INTERRUPTING)handlePossibleCancellationInterrupt(s);}}
调用.state启动线程后,cpu会去调用run方法。run方法调用call去执行真正的任务
set(V v)保存结果
protected void set(V v) {if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {//cas保证线程安全outcome = v;//保存结果UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state 设置最终状态finishCompletion();//唤醒get的时候阻塞的线程}}
finishCompletion() 唤醒等待结果的线程
private void finishCompletion() {// assert state > COMPLETING;for (WaitNode q; (q = waiters) != null;) {if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) {//casfor (;;) {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 gcq = next;}break;}}done();callable = null; // to reduce footprint}
V get() 获取结果
public V get() throws InterruptedException, ExecutionException {int s = state;if (s <= COMPLETING)//结果还未出来s = awaitDone(false, 0L);//进入等待return report(s);//获取结果}
awaitDone(boolean timed, long nanos)等待执行任务完成
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);//超时等待的方法,到时间就自动唤醒}elseLockSupport.park(this);//等待被唤醒}}
V report(int s)真正获取结果
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);//执行异常}
如果执行异常也会获取并抛出
