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、对计算结果进行消费
4、对计算速度进行选用