runAsync 和 supplyAsync runAsync接受一個Runable的實現,無返回值 CompletableFuture.runAsync(()->System.out.println("無返回結果的運行")); supplyAsync接受一個Supplier的實現,有返回值 Com ...
runAsync 和 supplyAsync
runAsync接受一個Runable的實現,無返回值
CompletableFuture.runAsync(()->System.out.println("無返回結果的運行"));
supplyAsync接受一個Supplier的實現,有返回值
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("有返回結果的運行");
return 1;
});
獲取結果的get和join
都是堵塞,直到返回結果
get方法拋出是經過處理的異常,ExecutionException或**InterruptedException **,需要用戶手動捕獲
try {
System.out.println(CompletableFuture.supplyAsync(() -> {
System.out.println("有返回結果的運行");
return 1;
}).get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
join方法拋出的就不用捕獲,是經過包裝的**CompletionException **或 CancellationException
System.out.println(CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("有返回結果的運行");
return 1;
}).join());
常用方法
獲取結果的get\join\getNow
get():一直等待
get(timeout,unit):等待,除非超時
getNow(valueIfAbsent):計算完返回計算的結果,未計算完返回預設的結果
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(1);
;
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
});
System.out.println("立即獲取:"+completableFuture.getNow(9999));
try {
TimeUnit.SECONDS.sleep(2);
System.out.println("doing");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("等一會獲取:"+completableFuture.getNow(9999));
join() 同get()
thenApply\handle
執行完前面的,前面返回的結果返回,然後傳給後面再,執行完後面任務,一步一步來。
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("step 1");
return 1;
}).thenApply(a -> {
System.out.println("step 2");
return a + 2;
}).thenApply(a -> {
System.out.println("step 3");
return a + 3;
});
System.out.println(completableFuture.get());
執行結果:
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("step 1");
int a=1/0;
return 1;
}).handle((a,b) -> {
System.out.println("step 2");
if (b!=null) {
System.out.println(b.getMessage());
return 0;
}
return a + 2;
}).handle((a,b) -> {
System.out.println("step 3");
if (b!=null) {
System.out.println(b.getMessage());
return 0;
}
return a + 3;
});
System.out.println(completableFuture.get());
執行結果:
thenApply和handle的區別:
thenApply執行的時候,有異常的則整個執行鏈會中斷,直接拋出異常。
handle有異常也可以往下一步走,根據帶的異常參數可以進一步處理
thenAccept
接收前面任務的返回結果,當前節點處理,並不返回結果。
CompletableFuture.supplyAsync(()->{
System.out.println("step 1");
return 10;
}).thenAccept(a->{
System.out.println("res "+a);
});
applyToEither
在多個任務段同時執行時,哪個任務段用時最少,就返回哪個
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("step 1");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
}).applyToEither(CompletableFuture.supplyAsync(() -> {
System.out.println("step 2");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 2;
}), a -> {
return a;
});
System.out.println(completableFuture.get());
執行結果:
thenCombine
合併多個任務段的返回結果
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("step 1");
return IntStream.range(1, 11).sum();
}).thenCombine(CompletableFuture.supplyAsync(() -> {
System.out.println("step 2");
return IntStream.range(11, 21).sum();
}), (a, b) -> a + b)
.thenCombine(CompletableFuture.supplyAsync(() -> {
System.out.println("step 3");
return IntStream.range(21, 31).sum();
}), (a, b) -> a + b);
System.out.println(completableFuture.get());