Java マルチスレッディング シリーズ -- 未来を極める、非同期タスクの結果を簡単に取得する

序文

最近、個人的な事情により、Javaマルチスレッドシリーズの更新にあまり力を注ぐことができず、数か月間保留されていました. 読者の皆様にまずお詫び申し上げます.

このシリーズの他の記事では、スレッド間の連携について言及しましたが、分業により、プログラム システムのさまざまなタスクをスレッドで分離し、マシンのパフォーマンスを最大限に活用して、特定のスレッドの使用率とエクスペリエンスを向上させます。プログラム。

詳細については、私の著書 Java Multithreading Foundation -- スレッドのライフサイクルとスレッドのコラボレーションに関する詳細な説明を参照してください。

また、スレッド プール関連の記事で言及されています: プログラム ビルダーとして、私たちはスレッド (グループ) の特性とそれらが実行するタスクに関心があり、スレッド操作に気を取られたくありません。

詳細については、私の作品を参照してください: Java Multithreading Foundation -- Thread Creation and Thread Pool Management

ただし、実際の開発では、タスクの実行結果と呼ばれる、プログラム システムに対するタスクの影響も考慮します

ランナブルの制限

前回の記事では、Runnable インターフェースをコーディングで実装する方法について説明しました。これにより、指定されたスレッド (またはスレッド プール) で実行するための境界を持つ "タスク" が得られます。

インターフェイスを再観察すると、メソッドの戻り値がないことがわかります。

public interface Runnable {
    void run();
}
复制代码

JDK 1.5 より前では、タスクの実行結果を使用するには、クリティカル セクション リソースにアクセスするスレッドを慎重に操作する必要があります。デカップリングに使用回调するのは非常に良い選択です。

ハンズオン デモ -- 過去の記事の知識を確認する

ラムダはスペースを削減するために使用されますが、jdk1.5 より前ではラムダはサポートされていないことに注意してください。

コンピューティング タスクを実行のために他のスレッドに分離し、メイン スレッドに戻って結果を消費する

計算や IO などの時間のかかるタスクを他のスレッドにスローし、メイン スレッドがユーザーの入力を受け入れてフィードバックを処理することを想定して、自分のビジネスに集中させますが、この部分は省略します。

次のようなコードを設計できます。

最適化するにはまだ理不尽なことがたくさんありますが、デモンストレーションには十分です

class Demo {
    static final Object queueLock = new Object();
    static List<Runnable> mainQueue = new ArrayList<>();
    static boolean running = true;

    static final Runnable FINISH = () -> running = false;

    public static void main(String[] args) {
        synchronized (queueLock) {
            mainQueue.add(Demo::onStart);
        }
        while (running) {
            Runnable runnable = null;
            synchronized (queueLock) {
                if (!mainQueue.isEmpty())
                    runnable = mainQueue.remove(0);
            }
            if (runnable != null) {
                runnable.run();
            }
            Thread.yield();
        }
    }

    public static void onStart() {
        //...
    }

    public static void finish() {
        synchronized (queueLock) {
            mainQueue.clear();
            mainQueue.add(FINISH);
        }
    }
}
复制代码

次に、計算スレッドとタスク コールバックをシミュレートします。

interface Callback {
    void onResultCalculated(int result);
}

class CalcThread extends Thread {

    private final Callback callback;

    private final int a;

    private final int b;

    public CalcThread(Callback callback, int a, int b) {
        this.callback = callback;
        this.a = a;
        this.b = b;
    }

    @Override
    public void run() {
        super.run();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        final int result = a + b;
        System.out.println("threadId" + Thread.currentThread().getId() + ",calc result:" + result + ";" + System.currentTimeMillis());

        synchronized (queueLock) {
            mainQueue.add(() -> callback.onResultCalculated(result));
        }
    }
}
复制代码

onStart ビジネスを入力します。

class Demo {
    public static void onStart() {
        System.out.println("threadId" + Thread.currentThread().getId() + ",onStart," + System.currentTimeMillis());

        new CalcThread(result -> {
            System.out.println("threadId" + Thread.currentThread().getId() + ",onResultCalculated:" + result + ";" + System.currentTimeMillis());
            finish();
        }, 200, 300).start();

    }
}
复制代码

レビュー: Runnable を使用するための最適化

在前文我们提到,如果业务仅关注任务的执行,并不过于关心线程本身,则可以利用Runnable:

class Demo {
    static class CalcRunnable implements Runnable {

        private final Callback callback;

        private final int a;

        private final int b;

        public CalcRunnable(Callback callback, int a, int b) {
            this.callback = callback;
            this.a = a;
            this.b = b;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            final int result = a + b;
            System.out.println("threadId" + Thread.currentThread().getId() + ",calc result:" + result + ";" + System.currentTimeMillis());

            synchronized (queueLock) {
                mainQueue.add(() -> callback.onResultCalculated(result));
            }
        }
    }

