completablefuture.get()坑
时间: 2023-04-22 18:02:25 浏览: 111
CompletableFuture.get() 是一个 Java CompletableFuture 类的方法,用于等待异步任务完成并返回其结果,它会阻塞当前线程,直到异步任务执行完毕并返回结果。
通常情况下,CompletableFuture.get() 方法应该在调用 CompletableFuture 对象的异步方法后使用。例如:
```
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
return "Hello, World!";
});
String result = future.get();
System.out.println(result); // 输出 "Hello, World!"
```
在上面的代码中,我们创建了一个 CompletableFuture 对象,并调用了它的 supplyAsync() 方法,这个方法接受一个 Supplier 函数作为参数,返回一个 CompletableFuture 对象。然后,我们使用 CompletableFuture.get() 方法来等待异步任务完成并返回结果。
需要注意的是,如果异步任务执行失败或被取消,CompletableFuture.get() 方法将会抛出异常。因此,在使用这个方法时需要适当地处理异常。
相关问题
return CompletableFuture.get
`CompletableFuture.get()`方法是用于获取CompletableFuture的结果的方法。如果CompletableFuture已经完成,它将立即返回结果。否则,它将等待CompletableFuture完成并返回结果。如果CompletableFuture完成时抛出异常,则此方法将抛出相应的异常。
以下是两个使用`CompletableFuture.get()`方法的例子:
1.获取CompletableFuture的结果
```java
CompletableFuture<String> future = CompletableFuture.completedFuture("Hello");
String result = future.get(); // 等待future完成并返回结果
System.out.println(result); // 输出:Hello
```
2.获取CompletableFuture的结果,如果发生异常则返回默认值
```java
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
throw new RuntimeException("Exception Occurred");
});
String result = future.exceptionally(ex -> "Default Value").get(); // 等待future完成并返回结果,如果发生异常则返回默认值
System.out.println(result); // 输出:Default Value
```
CompletableFuture.get()
CompletableFuture.get() 是一个 Java CompletableFuture 类的方法,用于等待异步任务完成并返回其结果,它会阻塞当前线程,直到异步任务执行完毕并返回结果。
通常情况下,CompletableFuture.get() 方法应该在调用 CompletableFuture 对象的异步方法后使用。例如:
```
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
return "Hello, World!";
});
String result = future.get();
System.out.println(result); // 输出 "Hello, World!"
```
在上面的代码中,我们创建了一个 CompletableFuture 对象,并调用了它的 supplyAsync() 方法,这个方法接受一个 Supplier 函数作为参数,返回一个 CompletableFuture 对象。然后,我们使用 CompletableFuture.get() 方法来等待异步任务完成并返回结果。
需要注意的是,如果异步任务执行失败或被取消,CompletableFuture.get() 方法将会抛出异常。因此,在使用这个方法时需要适当地处理异常。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)