Most visited

Recently visited

Added in API level 1

Future

public interface Future

java.util.concurrent.Future<V>
Known Indirect Subclasses


Future表示异步计算的结果。 提供的方法用于检查计算是否完成,是否等待完成以及检索计算结果。 只有在计算完成时才能使用方法get检索结果,如果需要,可以将其阻塞,直到准备就绪。 取消由cancel方法执行。 提供了其他方法来确定任务是否正常完成或取消。 一旦计算完成,计算就不能被取消。 如果您想为了取消而使用Future但不提供可用的结果,则可以声明表单Future<?>类型并返回null作为基础任务的结果。

样例用法 (请注意,以下类都是组成的。)

 interface ArchiveSearcher { String search(String target); }
 class App {
   ExecutorService executor = ...
   ArchiveSearcher searcher = ...
   void showSearch(final String target)
       throws InterruptedException {
     Future<String> future
       = executor.submit(new Callable<String>() {
         public String call() {
             return searcher.search(target);
         }});
     displayOtherThings(); // do other things while searching
     try {
       displayText(future.get()); // use future
     } catch (ExecutionException ex) { cleanup(); return; }
   }
 }
The FutureTask class is an implementation of Future that implements Runnable, and so may be executed by an Executor. For example, the above construction with submit could be replaced by:
 FutureTask<String> future =
   new FutureTask<>(new Callable<String>() {
     public String call() {
       return searcher.search(target);
   }});
 executor.execute(future);

内存一致性影响:在另一个线程中相应的 Future.get()之后的异步计算 happen-before操作所采取的操作。

也可以看看:

Summary

Public methods

abstract boolean cancel(boolean mayInterruptIfRunning)

试图取消执行此任务。

abstract V get(long timeout, TimeUnit unit)

如果需要,最多等待计算完成的给定时间,然后检索其结果(如果可用)。

abstract V get()

如果需要,等待计算完成,然后检索其结果。

abstract boolean isCancelled()

如果此任务在正常完成之前取消,则返回 true

abstract boolean isDone()

如果此任务完成,则返回 true

Public methods

cancel

Added in API level 1
boolean cancel (boolean mayInterruptIfRunning)

试图取消执行此任务。 如果任务已完成,已被取消或因其他原因无法取消,此尝试将失败。 如果成功,并且此任务在cancel被调用时尚未开始,则此任务不应运行。 如果任务已经启动,那么mayInterruptIfRunning参数确定执行此任务的线程是否应该中断以试图停止任务。

在此方法返回后,对isDone()后续调用将始终返回true 对后续调用isCancelled()总是返回true如果此方法返回true

Parameters
mayInterruptIfRunning boolean: true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete
Returns
boolean false if the task could not be cancelled, typically because it has already completed normally; true otherwise

get

Added in API level 1
V get (long timeout, 
                TimeUnit unit)

如果需要,最多等待计算完成的给定时间,然后检索其结果(如果可用)。

Parameters
timeout long: the maximum time to wait
unit TimeUnit: the time unit of the timeout argument
Returns
V 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

Added in API level 1
V get ()

如果需要,等待计算完成,然后检索其结果。

Returns
V 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

isCancelled

Added in API level 1
boolean isCancelled ()

如果此任务在正常完成之前取消,则返回 true

Returns
boolean true if this task was cancelled before it completed

isDone

Added in API level 1
boolean isDone ()

如果此任务完成,则返回true 完成可能是由于正常终止,例外或取消 - 在所有这些情况下,此方法将返回true

Returns
boolean true if this task completed

Hooray!