JUC - FutureTask.md

651

JUC 系列之 FutureTask

以下介绍了一种 FutureTask 的用法:

Callable<String> callable = new Callable<String>() {
    @Override
    public String call() throws Exception {
        return "Response";
    }
};
FutureTask<String> futureTask = new FutureTask<>(callable);
Thread th = new Thread(futureTask);
th.start();
String s = futureTask.get();
System.out.println(s);

来看看 FutureTask 的源码,先来类图:

image20210728195728164.png

可以看到它是包装了 Runnable 与 Future 对象 。

先来看构造方法:

    public FutureTask(Callable<V> callable) {
        if (callable == null)
            throw new NullPointerException();
        this.callable = callable;
        this.state = NEW;       // ensure visibility of callable
    }

    public FutureTask(Runnable runnable, V result) {
        this.callable = Executors.callable(runnable, result);
        this.state = NEW;       // ensure visibility of callable
    }

可以看到最终都需要一个 Callable 对象,如果传入 runnable 的话就会调用 Executors.callable 自动生成一个:

// Executors#callable()
public static <T> Callable<T> callable(Runnable task, T result) {
    if (task == null)
        throw new NullPointerException();
    return new RunnableAdapter<T>(task, result);
}

而 RunnableAdapter 就是 将 Runnable 包装成 Callable 的包装类,其中 call 方法的返回值需要直接给定:

private static final class RunnableAdapter<T> implements Callable<T> {
    private final Runnable task;
    private final T result;
    RunnableAdapter(Runnable task, T result) {
        this.task = task;
        this.result = result;
    }
    public T call() {
        task.run();
        return result;
    }
    public String toString() {
        return super.toString() + "[Wrapped task = " + task + "]";
    }
}

回到构造方法,首先需要把 Callable 对象赋值,然后将 当前状态赋值为 NEW,

这里的 state 表示该任务当前状态,为了在各个线程中都有可见性,添加了 volatile ,源码中给出可能的状态如下:

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;

注释中给出可能的状态转换链如下:

/**
 * Possible state transitions:
 * NEW -> COMPLETING -> NORMAL
 * NEW -> COMPLETING -> EXCEPTIONAL
 * NEW -> CANCELLED
 * NEW -> INTERRUPTING -> INTERRUPTED
 */

这里 FutureTask 对象实现了 Runnable 接口,可以直接放到 Thread 里,而新线程启动的时候会执行 run 方法,因此这里先看 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 {
                    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);
        }
    }

这里用 CAS 操作将 RUNNER 句柄设置为当前线程,保证运行安全。

然后是执行 callable 中的 call 方法,执行成功则调用 set(result) 方法分发结果,否则调用 setException(ex) 方法分发异常。最终如果任务被中断了,还需要调用 handlePossibleCancellationInterrupt(s)

先来看看 分发结果:

protected void set(V v) {
    if (STATE.compareAndSet(this, NEW, COMPLETING)) {
        outcome = v;
        STATE.setRelease(this, NORMAL); // final state
        finishCompletion();
    }
}

首先使用 CAS 操作将当前状态从 NEW 更新到 COMPLETIONG,这步保证了结果只能进行一次分发,并且分发的时候当前状态需要为 NEW 才能继续 。

然后将 结果 赋值给 outcome,此时调用 get 方法可以获取该结果(除此之外 get 带有阻塞等其他操作,之后会分析)。然后将当前状态设置为 NORMAL,这里采用了 setRelease 除了保证内存可见性,也是保证了不会有之前的操作指令重排到之后,保证 除非之后有显式修改 STATE ,否则之后 STATE 都为 NORMAL,然后调用 finishCompletion 方法,该方法为唤醒等待队列中的线程,之后在分析。

来到 setException 方法:

    protected void setException(Throwable t) {
        if (STATE.compareAndSet(this, NEW, COMPLETING)) {
            outcome = t;
            STATE.setRelease(this, EXCEPTIONAL); // final state
            finishCompletion();
        }
    }

与分发结果差不多,不过最终状态变为 EXCEPTIONAL ,并且 outcome 将赋值为异常。

private void handlePossibleCancellationInterrupt(int s) {
        // It is possible for our interrupter to stall before getting a
        // chance to interrupt us.  Let's spin-wait patiently.
        if (s == INTERRUPTING)
            while (state == INTERRUPTING)
                Thread.yield(); // wait out pending interrupt

        // assert state == INTERRUPTED;

        // We want to clear any interrupt we may have received from
        // cancel(true).  However, it is permissible to use interrupts
        // as an independent mechanism for a task to communicate with
        // its caller, and there is no way to clear only the
        // cancellation interrupt.
        //
        // Thread.interrupted();
    }

该方法可以看成是,当 state 为 INTERRUPTIONG 时,线程一直会忙等,这里调用 yield 为放弃当前 cpu,等待 CPU 重新分配资源 。

