一.相同点:

  join()和get()方法都是用来获取CompletableFuture异步之后的返回值

二.区别:

  1.join()方法抛出的是uncheck异常(即未经检查的异常),不会强制开发者抛出,

  会将异常包装成CompletionException异常 /CancellationException异常,但是本质原因还是代码内存在的真正的异常,

  文档说明:

  /**
* Returns the result value when complete, or throws an
* (unchecked) exception if completed exceptionally. To better
* conform with the use of common functional forms, if a
* computation involved in the completion of this
* CompletableFuture threw an exception, this method throws an
* (unchecked) {@link CompletionException} with the underlying
* exception as its cause.
*
* @return the result value
* @throws CancellationException if the computation was cancelled
* @throws CompletionException if this future completed
* exceptionally or a completion computation threw an exception
*/

  demo:

    public static void main(String[] args) {
CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(() -> {
int i =1/0;
return 1;
});
CompletableFuture.allOf(f1).join();
System.out.println("CompletableFuture Test");
}

  异常信息

Exception in thread "main" java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1592)
at java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1582)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: java.lang.ArithmeticException: / by zero
at com.gabriel.stage.utils.IpAddressUtil.lambda$main$0(IpAddressUtil.java:44)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
... 5 more

  2.get()方法抛出的是经过检查的异常,ExecutionException, InterruptedException 需要用户手动处理(抛出或者 try catch)

  文档说明

 /**
* Waits if necessary for this future to complete, and then
* returns its result.
*
* @return the result value
* @throws CancellationException if this future was cancelled
* @throws ExecutionException if this future completed exceptionally
* @throws InterruptedException if the current thread was interrupted
* while waiting
*/

  demo

public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(() -> {
int i =1/0;
return 1;
});
f1.get();
System.out.println("CompletableFuture Test");
}

【CompletableFuture】CompletableFuture中join()和get()方法的区别的更多相关文章

  1. Java线程中run和start方法的区别

    http://bbs.csdn.net/topics/350206340 Thread类中run()和start()方法的区别如下:run()方法:在本线程内调用该Runnable对象的run()方法 ...

  2. 深入理解jQuery中live与bind方法的区别

    本篇文章主要是对jQuery中live与bind方法的区别进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助 注意如果是通过jq添加的层和对象一定要用live(),用其他的都不起作用 ...

  3. js正则表达式中test,exec,match方法的区别说明

    js正则表达式中test,exec,match方法的区别说明 test test 返回 Boolean,查找对应的字符串中是否存在模式.var str = "1a1b1c";var ...

  4. jQuery中attr和prop方法的区别

    jQuery中attr和prop方法的区别。 http://my.oschina.net/bosscheng/blog/125833 http://www.javascript100.com/?p=8 ...

  5. C#中抽象方法与虚方法的区别(转)

    C#中抽象方法与虚方法的区别   一.抽象方法:只在抽象类中定义,方法修饰符不能使用private,virtual,static. 抽象方法如下示: public abstract class Peo ...

  6. Java -- Thread中start和run方法的区别

    一.认识Thread的 start() 和 run() 1.start(): 我们先来看看API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并发 ...

  7. 认识多线程中start和run方法的区别?

    一.认识多线程中的 start() 和 run() 1.start(): 先来看看Java API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并 ...

  8. 线程之sleep(),wait(),yield(),join()等等的方法的区别

    操作线程的常用方法大体上有sleep(),join(),yield()(让位),wait(),notify(),notifyAll(),关键字synchronized等等.    由于这些方法功能有些 ...

  9. Java中wait和sleep方法的区别

    1.两者的区别 这两个方法来自不同的类分别是Thread和Object 最主要是sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法(锁代码块和方法锁). wait ...

随机推荐

  1. System.IO.IOException:“找不到资源“window1.xaml”。” 解决方法

    报错:找不到资源"window1.xaml 原因:在编译时使用的是en-US选项进行编译并生成了en-US为名的文件夹,里面包含了可本地化的内容:但是你的本地系统使用的是zh-CN,在你运行 ...

  2. OSI协议简述版

    OSI简介 OSI只是计算机网络中的一种协议名称缩写,它只是电脑间传输数据的协议,并不代表具体的物理设备,并且这种协议,只是被人为的划分为五层:物理层.数据链路层.网络层.传输层.应用层.记住,它只是 ...

  3. 面试官:不会sql优化?出门右转顺便带上门,谢谢

    导读 作为一个后端程序员,数据库这个东西是绕不开的,特别是写sql的能力,如果您参加过多次面试,那么一定会从面试复盘中发现面试官总是会考察到sql优化这个东西. 我在之前的多次面试中最常遇到的一个问题 ...

  4. Java 常用类——StringBuffer&StringBuilder【可变字符序列】

    一.字符串拼接问题 由于 String 类的对象内容不可改变,所以每当进行字符串拼接时,总是会在内存中创建一个新的对象. Demo: 1 public class StringDemo { 2 pub ...

  5. ClickHouse元数据异常-MySQLHandlerFactory:Failed to read RSA key pair from server

    Clickhouse版本:20.3.6.40-2 clickhouse集群三个节点,一分片,三副本,三个节点数据完全一样 1. 问题描述 在使用连接工具操作时,发现其中一个节点连接拒绝,无法操作,另外 ...

  6. [SDOI2009] HH的项链(待续)

    [SDOI2009] HH的项链(待续) 题目大意:对一个由若干个数字(可重复)组成的数列,询问\([l,r]\)中出现的不同的数字数量 考试时(考试时范围小)用的暴力,but,没有考虑数字0的情况, ...

  7. Solon 框架详解(九)- 渲染控制之定制统一的接口输出

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

  8. ES6学习笔记(4)- 解构

    一.解构的意义 二.对象解构 三.数组解构

  9. Mybatis最权威的知识点

    1.什么是Mybatis? (1)Mybatis是一个半ORM(对象关系映射)框架,它内部封装了JDBC,开发时只需要关注SQL语句本身,不需要花费精力去处理加载驱动.创建连接.创建statement ...

  10. 从键盘读入学生成绩,找出最高分, 并输出学生成绩等级(Java)

    从键盘读入学生成绩,找出最高分, 并输出学生成绩等级 一.题目 从键盘读入学生成绩,找出最高分,并输出学生成绩等级. 成绩>=最高分-10 等级为'A' 成绩>=最高分-20 等级为'B' ...