CompletableFuture 類詳解
1.runAsync、supplyAsync、get、join:開啟異步任務(wù),獲取結(jié)果。
// runAsync:沒有返回值
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
System.out.println("Hello");
});
// supplyAsync:有返回值
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
return "World";
});
// join:不強(qiáng)制 try,不能設(shè)置超時(shí)時(shí)間,不能響應(yīng)線程中斷
System.out.println(future2.join());// World
// get:必須 try 或 添加異常簽名,可以設(shè)置超時(shí)時(shí)間,可以響應(yīng)線程中斷
try {
System.out.println(future2.get());// World
} catch (Exception ignored) {
}
2.thenApply、thenAccept、thenRun:處理任務(wù)的結(jié)果。
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");
// thenRun:在任務(wù)完成后執(zhí)行
CompletableFuture<Void> future2 = future.thenRun(() -> System.out.println("完成了"));
// thenAccept:接收任務(wù)的結(jié)果
CompletableFuture<Void> future3 = future.thenAccept(s -> System.out.println(s + " World"));// Hello World
// thenApply:接收任務(wù)的結(jié)果,返回一個(gè)新值
CompletableFuture<String> future4 = future.thenApply(s -> s + " World");
System.out.println("------");
System.out.println(future2.join());// null
System.out.println(future3.join());// null
System.out.println(future4.join());// Hello World
3.thenCombine、thenAcceptBoth、runAfterBoth:處理兩個(gè)任務(wù)的結(jié)果。
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
// runAfterBoth:在兩個(gè)任務(wù)完成后執(zhí)行
CompletableFuture<Void> combinedFuture = future1.runAfterBoth(future2, () -> System.out.println("完成了"));
// thenAcceptBoth:接收兩個(gè)任務(wù)的結(jié)果
CompletableFuture<Void> combinedFuture2 = future1.thenAcceptBoth(future2, (s1, s2) -> System.out.println(s1 + " " + s2));// Hello World
// thenCombine:接收兩個(gè)任務(wù)的結(jié)果,返回一個(gè)新值
CompletableFuture<String> combinedFuture3 = future1.thenCombine(future2, (s1, s2) -> s1 + " " + s2);
System.out.println("------");
System.out.println(combinedFuture.join());// null
System.out.println(combinedFuture2.join());// null
System.out.println(combinedFuture3.join());// Hello World
4.applyToEither、acceptEither、runAfterEither:處理兩個(gè)任務(wù)任一的結(jié)果。
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
// runAfterBoth:在兩個(gè)任務(wù)任一完成后執(zhí)行
CompletableFuture<Void> combinedFuture = future1.runAfterEither(future2, () -> System.out.println("完成了"));
// thenAcceptBoth:接收兩個(gè)任務(wù)的任一結(jié)果
CompletableFuture<Void> combinedFuture2 = future1.acceptEither(future2, (s) -> System.out.println(s));// Hello 或 World
// thenCombine:接收兩個(gè)任務(wù)的任一結(jié)果,返回一個(gè)新值
CompletableFuture<String> combinedFuture3 = future1.applyToEither(future2, (s) -> s);
System.out.println("------");
System.out.println(combinedFuture.join());// null
System.out.println(combinedFuture2.join());// null
System.out.println(combinedFuture3.join());// Hello 或 World
5.exceptionally、whenComplete、handle:處理異常。
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
if(Math.random() > 0.5){
throw new RuntimeException();
}
return "Hello";
});
// exceptionally:處理異常
CompletableFuture<String> future2 = future.exceptionally(e -> {
System.out.println("異常信息: " + e.getMessage());// 異常信息: java.lang.RuntimeException
return null;
});
// whenComplete:處理異常和結(jié)果
CompletableFuture<String> future3 = future.whenComplete((s, e) -> {
if (e != null) {
System.out.println("異常信息: " + e.getMessage());
} else {
System.out.println(s);
}
});
// handle:處理異常和結(jié)果,返回新結(jié)果
CompletableFuture<String> future4 = future.handle((s, e) -> {
if (s != null) {
return s + " World";
} else {
return "異常信息: " + e.getMessage();
}
});
System.out.println("------");
System.out.println(future2.join());// Hello 或 null
System.out.println(future4.join());// Hello World 或 異常信息: java.lang.RuntimeException
System.out.println(future3.join());// Hello World 或 直接報(bào)錯(cuò)退出
6.allOf、anyOf:等待 多個(gè) 或 任一 任務(wù)的完成。
String[] arr = new String[2];
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
arr[0] = "Hello";
return "Hello";
});
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
arr[1] = "World";
return "World";
});
// anyOf:等待任一任務(wù)完成
CompletableFuture<Object> future3 = CompletableFuture.anyOf(future1, future2);
System.out.println(arr[0] + " " + arr[1]);// Hello World 或 Hello null 或 null World
// allOf:等待所有任務(wù)全部完成
CompletableFuture<Void> future4 = CompletableFuture.allOf(future1, future2);
System.out.println(arr[0] + " " + arr[1]);// Hello World
System.out.println("------");
System.out.println(future3.join());// Hello 或 World
System.out.println(future4.join());// null
浙公網(wǎng)安備 33010602011771號(hào)