    public static void onStart() {
        System.out.println("threadId" + Thread.currentThread().getId() + ",onStart," + System.currentTimeMillis());

        new Thread(new CalcRunnable(result -> {
            System.out.println("threadId" + Thread.currentThread().getId() + ",onResultCalculated:" + result + ";" + System.currentTimeMillis());
            finish();
        }, 200, 300)).start();

    }
}
复制代码

不难想象出:我们非常需要

  • 让特定线程、特定类型的线程方便地接收任务,回顾本系列文章中的 线程池篇 ,线程池是应运而生
  • 拥有比Synchronize更轻量的机制
  • 拥有更方便的数据结构

至此,我们可以体会到:JDK1.5之前,因为JDK的功能不足,Java程序对于线程的使用 较为粗糙

为异步而生的Future

终于在JDK1.5中,迎来了新特性: Future 以及先前文章中提到的线程池, 时光荏苒,一晃将近20年了

/**
 * 略
 * @since 1.5
 * @author Doug Lea
 * @param <V> The result type returned by this Future's {@code get} method
 */
public interface Future<V> {

    boolean cancel(boolean mayInterruptIfRunning);

    boolean isCancelled();

    boolean isDone();

    V get() throws InterruptedException, ExecutionException;

    V get(long timeout, TimeUnit unit)
            throws InterruptedException, ExecutionException, TimeoutException;
}
复制代码

尽管已经移除了API注释,但仍然能够理解每个API的含义,不多做赘述。

显而易见,为了增加返回值,没有必要用如此复杂的 接口来替代 Runnable。简单思考后可以对返回值的情况进行归纳:

  • 返回Runnable中业务的结果,例如计算、读取资源等
  • 单纯的在Runnable执行完毕后返回一个结果

从业务层上看,仅需要如下接口即可,它增加了返回值、并可以更友好地让使用者处理异常:

作者按:抛开底层实现,仅看业务方编码需要

public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     * 防盗戳 leobert-lan https://juejin.cn/user/2066737589654327
     */
    V call() throws Exception;
}
复制代码

显然,JDK需要提供后向兼容能力:

  • 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;
    }
}
复制代码

而Future恰如其名,它代表了在 "未来" 的一个结果和状态,为了更方便地处理异步而生。

并且内置了 FutureTask,在 FutureTask详解 章节中再行展开。

类图

在JDK1.8的基础上,看一下精简的类图结构:

FutureDiagram.png FutureDiagram.png

FutureTask详解

构造函数

public class FutureTask {
    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
    }
}
复制代码

生命周期

public class FutureTask {
    //新建
    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;
}
复制代码

可能的生命周期转换如下:

  • NEW -> COMPLETING -> NORMAL
  • NEW -> COMPLETING -> EXCEPTIONAL
  • NEW -> CANCELLED
  • NEW -> INTERRUPTING -> INTERRUPTED

JDK中原汁原味的解释如下:

The run state of this task, initially NEW. The run state transitions to a terminal state only in methods set, setException, and cancel. During completion, state may take on transient values of COMPLETING (while outcome is being set) or INTERRUPTING (only while interrupting the runner to satisfy a cancel(true)). Transitions from these intermediate to final states use cheaper ordered/lazy writes because values are unique and cannot be further modified.

核心方法

本节从以下三块入手阅读源码

  • 状态判断
  • 取消
  • 获取结果

状态判断API的实现非常简单

public class FutureTask {
    public boolean isCancelled() {
        return state >= CANCELLED;
    }

    public boolean isDone() {
        return state != NEW;
    }
}
复制代码

取消:

  1. 当前状态为 NEW 且 CAS修改 state 成功,否则返回取消失败
  2. 如果 mayInterruptIfRunning 则中断在执行的线程并CAS修改state为INTERRUPTED
  3. 调用 finishCompletion
    1. 删除并通知所有等待的线程
    2. 调用done()
    3. 设置callable为null
public class FutureTask {
    public boolean cancel(boolean mayInterruptIfRunning) {
        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;
    }

    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
    }
}
复制代码

获取结果: 先判断状态,如果未进入到 COMPLETING(即为NEW状态),则阻塞等待状态改变,返回结果或抛出异常

public class FutureTask {
    public V get() throws InterruptedException, ExecutionException {
        int s = state;
        if (s <= COMPLETING)
            s = awaitDone(false, 0L);
        return report(s);
    }

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

    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);
    }
}
复制代码

如何使用

而使用则非常简单,也非常的朴素。

我们以文中的的例子进行改造:

  1. 沿用原Runnable逻辑
  2. 移除回调,增加 CalcResult
  3. CalcResult 对象作为既定返回结果,Runnable中设置其属性
