当前位置: 首页 > news >正文

异步任务 -- FutureTask

任务提交

之前在分析线程池的时候,提到过 AbstractExecutorService 的实现:

public Future<?> submit(Runnable task) {
    if (task == null) throw new NullPointerException();
    RunnableFuture<Void> ftask = newTaskFor(task, null);
    execute(ftask);
    return ftask;
}

public <T> Future<T> submit(Runnable task, T result) {
    if (task == null) throw new NullPointerException();
    RunnableFuture<T> ftask = newTaskFor(task, result);
    execute(ftask);
    return ftask;
}

public <T> Future<T> submit(Callable<T> task) {
    if (task == null) throw new NullPointerException();
    RunnableFuture<T> ftask = newTaskFor(task);
    execute(ftask);
    return ftask;
}

protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
    return new FutureTask<T>(runnable, value);
}

protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
    return new FutureTask<T>(callable);
}

对于 submit 提交的任务,不管是 Runnable 还是 Callable,最终都会统一为 FutureTask 并传给 execute 方法。

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
}

对于 Runnable 还会创建一个适配器 :

static final class RunnableAdapter<T> implements Callable<T> {
    final Runnable task;
    final T result;
    RunnableAdapter(Runnable task, T result) {
        this.task = task;
        this.result = result;
    }
    public T call() {
        task.run();
        return result;
    }
}

任务状态

FutureTask 有下面几种状态:

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;

初次创建的时候构造器中赋值 state = NEW,后面状态可能有下面几种演化:

  • NEW -> COMPLETING -> NORMAL (正常完成的过程)
  • NEW -> COMPLETING -> EXCEPTIONAL (执行过程中遇到异常)
  • NEW -> CANCELLED (执行前被取消)
  • NEW -> INTERRUPTING -> INTERRUPTED (取消时被中断)

任务执行

当线程池执行任务的时候,最终都会执行 FutureTask 的 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 interrupts
        int s = state;
        if (s >= INTERRUPTING)
            handlePossibleCancellationInterrupt(s);
    }
}

对于 Callable 直接执行其 call 方法。执行成功则调用 set 方法设置结果,如果遇到异常则调用 setException 设置异常:

protected void set(V v) {
    // 首先 CAS 设置 state 为中间状态 COMPLETING
    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();
    }
}

这两个方法都是对全局变量 outcome 的赋值。当我们通过 get 方法获取结果时,往往是在另一个线程:

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 循环来阻塞当前线程
    for (;;) {
        // 响应中断
        if (Thread.interrupted()) {
            removeWaiter(q);
            throw new InterruptedException();
        }

        int s = state;
        // 任务已完成或者已抛出异常  直接返回
        if (s > COMPLETING) {
            // WaitNode已创建此时也没用了
            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);
        }
        else
            LockSupport.park(this);
    }
}

如果任务已完成或者等待任务直到完成后,调用 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);
}

如果 state == NORMAL,标识任务正常完成,返回实际结果。如果 state >= CANCELLED, 则返回 CancellationException,否则返回 ExecutionException,这样在线程池中执行的任务不管是异常还是正常返回了结果,都能被感知。

Treiber Stack

/**
 * Simple linked list nodes to record waiting threads in a Treiber
 * stack.  See other classes such as Phaser and SynchronousQueue
 * for more detailed explanation.
 */
static final class WaitNode {
    volatile Thread thread;
    volatile WaitNode next;
    WaitNode() { thread = Thread.currentThread(); }
}

在 awaitDone 方法中 WaitNode q = null,第一次会创建一个 WaitNode,这时即使有多个线程在等待结果,都会创建各自的 WaitNode:

else if (q == null)
    q = new WaitNode();
else if (!queued)
    queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
                                         q.next = waiters, q);

然后在for循环中会跳到第二个 else if,由于没有入队,这时会通过 CAS 将新建的 WaitNode 类型的 q 赋值给 waiters,这个时候同一时刻只有一个线程能赋值成功,后一个在失败后又经历一次循环,最终成功地将当前 WaitNode 插入到 waiters 的头部。

任务取消

FutureTask 有一个 cancel 方法,包含一个 boolean 类型的参数(在执行中的任务是否可以中断):

