JUC 学习笔记 2:Future

1、接口介绍

A Future represents the result of an asynchronous computation.

Future 表示异步计算的结果。

Methods are provided

  • to check if the computation is complete,
  • to wait for its completion,
  • and to retrieve the result of the computation.

Future 提供了

  • 检查计算是否完成 isDone()
  • 等待计算完成 get(long timeout, TimeUnit unit)
  • 以及获取计算结果的方法 get()

The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready.

当计算完成时,才能使用 get() 方法获取结果,否则 get() 方法将会进行阻塞,直到计算完成

Cancellation is performed by the cancel method. Additional methods are provided to determine if the task completed normally or was cancelled.

通过 cancel() 方法进行取消。提供了 isCancelled() 方法来确定任务是正常完成还是被取消。

Once a computation has completed, the computation cannot be cancelled.
一旦计算完成,就不能取消计算。

If you would like to use a Future for the sake of cancellability but not provide a usable result, you can declare types of the form Future<?> and return null as a result of the underlying task.

如果为了可取消性而使用 Future,但不提供可用的结果,可以声明 Future<?> 类型并且返回 null 作为任务结果。

2、方法介绍

2.1、取消任务

boolean cancel(boolean mayInterruptIfRunning);

Attempts to cancel execution of this task. This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason.

cancel() 方法尝试取消执行任务。如果

  1. 任务已完成
  2. 任务已取消
  3. 或由于其他原因无法取消,

则此尝试将失败。

If successful, and this task has not started when cancel is called, this task should never run.

如果取消成功,并且在调用 cancel() 方法时任务尚未开始执行,则任务将不会执行。

If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

如果任务已经启动,那么 mayInterruptIfRunning 参数确定是否应该中断执行任务的线程以尝试停止任务

After this method returns, subsequent calls to isDone will always return true. Subsequent calls to isCancelled will always return true if this method returned true.

cancel() 方法返回后,对 isDone() 方法的后续调用将始终返回 true。如果 cancel() 方法返回 true,则对 isCancelled 方法的后续调用将始终返回 true。

2.2、检查任务是否已取消

boolean isCancelled();

2.3、检查任务是否已完成

boolean isDone();

Returns true if this task completed. Completion may be due to

  1. normal termination,
  2. an exception,
  3. or cancellation

– in all of these cases, this method will return true.

如果任务已完成,则 isDone() 方法返回 true。完成可能是由于

  1. 任务正常终止
  2. 任务发生异常
  3. 或任务已被取消

在所有这些情况下,此 isDone() 方法都将返回 true。

2.4、等待任务完成,获取任务结果

V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.

Params:

  1. timeout – the maximum time to wait
  2. unit – the time unit of the timeout argument

Returns: the computed result

Throws:

  • CancellationException – if the computation was cancelled
  • ExecutionException – if the computation threw an exception
  • InterruptedException – if the current thread was interrupted while waiting
  • TimeoutException – if the wait timed out

get(long timeout, TimeUnit unit) 方法最多等待指定时间,然后获取任务结果。

参数:

  1. timeout – 等待的最长时间
  2. unit – 超时参数的时间单位

抛出:

  • CancellationException – 如果任务被取消
  • ExecutionException – 如果任务发生异常
  • InterruptedException – 如果当前线程在等待时被中断
  • TimeoutException – 如果等待超时
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值