• CompletableFuture异步编排(两任务组合——其中一个任务执行)


    一、CompletableFuture源码中两任务组合其中一个任务执行的相关方法

    • CompletableFuture源码中两任务组合其中一个任务执行的相关方法
      在这里插入图片描述在这里插入图片描述
      在这里插入图片描述

    二、runAfterEither方法代码示例

    • runAfterEither方法: 两个任务有一个执行完成,不需要获取future的结果,处理任务,也没有返回值。

    • 代码示例

      package com.xz.thread.day1;
      import lombok.SneakyThrows;
      import java.util.concurrent.*;
      /**
       * @description: 两任务组合(一个任务执行)
       *              runAfterEither 两个任务有一个执行完成,不需要获取future的结果,处理任务,也没有返回值。
       * @author: xz
       * @create: 2022-08-23
       */
      public class Test14 {
          /**
           * 定义线程池
           */
          public static ExecutorService service = Executors.newFixedThreadPool(3);
      
          @SneakyThrows
          public static void main(String[] args)  {
              System.out.println("main start ...");
      
              CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
                  System.out.println("开启异步任务1...");
                  int i = 10 / 1;
                  return i;
              }, service);
      
              CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  System.out.println("开启异步任务2...");
                  return "hello world!";
              }, service);
      
              future1.runAfterEitherAsync(future2, () -> {
                  System.out.println("任务3 启动了....");
              }, service);
      
              System.out.println("main end ...");
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
    • 输出结果
      在这里插入图片描述

    • 由上述输出结果可知,任务1执行完成后,任务3不需要等待任务2执行完成,即可启动任务3。但是使用runAfterEitherAsync不能感知任务的返回值,自身也无返回值。

    三、acceptEither方法代码示例

    • acceptEither方法: 两个任务有一个执行完成,获取它的返回值,处理任务,没有新的返回值。

    • 代码示例

      package com.xz.thread.day1;
      import lombok.SneakyThrows;
      import java.util.concurrent.*;
      /**
       * @description: 两任务组合(一个任务执行)
       *              acceptEither 两个任务有一个执行完成,获取它的返回值,处理任务,没有新的返回值。
       * @author: xz
       * @create: 2022-08-23
       */
      public class Test15 {
          /**
           * 定义线程池
           */
          public static ExecutorService service = Executors.newFixedThreadPool(5);
      
          @SneakyThrows
          public static void main(String[] args) {
              System.out.println("main start ...");
      
              CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
                  System.out.println("开启异步任务1...");
                  int i = 10 / 1;
                  return i;
              }, service);
      
              CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  System.out.println("开启异步任务2...");
                  return 20;
              }, service);
      
              future1.acceptEitherAsync(future2, (res) -> {
                  System.out.println("任务3 启动了...., 任务结果是:" + res);
              }, service);
      
              System.out.println("main end ...");
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
    • 输出结果
      在这里插入图片描述

    • 由上述输出结果可知,可以获取任务1的执行结果,但不返回执行结果。

    四、applyToEither方法代码示例

    • applyToEither方法: 两个任务有一个任务执行完成,获取它的返回值,处理任务并有新的返回值。

    • 代码示例

      package com.xz.thread.day1;
      import lombok.SneakyThrows;
      import java.util.concurrent.*;
      /**
       * @description:
       *          ApplyToEither 两个任务有一个任务执行完成,获取它的返回值,处理任务并有新的返回值。
       * @author: xz
       * @create: 2022-08-23
       */
      public class Test16 {
          /**
           * 定义线程池
           */
          public static ExecutorService service = Executors.newFixedThreadPool(3);
      
          @SneakyThrows
          public static void main(String[] args){
              System.out.println("main start ...");
      
              CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
                  System.out.println("开启异步任务1...");
                  int i = 10 / 1;
                  return i;
              }, service);
      
              CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  System.out.println("开启异步任务2...");
                  return 20;
              }, service);
      
              CompletableFuture<String> stringCompletableFuture = future1.applyToEitherAsync(future2, (res) -> {
                  System.out.println("任务3 启动了...., 上个任务结果是:" + res);
                  return "我是任务3的返回值, 上个任务的执行结果是:" + res;
              }, service);
              System.out.println("======"+stringCompletableFuture.get());
      
              System.out.println("main end ...");
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
    • 输出结果
      在这里插入图片描述

    • 由上述输出结果可知,可以获取任务1的执行结果,并且返回执行结果。

  • 相关阅读:
    Flask 学习-44.Flask-RESTX 请求参数校验reqparse.RequestParser()
    借助PLC-Recorder,汇川中型PLC(AM、AC系列,CODESYS平台)2ms高速采集的方法
    Android Gradle三种自定义插件方式详解(含报错解决方案)
    红黑树及其相关操作(一遍包会)
    使用docker部署springboot项目并连接上mysql数据库
    如何安全传输存储用户密码?(程序员必备)
    qt完善登录框
    PDFBOX和ASPOSE.PDF
    数据结构 - AVL树
    【进阶】Spring中的注解与反射
  • 原文地址:https://blog.csdn.net/li1325169021/article/details/126494929