class Demo {
   static class CalcResult {
      public int result;
   }
   public static void onStart() {
      System.out.println("threadId" + Thread.currentThread().getId() + ",onStart," + System.currentTimeMillis());

      final CalcResult calcResult = new CalcResult();
      Future<CalcResult> resultFuture = Executors.newSingleThreadExecutor().submit(() -> {
         try {
            Thread.sleep(10);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
         final int result = 200 + 300;
         System.out.println("threadId" + Thread.currentThread().getId() + ",calc result:" + result + ";" + System.currentTimeMillis());
         calcResult.result = result;
      }, calcResult);

      System.out.println("threadId" + Thread.currentThread().getId() + "反正干点什么," + System.currentTimeMillis());
      if (resultFuture.isDone()) {
         try {
            final int ret = resultFuture.get().result;
            System.out.println("threadId" + Thread.currentThread().getId() + ",get result:" + ret + ";" + System.currentTimeMillis());
         } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
         }
      }
      finish();
   }
}
复制代码

如果直接使用新特性Callback,则如下:

直接返回结果,当然也可以直接返回Integer,不再包裹一层

class Demo {
   public static void onStart() {
      System.out.println("threadId" + Thread.currentThread().getId() + ",onStart," + System.currentTimeMillis());

      ExecutorService executor = Executors.newSingleThreadExecutor();
      Future<CalcResult> resultFuture = executor.submit(() -> {
         try {
            Thread.sleep(10);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
         final int result = 200 + 300;
         System.out.println("threadId" + Thread.currentThread().getId() + ",calc result:" + result + ";" + System.currentTimeMillis());
         final CalcResult calcResult = new CalcResult();
         calcResult.result = result;
         return calcResult;
      });

      System.out.println("threadId" + Thread.currentThread().getId() + "反正干点什么," + System.currentTimeMillis());
      if (resultFuture.isDone()) {
         try {
            final int ret = resultFuture.get().result;
            System.out.println("threadId" + Thread.currentThread().getId() + ",get result:" + ret + ";" + System.currentTimeMillis());
         } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
         }
      }
      executor.shutdown();
      finish();
   }
}
复制代码

相信读者诸君会有这样的疑惑:

为何使用Future比原先的回调看起来粗糙?

首先要明确一点:文中前段的回调Demo,虽然达成了既定目标,但效率并不高!!在当时计算很昂贵的背景下,并不会如此莽撞地使用!

而在JDK1.5开始,提供了大量内容支持多线程开发。考虑到篇幅,会在系列文章中逐步展开。

另外,FutureTask中的CAS与Happens-Before本篇中亦不做展开。

接下来,再做一些引申,简单看一看多线程业务模式。

引申,多线程业务模式

常用的多线程设计模式包括:

  • Future模式
  • Master-Worker模式
  • Guarded Suspension模式
  • 不变模式
  • 生产者-消费

Future模式

文中对于Future的使用方式遵循了Future模式。

业务方在使用时,已经明确了任务被分离到其他线程执行时有等待期,在此期间,可以干点别的事情,不必浪费系统资源。

Master-Worker模式

在程序系统中设计两类线程,并相互协作:

  • Master线程(单个)
  • Worker线程

Master线程负责接受任务、分配任务、接收(必要时进一步组合)结果并返回;

Worker线程负责处理子任务,当子任务处理完成后,向Master线程返回结果;

作者按:此时可再次回想一下文章开头的Demo

Guarded Suspension模式

  1. 使用缓存队列,使得 服务线程/服务进程 在未就绪、忙碌时能够延迟处理请求。
  2. 使用等待-通知机制,将消费 服务的返回结果 的方式规范化

不变模式

並行開発のプロセスでは、データの一貫性と正確性を確保するために、オブジェクトを同期する必要があり、同期操作によりプログラム システムのパフォーマンスが大幅に低下します。

したがって、状態が不変のオブジェクトを使用し、それらの不変性に依存し、同期メカニズムがない場合一貫性と正確性が維持されるようにします。

  1. オブジェクトが作成された後、その内部状態とデータは変更されません
  2. オブジェクトは複数のスレッドによって共有され、アクセスされます

生産者と消費者

いくつかのプロデューサー スレッドといくつかのコンシューマー スレッドの 2 種類のスレッドを設計します。

プロデューサー スレッドはユーザー リクエストの送信を担当し、コンシューマー スレッドはユーザー リクエストの処理を担当します。プロデューサとコンシューマの間の通信は、共有メモリ バッファを介して行われます。

メモリ バッファの意味:

  • 解決策は、複数のスレッド間でデータを共有することです
  • プロデューサーとコンシューマーの間のパフォーマンスの低下を軽減する

これらのモードは、さまざまな観点から特定の問題を解決しますが、特定の類似点もあり、これ以上拡張することはありません。

あとがき

この時点で終わりに近づいており、JDK1.5 では、マルチスレッドのサポートが突発的な事態を招いています。この記事と一連の記事のスレッド プールに関する内容は、基礎の基礎にすぎません. まだまだ掘り下げる価値のある内容がたくさんあります. この記事ではこれ以上掘り下げることはしません.

以降の連載では、本記事との関連性が高い、AQS、HAPPENS-BEFOREなど、CompleteFutureTask、JUCツールなどを展開していきます。

おすすめ

転載: juejin.im/post/7147552484213719076