CompletableFuture 类详解

1.runAsyncsupplyAsyncgetjoin:开启异步任务,获取结果。
// runAsync:没有返回值
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
    System.out.println("Hello");
});

// supplyAsync:有返回值
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
    return "World";
});

// join:不强制 try,不能设置超时时间,不能响应线程中断
System.out.println(future2.join());// World

// get:必须 try 或 添加异常签名,可以设置超时时间,可以响应线程中断
try {
    System.out.println(future2.get());// World
} catch (Exception ignored) {
}
 
2.thenApplythenAcceptthenRun:处理任务的结果。
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");

// thenRun:在任务完成后执行
CompletableFuture<Void> future2 = future.thenRun(() -> System.out.println("完成了"));

// thenAccept:接收任务的结果
CompletableFuture<Void> future3 = future.thenAccept(s -> System.out.println(s + " World"));// Hello World

// thenApply:接收任务的结果,返回一个新值
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.thenCombinethenAcceptBothrunAfterBoth:处理两个任务的结果。
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");

// runAfterBoth:在两个任务完成后执行
CompletableFuture<Void> combinedFuture = future1.runAfterBoth(future2, () -> System.out.println("完成了"));

// thenAcceptBoth:接收两个任务的结果
CompletableFuture<Void> combinedFuture2 = future1.thenAcceptBoth(future2, (s1, s2) -> System.out.println(s1 + " " + s2));// Hello World

// thenCombine:接收两个任务的结果,返回一个新值
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.applyToEitheracceptEitherrunAfterEither:处理两个任务任一的结果。
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");

// runAfterBoth:在两个任务任一完成后执行
CompletableFuture<Void> combinedFuture = future1.runAfterEither(future2, () -> System.out.println("完成了"));

// thenAcceptBoth:接收两个任务的任一结果
CompletableFuture<Void> combinedFuture2 = future1.acceptEither(future2, (s) -> System.out.println(s));// Hello 或 World

// thenCombine:接收两个任务的任一结果,返回一个新值
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.exceptionallywhenCompletehandle:处理异常。
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:处理异常和结果
CompletableFuture<String> future3 = future.whenComplete((s, e) -> {
    if (e != null) {
        System.out.println("异常信息: " + e.getMessage());
    } else {
        System.out.println(s);
    }
});

// handle:处理异常和结果,返回新结果
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 或 直接报错退出

 

6.allOfanyOf:等待 多个 或 任一 任务的完成。
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:等待任一任务完成
CompletableFuture<Object> future3 = CompletableFuture.anyOf(future1, future2);
System.out.println(arr[0] + " " + arr[1]);// Hello World 或 Hello null 或 null World

// allOf:等待所有任务全部完成
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

 

posted @ 2024-04-28 20:07  Yfeil  阅读(107)  评论(0编辑  收藏  举报