Java
Python前端运维数据库
Java
Java
  • 新特性
    • Record
    • Optional
  • 面向对象
    • 面向对象基础
    • 构造方法
    • 继承与多态
    • 接口
    • 修饰符
    • 代码块
    • 接口(Interface)
    • 枚举类
  • IO流
    • IO
      • 字节流
      • 字符流
      • 缓冲流
      • 转换流
      • 操作ZIP
      • File 对象
    • NIO
      • Channel和Buffer
      • 异步文件通道AsynchronousFileChannel
      • Selector
      • Path/Files/Pipe
  • 反射
  • 内存分配
  • 集合
    • 简介
    • List
    • Set
    • Map
    • EnumMap
  • 日期与时间
    • Date和Calendar
    • Java8 新时间 ✨
      • LocalDateTime
      • ZonedDateTime
      • Duration
    • 时间格式化
      • SimpleDateFromat
      • DateTimeFormatter ✨
    • Instant
    • 实践
  • 网络编程
    • IP 地址
    • 网络模型
    • TCP 编程
    • UDP 编程
    • HTTP 编程
  • 加密和安全
  • 并发编程
    • 多线程
    • 线程与进程的区别
    • 线程组和线程优先级
    • 线程池
    • 线程锁
  • 异步任务
    • Future
    • CompletableFuture
      • 开启异步任务
      • 串行任务方法
      • 并行任务方法
      • 任务结束方法
      • 异常处理方法
      • 查看状态方法
      • 设置任务结果方法
  • 执行系统命令
  • Stream 流
    • Stream 流的创建
    • Stream 流串行与并行
    • Stream 流中间操作
    • Stream 流终端操作
  • Lambda 表达式
    • Lambda 表达式简介
    • Lambda 表达式语法
    • 方法引用
  • String
  • StringBuffer
由 GitBook 提供支持
在本页
  • thenRun & thenRunAsync (无返回值)
  • thenAccept & thenAcceptAsync (无返回值)
  • whenComplete & whenCompleteAsync (无返回值)
  • thenApply & thenApplyAsync
  • handle & handleAsync
  • thenCompose & thenComposeAsync (返回新任务)

这有帮助吗?

  1. 异步任务
  2. CompletableFuture

串行任务方法

thenRun & thenRunAsync (无返回值)

  • thenRun:使用主线程或者执行上一步任务的子线程,串行执行任务。无返回值。

  • thenRunAsync:串行执行任务,从公共的commonPool线程中获取一个子线程,执行任务。无返回值。

CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
    System.out.println("执行异步任务");
}).thenRun(() -> {
    System.out.println("执行异步任务完成后的回调");
});
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
    System.out.println("执行异步任务");
}).thenRunAsync(() -> {
    System.out.println("执行异步任务完成后的回调");
});

thenAccept & thenAcceptAsync (无返回值)

  • thenAccept:使用主线程或者上一步任务的线程,串行执行任务。将上一步任务执行的结果作为当前任务方法的参数,执行指定的任务。无返回值。

  • thenAcceptAsync:串行执行任务,将上一步任务执行的结果作为当前任务方法执行时候的参数,然后从公共 commonPool 线程中获取一个子线程,执行任务,无返回值。

CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
    System.out.println("执行异步任务");
    return "异步任务返回值";
}).thenAccept((String s) -> {
    System.out.println("执行异步任务完成后的回调");
});
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
    System.out.println("执行异步任务");
    return "异步任务返回值";
}).thenAcceptAsync((String s) -> {
    System.out.println("执行异步任务完成后的回调");
});

whenComplete & whenCompleteAsync (无返回值)

  • whenComplete:使用主线程或者上一步任务的线程,串行执行任务,将上一步任务执行的结果和异常作为当前任务方法的参数,执行任务。无返回值。

  • whenCompleteAsync:串行执行任务,将上一步任务执行的结果或异常作为当前任务方法的参数,然后从commonPool线程池中获取一个子线程,执行任务,无返回值。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    System.out.println("执行异步任务");
    return "异步任务返回值";
}).`((result, e) -> {
    System.out.println("执行异步任务完成后的回调");
    System.out.println("异步任务返回值:" + result);
});

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    System.out.println("执行异步任务");
    return "异步任务返回值";
}).whenCompleteAsync((result, e) -> {
    System.out.println("执行异步任务完成后的回调");
    System.out.println("异步任务返回值:" + result);
});

thenApply & thenApplyAsync

  • thenApply:使用主线程或者上一步任务的线程,串行执行任务。将上一步任务的返回值作为当前任务方法的入参,执行任务,最终将执行结果返回。

  • thenApplyAsync:串行执行任务,将上一步任务执行的结果作为当前任务执行时的参数,然后从公共commonPool线程池中获取一个子线程,执行任务,并返回执行结果。

CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> {
    System.out.println("执行异步任务");
    return "异步任务返回值";
}).thenApply((String s) -> {
    System.out.println("执行异步任务完成后的回调");
    return true;
});
CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> {
    System.out.println("执行异步任务");
    return "异步任务返回值";
}).thenApplyAsync((String s) -> {
    System.out.println("执行异步任务完成后的回调");
    return true;
});

handle & handleAsync

  • handle:使用主线程或者执行上一步任务的线程,串行执行任务。将上一步任务的返回值和异常作为当前任务方法的参数,执行任务,并返回执行后的结果。

  • handleAsync:串行执行任务,将上一步任务的返回值和异常作为当前任务方法的参数,然后从公共commonPool 线程池中获取一个子线程,执行任务,并返回执行后的结果。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    System.out.println("执行异步任务");
    return "异步任务返回值";
}).handle((result, throwable) -> {
    if (throwable != null) {
        System.out.println("异步任务执行过程中出现异常:" + throwable.getMessage());
        return "hello world";
    }
    return result;
});
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    System.out.println("执行异步任务");
    return "异步任务返回值";
}).handleAsync((result, throwable) -> {
    if (throwable != null) {
        System.out.println("异步任务执行过程中出现异常:" + throwable.getMessage());
        return "hello world";
    }
    return result;
});

thenCompose & thenComposeAsync (返回新任务)

  • thenCompose:使用主线程或者上一步任务的线程,串行执行任务,按照顺序组合两个有依赖关系的任务,将上一步任务的返回值作为当前任务方法的参数,执行任务,并返回一个新任务。

  • thenComposeAsync:串行执行任务,按照顺序组合两个有依赖关系的任务,将上一步任务的返回值作为当前任务方法的参数,然后从公共的commonPool线程池中获取一个子线程,执行任务,并返回一个新任务。

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
    System.out.println("执行异步任务");
    return 1;
}).thenComposeAsync((result) -> {
    System.out.println("执行异步任务完成后的回调");
    return CompletableFuture.supplyAsync(() -> result+1);
}).thenComposeAsync((result) -> {
    System.out.println("执行异步任务完成后的回调");
    return CompletableFuture.supplyAsync(() -> {
        return result + 1;
    });
});

Integer result = future.join();
System.out.println("计算结果:" + result); // 3
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    System.out.println("执行异步任务");
    return "异步任务返回值";
}).thenComposeAsync((result) -> {
    System.out.println("执行异步任务完成后的回调");
    return CompletableFuture.supplyAsync(() -> {
        return result + "组合任务";
    });
});
上一页开启异步任务下一页并行任务方法

最后更新于8个月前

这有帮助吗?