public boolean cancel(boolean mayInterruptIfRunning) {
    // 如果任务不是刚创建或者是刚创建但是更改为指定状态失败则返回 false
    if (!(state == NEW &&
          UNSAFE.compareAndSwapInt(this, stateOffset, NEW,
              mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
        return false;
    try {    // in case call to interrupt throws exception
        if (mayInterruptIfRunning) {
            try {
                Thread t = runner;
                if (t != null)
                    t.interrupt();
            } finally { // final state
                UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);
            }
        }
    } finally {
        finishCompletion();
    }
    return true;
}

最终都会调用 finishCompletion() ,在 set 方法和 setException 方法中也调用了这个 finishCompletion 方法:

private void finishCompletion() {
    // assert state > COMPLETING;
    // 如果任务执行完或者存在异常的话  这个waiters已经为null了
    for (WaitNode q; (q = waiters) != null;) {
        // 首先不断尝试把 waiters 设置为 null,如果很多线程调用 task.cancel(),也只有一个能成功
        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
}

当在 finishCompletion 方法中唤醒线程后,被唤醒的线程在 awaitDone 方法中继续循环,发现状态已完成:

int s = state;
// 任务已完成或者已抛出异常  直接返回
if (s > COMPLETING) {
    // WaitNode已创建此时也没用了
    if (q != null)
        q.thread = null;
    return s;
}

接着调用 report 方法,发现状态为异常的话将包装成 ExecutionException((Throwable)x); 这个异常就是我们在使用 get 的时候需要捕获的异常。

最近比较忙,这块东西已经很久没有看了, FutureTask 感觉没有彻底弄明白,也没有一个好的结尾,现在这里标记下,后面继续更新。

转载于:https://www.cnblogs.com/lucare/p/10316808.html

相关文章:

  • (二)学习JVM —— 垃圾回收机制
  • 搭建私有CA和证书认证
  • Linux rpm 命令参数使用详解
  • 智能合约开发环境搭建及Hello World合约
  • zookeeper安装部署
  • java B2B2C Springcloud多租户电子商城系统- 分布式事务
  • Shell 脚本 100 例《四》
  • Powershell 批量重命名
  • 浙江台州警方侦破特大制售假酒案 涉案金额超4000万元
  • 《SQL必知必会》读书笔记
  • Unity C#编程优化——枚举
  • 正则表达式知识点汇总
  • 山西政协委员建言探索农业托管模式 解决“谁来种地”问题
  • 发现操作系统的数据库出现死锁如何处理
  • 微服务架构到底应该如何选择?
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • create-react-app做的留言板
  • MQ框架的比较
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • Terraform入门 - 1. 安装Terraform
  • Web标准制定过程
  • Web设计流程优化:网页效果图设计新思路
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 如何胜任知名企业的商业数据分析师?
  • 数据可视化之 Sankey 桑基图的实现
  • 思维导图—你不知道的JavaScript中卷
  • 用jquery写贪吃蛇
  • 翻译 | The Principles of OOD 面向对象设计原则
  • #我与Java虚拟机的故事#连载02:“小蓝”陪伴的日日夜夜
  • (52)只出现一次的数字III
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (zt)基于Facebook和Flash平台的应用架构解析
  • (ZT)薛涌:谈贫说富
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (五)网络优化与超参数选择--九五小庞
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • (转)ABI是什么
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • ******IT公司面试题汇总+优秀技术博客汇总
  • .bat批处理(三):变量声明、设置、拼接、截取
  • .mkp勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .net core控制台应用程序初识
  • .net 程序 换成 java,NET程序员如何转行为J2EE之java基础上(9)
  • .NET 中什么样的类是可使用 await 异步等待的?
  • .NET:自动将请求参数绑定到ASPX、ASHX和MVC(菜鸟必看)
  • .NET6使用MiniExcel根据数据源横向导出头部标题及数据
  • .NET单元测试
  • :“Failed to access IIS metabase”解决方法
  • [acwing周赛复盘] 第 94 场周赛20230311
  • [BZOJ2281][SDOI2011]黑白棋(K-Nim博弈)
  • [git]git命令如何取消先前的配置
  • [idea]关于idea开发乱码的配置
  • [java]删除数组中的某一个元素