前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >FutureTask 原理剖析

FutureTask 原理剖析

作者头像
luoxn28
发布2020-11-05 12:33:37
8370
发布2020-11-05 12:33:37
举报
文章被收录于专栏:TopCoderTopCoder

戳蓝字「TopCoder」关注我们哦!

编者注:FutureTask用于在异步操作场景中,FutureTask作为生产者(执行FutureTask的线程)和消费者(获取FutureTask结果的线程)的桥梁,如果生产者先生产出了数据,那么消费者get时能会直接拿到结果;如果生产者还未产生数据,那么get时会一直阻塞或者超时阻塞,一直到生产者产生数据唤醒阻塞的消费者为止。话不多说,下来开始FutureTask的分析~

Future接口和实现Future接口的FutureTask,代表异步计算的结果,Future使用示例如下:

代码语言:javascript
复制
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种状态:未启动、运行中、已完成

当FutureTask处于未启动或已启动状态时,执行FutureTask.get()方法将导致调用线程阻塞;当FutureTask处于已完成状态时,执行FutureTask.get()方法将导致调用线程立即返回结果或抛出异常。

  • 当FutureTask处于未启动状态时,执行FutureTask.cancel()方法将导致此任务永远不会被执行;
  • 当FutureTask处于已启动状态时,执行FutureTask.cancel(true)方法将以中断执行此任务线程的方式来试图停止任务;
  • 当FutureTask处于已启动状态时,执行FutureTask.cancel(false)方法将不会对正在执行此任务的线程产生影响(让正在执行的任务运行完成);
  • 当FutureTask处于已完成状态时,执行FutureTask.cancel(…)方法将返回false。

FutureTask的生命周期如下:

Future.get() 阻塞/唤醒原理

执行future.get()时,如果对应线程还未执行完,则会阻塞当前线程,以FutureTask为例,FutureTask中有一个int型的状态标志,表示future对应线程的运行状态。

代码语言:javascript
复制
/**
 * 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添加上),然后会阻塞当前线程(包括超时阻塞)。

代码语言:javascript
复制
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需要将这些线程唤醒起来。

代码语言:javascript
复制
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上的线程不太公平哈,因为最开始阻塞的线程是到最后才被唤醒的

推荐阅读

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-10-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 TopCoder 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Future.get() 阻塞/唤醒原理
  • 小结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档