CompletableFuture-对计算结果进行消费

红太狼 2024-02-28 04:46 150阅读 0赞

979c534d5ff74307bfec139bbfdd75eb.png

  1. package com.nanjing.gulimall.zhouyimo.test;
  2. import java.util.concurrent.*;
  3. /**
  4. * @author zhou
  5. */
  6. public class CompletableFutureAPI2Demo {
  7. public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
  8. ExecutorService threadPool = Executors.newFixedThreadPool(3);
  9. CompletableFuture.supplyAsync(() -> {
  10. return 1;
  11. }, threadPool).thenApply(f -> {
  12. return f + 2;
  13. }).thenApply(f -> {
  14. return f + 3;
  15. }).thenAccept(r -> {
  16. System.out.println(r);//6
  17. });
  18. System.out.println(Thread.currentThread().getName() + "------主线程先去做其他事情");
  19. threadPool.shutdown();
  20. }
  21. }
  22. 6
  23. main------主线程先去做其他事情

发表评论

表情:
评论列表 (有 0 条评论,150人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Matlab:函数调用结果进行索引

    Matlab:对函数调用结果进行索引 在Matlab中,函数调用结果可以是一个向量、矩阵或多维数组。当我们需要访问特定元素或子集时,可以使用索引操作。索引允许我们按照位置来访