随笔 - 8  文章 - 0 评论 - 0 阅读 - 64
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

1、获得结果和触发计算

获得结果

1 public T get()
2 
3 public T get(long timeOut, Timeunit unit)
4 
5 public T join()
6 
7 public getNow(T valueIfAbsent)

主动触发计算

public boolean complete(T value)

 

2、对计算结果进行处理

计算结果存在依赖关系,这两个线程串行化

1 thenApply()
计算结果存在依赖关系,这两个线程串行化,步骤有异常也可以往下走,根据带的异常参数可以进一步往下走
1 handle()

 

示例代码

复制代码
  1 package com.heima.Thread;
  2 
  3 import java.util.concurrent.*;
  4 
  5 public class CompletableFutureApiDemo {
  6     private static final ExecutorService THREAD_POOL = Executors.newFixedThreadPool(3);
  7 
  8     public static void main(String[] args) {
  9         try {
 10             // public T getNow(T valueIfAbsent)
 11             test_getNow();
 12             // public boolean complete(T value)  是否打断get()方法立即返回括号值
 13             test_complete();
 14             // thenApply() 计算结果存在依赖关系,这两个线程串行化
 15             test_thenApply();
 16             // handle() 计算结果存在依赖关系,这两个线程串行化,步骤有异常也可以往下走,根据带的异常参数可以进一步往下走
 17             test_handle();
 18         } finally {
 19             THREAD_POOL.shutdown();
 20         }
 21     }
 22 
 23     private static void test_handle() {
 24         CompletableFuture.supplyAsync(() -> {
 25             try {
 26                 TimeUnit.SECONDS.sleep(1);
 27             } catch (Exception e) {
 28                 e.printStackTrace();
 29             }
 30             System.out.println("step 1");
 31             return ThreadLocalRandom.current().nextInt();
 32         }, THREAD_POOL).handle((f, e) -> {
 33             int i = 10/0;
 34             System.out.println("step 2, get result " + f);
 35             return f + 2;
 36         }).handle((f, e) -> {
 37             System.out.println("step 3, get result " + f);
 38             return f * 3;
 39         }).whenComplete((v, e) -> {
 40             if (e == null) {
 41                 System.out.println("result is " + v);
 42             }
 43         }).exceptionally(e -> {
 44             e.printStackTrace();
 45             System.out.println(e.getMessage());
 46             return null;
 47         });
 48 
 49 
 50         // CompletableFuture使用默认的ForkJoinPool时,主线程结束时,其他线程也会结束
 51         System.out.println("Thread " + Thread.currentThread().getName() + " process other task.");
 52     }
 53 
 54     private static void test_thenApply() {
 55         CompletableFuture.supplyAsync(() -> {
 56             try {
 57                 TimeUnit.SECONDS.sleep(1);
 58             } catch (Exception e) {
 59                 e.printStackTrace();
 60             }
 61             System.out.println("step 1");
 62             return ThreadLocalRandom.current().nextInt();
 63         }, THREAD_POOL).thenApply(f -> {
 64             System.out.println("step 2, get result " + f);
 65             return f + 2;
 66         }).thenApply(f -> {
 67             System.out.println("step 3, get result " + f);
 68             return f * 3;
 69         }).whenComplete((v, e) -> {
 70             if (e == null) {
 71                 System.out.println("result is " + v);
 72             }
 73         }).exceptionally(e -> {
 74             e.printStackTrace();
 75             System.out.println(e.getMessage());
 76             return null;
 77         });
 78 
 79 
 80         // CompletableFuture使用默认的ForkJoinPool时,主线程结束时,其他线程也会结束
 81         System.out.println("Thread " + Thread.currentThread().getName() + " process other task.");
 82     }
 83 
 84 
 85     private static void test_getNow() {
 86 
 87         CompletableFuture<String> result = CompletableFuture.supplyAsync(() -> {
 88             try {
 89                 TimeUnit.SECONDS.sleep(2);
 90             } catch (Exception e) {
 91                 e.printStackTrace();
 92             }
 93             return "ok";
 94         }, THREAD_POOL);
 95 
 96         System.out.println(result.getNow("not ok"));
 97 
 98     }
 99 
100     private static void test_complete() {
101         CompletableFuture<String> result = CompletableFuture.supplyAsync(() -> {
102             try {
103                 TimeUnit.SECONDS.sleep(2);
104             } catch (Exception e) {
105                 e.printStackTrace();
106             }
107             return "ok";
108         }, THREAD_POOL);
109 
110         try {
111             TimeUnit.SECONDS.sleep(1);
112         } catch (Exception e) {
113             e.printStackTrace();
114         }
115         System.out.println(result.complete("completable ok") + "\t" + result. Join());
116     }
117 }
复制代码

 

3、对计算结果进行消费

 

thenAccept(Consumer action) 任务A执行完执行任务B,B需要A的结果,但是任务B无返回值
 
thenRun(Runnable runnable) 任务A执行完执行B,并且B不需要A的结果

4、对计算速度进行选用

 

5、对计算结果进行合并

posted on   不会java的菜鸟程序员  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示