【CompletableFuture】CompletableFuture中join()和get()方法的区别
方法 区别 get Join CompletableFuture
2023-06-13 09:17:38 时间
【CompletableFuture】CompletableFuture中join()和get()方法的区别
相同点:
join()和get()方法都是阻塞调用它们的线程(通常为主线程)来获取CompletableFuture异步之后的返回值。
这里再强调一下:
CompletableFuture.get() 和 CompletableFuture.join() 这两个方法是获取异步守护线程的返回值的。 ps: stage就是 CompletionStage 也就是 CompletableFuture 实现的接口,意思就是每一个 CompletableFuture的任务返回都是一个stage
看代码:
public class Test {
static Integer RES = 0;
public static Integer multipart(Integer a){
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
RES=30;
return a*a;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> multipart(5));
// System.out.println(future.join());
System.out.println(RES);
}
}
结果:
0
Process finished with exit code 0
把注释去掉后结果:
25
30
Process finished with exit code 0
不同点:
get() 方法会抛出经检查的异常,可被捕获,自定义处理或者直接抛出。
而 join() 会抛出未经检查的异常。
相关文章
- idea设置文件头注释_idea设置方法注释
- 在 .NET 6 中使用 Startup.cs 更简洁的方法
- win10企业版永久激活方法「建议收藏」
- getparameter方法的作用_get to和arrive的区别
- 面试官问:Stream 中的 map、peek、foreach 方法的区别?彻底懵了。。
- Dataloader有哪些使用方法
- Java 中static和非static的区别(方法和变量)
- 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 @Mixin 注解进行方法注入 | Mixin 混合多个类优先级分析 )
- 正则化方法:L1和L2 regularization及区别、数据集扩增、dropout详解大数据
- java获取服务器一些信息方法集合(访问路径之类的)详解编程语言
- Jquery中的.post和 .ajax两个方法的区别是什么详解编程语言
- Python sys模块常用方法
- Oracle中获取字符串位置的方法(oracle获取字符串位置)
- 的处理处理Linux参数列表过长的有效方法(linux参数列表过长)
- 应对MySQL数据库崩溃:恢复数据的有效方法(mysql数据库崩溃)
- MySQL中in和out查询语句的使用方法与区别(mysql中in和out)
- Oracle中时间减一小时的方法(oracle 减一个小时)
- JS特权方法定义作用以及与公有方法的区别
- c#方法中调用参数的值传递方式和引用传递方式以及ref与out的区别深入解析
- js中同步与异步处理的方法和区别总结
- 启用Nginx目录浏览功能的方法