相同点
- 二者都会当任务完成时,返回 Future 结果。
- get():Waits if necessary for this future to complete, and then returns its result.
- join():Returns the result value when complete
- 如果计算被取消抛出异常:
CancellationException
@throws CancellationException if the computation was cancelled
不同点
- 异常处理不同:
- get():抛出的是经过检查的异常,
ExecutionException, InterruptedException 需要用户手动处理(抛出或者 try catch)
- join():抛出的是uncheck异常(即继承RuntimeException),不会强制开发者抛出,会将异常包装成
CompletionException异常,异常原因为代码中存在的真正的异常。
- get 方法可设置任务阻塞超时时间,具体方法:
public T get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
Object r;
long nanos = unit.toNanos(timeout);
return reportGet((r = result) == null ? timedGet(nanos) : r);
}