future java join_【CompletableFuture】CompletableFuture中join()和get()方法的区别

一.相同点:

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) {@linkCompletionException} with the underlying

* exception as its cause.

*

*@returnthe result value

*@throwsCancellationException if the computation was cancelled

*@throwsCompletionException if this future completed

* exceptionally or a completion computation threw an exception*/

demo:

public static voidmain(String[] args) {

CompletableFuture 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.

*

*@returnthe result value

*@throwsCancellationException if this future was cancelled

*@throwsExecutionException if this future completed exceptionally

*@throwsInterruptedException if the current thread was interrupted

* while waiting*/

demo

public static void main(String[] args) throwsExecutionException, InterruptedException {

CompletableFuture f1 = CompletableFuture.supplyAsync(() ->{int i =1/0;return 1;

});

f1.get();

System.out.println("CompletableFuture Test");

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值