completableFuture.join
时间: 2023-12-01 13:04:19 浏览: 38
CompletableFuture.join() is a method in Java's CompletableFuture class that blocks the current thread until the CompletableFuture has completed its computation and returns the result of the computation. It is similar to calling get() method, but with one important difference: join() does not throw checked exceptions, so it can be used in lambda expressions and method references that do not declare any checked exceptions.
Here is an example of using join() method:
```
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
return "Hello";
});
String result = future.join();
System.out.println(result);
```
In this example, we create a CompletableFuture that returns the string "Hello" asynchronously using the supplyAsync() method. We then call the join() method on the CompletableFuture to block the current thread until the computation is completed and get the result of the computation. Finally, we print the result "Hello" to the console.