至此, run 方法分析完了,来看 get 方法:

   /**
     * @throws CancellationException {@inheritDoc}
     */
    public V get() throws InterruptedException, ExecutionException {
        int s = state;
        if (s <= COMPLETING)
            s = awaitDone(false, 0L);
        return report(s);
    }

    /**
     * @throws CancellationException {@inheritDoc}
     */
    public V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException {
        if (unit == null)
            throw new NullPointerException();
        int s = state;
        if (s <= COMPLETING &&
            (s = awaitDone(true, unit.toNanos(timeout))) <= COMPLETING)
            throw new TimeoutException();
        return report(s);
    }

可以看到,无论是有没有超时时间的 get,都是先调用 awaitDone 方法等待结果,而 awaitDone 方法第一个参数为是否有超时时间 。然后调用 report 方法获取结果。

来到这里:

    private int awaitDone(boolean timed, long nanos)
        throws InterruptedException {
        // The code below is very delicate, to achieve these goals:
        // - call nanoTime exactly once for each call to park
        // - if nanos <= 0L, return promptly without allocation or nanoTime
        // - if nanos == Long.MIN_VALUE, don't underflow
        // - if nanos == Long.MAX_VALUE, and nanoTime is non-monotonic
        //   and we suffer a spurious wakeup, we will do no worse than
        //   to park-spin for a while
        long startTime = 0L;    // Special value 0L means not yet parked
        WaitNode q = null;
        boolean queued = false;
        for (;;) {
            int s = state;
            if (s > COMPLETING) {
                if (q != null)
                    q.thread = null;
                return s;
            }
            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();
            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);
            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 before parking
                if (state < COMPLETING)
                    LockSupport.parkNanos(this, parkNanos);
            }
            else
                LockSupport.park(this);
        }
    }

代码有点长,不过主题是一个死循环和一堆 if,主要是维护了一个等待链队列,这个队列结点为 WaitNode

   static final class WaitNode {
        volatile Thread thread;
        volatile WaitNode next;
        WaitNode() { thread = Thread.currentThread(); }
    }

比较简单,就是 next 域与 当前线程。

回到 awaitDone,主要是判断当前 status

如果当前 status > COMPLETING 即已经完成,包括中断状态,只要此时有 outcome 就行,此时如果当前线程加入了等待队列,则将结点的 thread 设置为 null,结点不用删除,之后会处理。

然后返回当前 status 。

否则,如果 status = completing,则表示当前正好正在执行 set 方法第一行代码之后,这里直接调用 Thread.yield();让出 CPU,等再次获取资源之后交给下一个循环处理。

否则,如果当前线程中断标志位为 true,则从等待队列中移除当前结点,然后抛出中断异常 。来到 。

否则,如果 q == null ,则还没创建当前等待节点,则直接实例化一个 WaitNode,然后进入下次自旋,如果下次自旋依然未完成,则会加入等待队列中。这里顺便做了一次超时判断,如果超时则直接返回。

否则,如果 queue 为 false,则当前结点还没加入等待队列,则使用 CAS 操作将结点加入等待队列,注意看这里的 CAS 操作:

queued = WAITERS.weakCompareAndSet(this, q.next = waiters, q);

这是使用 CAS 操作的头插法,返回是否更新成功 。

否则,如果设置了超时操作,则进行超时判断,这里逻辑还是比较简单,如果需要挂起则调用 LockSupport.parkNanos(this, parkNanos); 挂起。

否则,直接挂起别商量 。

来看看 removeWaiter:

    private void removeWaiter(WaitNode node) {
        if (node != null) {
            node.thread = null;
            retry:
            for (;;) {          // restart on removeWaiter race
                for (WaitNode pred = null, q = waiters, s; q != null; q = s) {
                    s = q.next;
                    if (q.thread != null)
                        pred = q;
                    else if (pred != null) {
                        pred.next = s;
                        if (pred.thread == null) // check for race
                            continue retry;
                    }
                    else if (!WAITERS.compareAndSet(this, q, s))
                        continue retry;
                }
                break;
            }
        }
    }

先将结点的 thread 赋值为 null 然后遍历删除所有 thread 为 null 的结点。

最后来看看 report 方法:

    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);
    }

判断状态抛异常或者返回结果。

最后来看看刚刚跳过的 finishComplete 方法:

    /**
     * Removes and signals all waiting threads, invokes done(), and
     * nulls out callable.
     */
    private void finishCompletion() {
        // assert state > COMPLETING;
        for (WaitNode q; (q = waiters) != null;) {
            if (WAITERS.weakCompareAndSet(this, 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
    }

首先是将头结点取出并通过 CAS 操作将头节点设置为 null,保证只有一个线程可以来到循环中,然后遍历所有结点将其唤醒。

最后会调用一边 done 方法,该方法为一个空实现,由子类实现,主要是一个完成钩子 。