1、 runAsync 和 supplyAsync方法
CompletableFuture 提供了四個靜態方法來創建一個異步操作。
1 public static CompletableFuture<Void> runAsync(Runnable runnable) 2 public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor) 3 public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) 4 public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)
沒有指定Executor的方法會使用ForkJoinPool.commonPool() 作為它的線程池執行異步代碼。如果指定線程池,則使用指定的線程池運行。以下所有的方法都類同。
- runAsync方法不支持返回值。
- supplyAsync可以支持返回值。
示例
1 //無返回值 2 public static void runAsync() throws Exception { 3 CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { 4 try { 5 TimeUnit.SECONDS.sleep(1); 6 } catch (InterruptedException e) { 7 } 8 System.out.println("run end ..."); 9 }); 10 11 future.get(); 12 } 13 14 //有返回值 15 public static void supplyAsync() throws Exception { 16 CompletableFuture<Long> future = CompletableFuture.supplyAsync(() -> { 17 try { 18 TimeUnit.SECONDS.sleep(1); 19 } catch (InterruptedException e) { 20 } 21 System.out.println("run end ..."); 22 return System.currentTimeMillis(); 23 }); 24 25 long time = future.get(); 26 System.out.println("time = "+time); 27 }
2、計算結果完成時的回調方法
當CompletableFuture的計算結果完成,或者拋出異常的時候,可以執行特定的Action。主要是下面的方法:
1 public CompletableFuture<T> whenComplete(BiConsumer<? super T,? super Throwable> action) 2 public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action) 3 public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor) 4 public CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn)
可以看到Action的類型是BiConsumer<? super T,? super Throwable>它可以處理正常的計算結果,或者異常情況。
whenComplete 和 whenCompleteAsync 的區別:
whenComplete:是執行當前任務的線程執行繼續執行 whenComplete 的任務。
whenCompleteAsync:是執行把 whenCompleteAsync 這個任務繼續提交給線程池來進行執行。
示例
1 public static void whenComplete() throws Exception { 2 CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { 3 try { 4 TimeUnit.SECONDS.sleep(1); 5 } catch (InterruptedException e) { 6 } 7 if(new Random().nextInt()%2>=0) { 8 int i = 12/0; 9 } 10 System.out.println("run end ..."); 11 }); 12
13 future.whenComplete(new BiConsumer<Void, Throwable>() { 14 @Override 15 public void accept(Void t, Throwable action) { 16 System.out.println("執行完成!"); 17 } 18
19 }); 20 future.exceptionally(new Function<Throwable, Void>() { 21 @Override 22 public Void apply(Throwable t) { 23 System.out.println("執行失敗!"+t.getMessage()); 24 return null; 25 } 26 }); 27
28 TimeUnit.SECONDS.sleep(2); 29 }
3、 thenApply 方法
當一個線程依賴另一個線程時,可以使用 thenApply 方法來把這兩個線程串行化。
1 public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn) 2 public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn) 3 public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
Function<? super T,? extends U>
T:上一個任務返回結果的類型
U:當前任務的返回值類型
示例
1 private static void thenApply() throws Exception { 2 CompletableFuture<Long> future = CompletableFuture.supplyAsync(new Supplier<Long>() { 3 @Override 4 public Long get() { 5 long result = new Random().nextInt(100); 6 System.out.println("result1="+result); 7 return result; 8 } 9 }).thenApply(new Function<Long, Long>() { 10 @Override 11 public Long apply(Long t) { 12 long result = t*5; 13 System.out.println("result2="+result); 14 return result; 15 } 16 }); 17
18 long result = future.get(); 19 System.out.println(result); 20 }
4、 handle 方法
handle 是執行任務完成時對結果的處理。
handle 方法和 thenApply 方法處理方式基本一樣。不同的是 handle 是在任務完成后再執行,還可以處理異常的任務。thenApply 只可以執行正常的任務,任務出現異常則不執行 thenApply 方法。
1 public <U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn); 2 public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn); 3 public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn,Executor executor);
示例
1 public static void handle() throws Exception{ 2 CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() { 3
4 @Override 5 public Integer get() { 6 int i= 10/0; 7 return new Random().nextInt(10); 8 } 9 }).handle(new BiFunction<Integer, Throwable, Integer>() { 10 @Override 11 public Integer apply(Integer param, Throwable throwable) { 12 int result = -1; 13 if(throwable==null){ 14 result = param * 2; 15 }else{ 16 System.out.println(throwable.getMessage()); 17 } 18 return result; 19 } 20 }); 21 System.out.println(future.get()); 22 }
從示例中可以看出,在 handle 中可以根據任務是否有異常來進行做相應的后續處理操作。而 thenApply 方法,如果上個任務出現錯誤,則不會執行 thenApply 方法。
5、 thenAccept 消費處理結果
接收任務的處理結果,並消費處理,無返回結果。
1 public CompletionStage<Void> thenAccept(Consumer<? super T> action); 2 public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action); 3 public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor);
示例
1 public static void thenAccept() throws Exception{ 2 CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() { 3 @Override 4 public Integer get() { 5 return new Random().nextInt(10); 6 } 7 }).thenAccept(integer -> { 8 System.out.println(integer); 9 }); 10 future.get(); 11 }
從示例代碼中可以看出,該方法只是消費執行完成的任務,並可以根據上面的任務返回的結果進行處理。並沒有后續的輸錯操作。
6、thenRun 方法
跟 thenAccept 方法不一樣的是,不關心任務的處理結果。只要上面的任務執行完成,就開始執行 thenAccept 。
public CompletionStage<Void> thenRun(Runnable action); public CompletionStage<Void> thenRunAsync(Runnable action); public CompletionStage<Void> thenRunAsync(Runnable action,Executor executor);
示例
public static void thenRun() throws Exception{ CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() { @Override public Integer get() { return new Random().nextInt(10); } }).thenRun(() -> { System.out.println("thenRun ..."); }); future.get(); }
該方法同 thenAccept 方法類似。不同的是上個任務處理完成后,並不會把計算的結果傳給 thenRun 方法。只是處理玩任務后,執行 thenAccept 的后續操作。
7、thenCombine 合並任務
thenCombine 會把 兩個 CompletionStage 的任務都執行完成后,把兩個任務的結果一塊交給 thenCombine 來處理。
1 public <U,V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn); 2 public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn); 3 public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn,Executor executor);
示例
1 private static void thenCombine() throws Exception { 2 CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() { 3 @Override 4 public String get() { 5 return "hello"; 6 } 7 }); 8 CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() { 9 @Override 10 public String get() { 11 return "hello"; 12 } 13 }); 14 CompletableFuture<String> result = future1.thenCombine(future2, new BiFunction<String, String, String>() { 15 @Override 16 public String apply(String t, String u) { 17 return t+" "+u; 18 } 19 }); 20 System.out.println(result.get()); 21 }
8、thenAcceptBoth
當兩個CompletionStage都執行完成后,把結果一塊交給thenAcceptBoth來進行消耗
1 public <U> CompletionStage<Void> thenAcceptBoth(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action); 2 public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action); 3 public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action, Executor executor);
示例
1 private static void thenAcceptBoth() throws Exception { 2 CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() { 3 @Override 4 public Integer get() { 5 int t = new Random().nextInt(3); 6 try { 7 TimeUnit.SECONDS.sleep(t); 8 } catch (InterruptedException e) { 9 e.printStackTrace(); 10 } 11 System.out.println("f1="+t); 12 return t; 13 } 14 }); 15
16 CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() { 17 @Override 18 public Integer get() { 19 int t = new Random().nextInt(3); 20 try { 21 TimeUnit.SECONDS.sleep(t); 22 } catch (InterruptedException e) { 23 e.printStackTrace(); 24 } 25 System.out.println("f2="+t); 26 return t; 27 } 28 }); 29 f1.thenAcceptBoth(f2, new BiConsumer<Integer, Integer>() { 30 @Override 31 public void accept(Integer t, Integer u) { 32 System.out.println("f1="+t+";f2="+u+";"); 33 } 34 }); 35 }
9、applyToEither 方法
兩個CompletionStage,誰執行返回的結果快,我就用那個CompletionStage的結果進行下一步的轉化操作。
1 public <U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other,Function<? super T, U> fn); 2 public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn); 3 public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn,Executor executor);
示例
1 private static void applyToEither() throws Exception { 2 CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() { 3 @Override 4 public Integer get() { 5 int t = new Random().nextInt(3); 6 try { 7 TimeUnit.SECONDS.sleep(t); 8 } catch (InterruptedException e) { 9 e.printStackTrace(); 10 } 11 System.out.println("f1="+t); 12 return t; 13 } 14 }); 15 CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() { 16 @Override 17 public Integer get() { 18 int t = new Random().nextInt(3); 19 try { 20 TimeUnit.SECONDS.sleep(t); 21 } catch (InterruptedException e) { 22 e.printStackTrace(); 23 } 24 System.out.println("f2="+t); 25 return t; 26 } 27 }); 28
29 CompletableFuture<Integer> result = f1.applyToEither(f2, new Function<Integer, Integer>() { 30 @Override 31 public Integer apply(Integer t) { 32 System.out.println(t); 33 return t * 2; 34 } 35 }); 36
37 System.out.println(result.get()); 38 }
10、acceptEither 方法
兩個CompletionStage,誰執行返回的結果快,我就用那個CompletionStage的結果進行下一步的消耗操作。
1 public CompletionStage<Void> acceptEither(CompletionStage<? extends T> other,Consumer<? super T> action); 2 public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other,Consumer<? super T> action); 3 public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other,Consumer<? super T> action,Executor executor);
示例
private static void acceptEither() throws Exception { CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() { @Override public Integer get() { int t = new Random().nextInt(3); try { TimeUnit.SECONDS.sleep(t); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("f1="+t); return t; } }); CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() { @Override public Integer get() { int t = new Random().nextInt(3); try { TimeUnit.SECONDS.sleep(t); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("f2="+t); return t; } });
f1.acceptEither(f2, new Consumer<Integer>() { @Override public void accept(Integer t) { System.out.println(t); } }); }
11、runAfterEither 方法
兩個CompletionStage,任何一個完成了都會執行下一步的操作(Runnable)
1 public CompletionStage<Void> runAfterEither(CompletionStage<?> other,Runnable action); 2 public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action); 3 public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action,Executor executor);
示例
1 private static void runAfterEither() throws Exception { 2 CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() { 3 @Override 4 public Integer get() { 5 int t = new Random().nextInt(3); 6 try { 7 TimeUnit.SECONDS.sleep(t); 8 } catch (InterruptedException e) { 9 e.printStackTrace(); 10 } 11 System.out.println("f1="+t); 12 return t; 13 } 14 }); 15
16 CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() { 17 @Override 18 public Integer get() { 19 int t = new Random().nextInt(3); 20 try { 21 TimeUnit.SECONDS.sleep(t); 22 } catch (InterruptedException e) { 23 e.printStackTrace(); 24 } 25 System.out.println("f2="+t); 26 return t; 27 } 28 }); 29 f1.runAfterEither(f2, new Runnable() { 30
31 @Override 32 public void run() { 33 System.out.println("上面有一個已經完成了。"); 34 } 35 }); 36 }
12、runAfterBoth
兩個CompletionStage,都完成了計算才會執行下一步的操作(Runnable)
1 public CompletionStage<Void> runAfterBoth(CompletionStage<?> other,Runnable action); 2 public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action); 3 public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action,Executor executor);
示例
1 private static void runAfterBoth() throws Exception { 2 CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() { 3 @Override 4 public Integer get() { 5 int t = new Random().nextInt(3); 6 try { 7 TimeUnit.SECONDS.sleep(t); 8 } catch (InterruptedException e) { 9 e.printStackTrace(); 10 } 11 System.out.println("f1="+t); 12 return t; 13 } 14 }); 15
16 CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() { 17 @Override 18 public Integer get() { 19 int t = new Random().nextInt(3); 20 try { 21 TimeUnit.SECONDS.sleep(t); 22 } catch (InterruptedException e) { 23 e.printStackTrace(); 24 } 25 System.out.println("f2="+t); 26 return t; 27 } 28 }); 29 f1.runAfterBoth(f2, new Runnable() { 30
31 @Override 32 public void run() { 33 System.out.println("上面兩個任務都執行完成了。"); 34 } 35 }); 36 }
13、thenCompose 方法
thenCompose 方法允許你對兩個 CompletionStage 進行流水線操作,第一個操作完成時,將其結果作為參數傳遞給第二個操作。
1 public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn); 2 public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn) ; 3 public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor) ;
示例
1 private static void thenCompose() throws Exception { 2 CompletableFuture<Integer> f = CompletableFuture.supplyAsync(new Supplier<Integer>() { 3 @Override 4 public Integer get() { 5 int t = new Random().nextInt(3); 6 System.out.println("t1="+t); 7 return t; 8 } 9 }).thenCompose(new Function<Integer, CompletionStage<Integer>>() { 10 @Override 11 public CompletionStage<Integer> apply(Integer param) { 12 return CompletableFuture.supplyAsync(new Supplier<Integer>() { 13 @Override 14 public Integer get() { 15 int t = param *2; 16 System.out.println("t2="+t); 17 return t; 18 } 19 }); 20 } 21
22 }); 23 System.out.println("thenCompose result : "+f.get()); 24 }