Future类源码解析翻译

90 篇文章 0 订阅
25 篇文章 0 订阅
/**

 * A <tt>Future</tt> represents the result of an asynchronous

 * computation.  Methods are provided to check if the computation is
 * complete, to wait for its completion, and to retrieve the result of
 * the computation.  The result can only be retrieved using method
 * <tt>get</tt> when the computation has completed, blocking if
 * necessary until it is ready.  Cancellation is performed by the
 * <tt>cancel</tt> method.  Additional methods are provided to
 * determine if the task completed normally or was cancelled. Once a
 * computation has completed, the computation cannot be cancelled.
 * If you would like to use a <tt>Future</tt> for the sake
 * of cancellability but not provide a usable result, you can
 * declare types of the form {@code Future<?>} and
 * return <tt>null</tt> as a result of the underlying task.

 *上段翻译:

Future表示异步计算的结果。它提供的方法是去检测异步任务是否完成,等待其完成,检索计算结果。

异步计算的结果只能通过get()方法在异步计算完成的时候得到,并且它是阻塞的,直到计算完成。

通过cancel()方法进行取消。提供的其他方法用于确定任务是完成或者取消。一旦异步计算完成,它将不能够被取消。

如果你想用Future但是不想要结果,你可以声明在任务的结果返回null。

 * <p>
 * <b>Sample Usage应用例子</b> (Note that the following classes are all
 * made-up.) <p>请注意,以下类都是用Future
 *  <pre> {@code
 * 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; }
 *   }
 * }}</pre>
 *
 * The {@link FutureTask} class is an implementation of <tt>Future</tt> that
 * implements <tt>Runnable</tt>, and so may be executed by an <tt>Executor</tt>.

 * For example, the above construction with <tt>submit</tt> could be replaced by:

翻译:FutureTask,是实现了Runnable接口的类是Future的一个实现,它可以用Executor(线程池)执行。

例如,上面的代码可以被替换为:

 *  <pre> {@code
 *     FutureTask<String> future =
 *       new FutureTask<String>(new Callable<String>() {
 *         public String call() {
 *           return searcher.search(target);
 *       }});
 *     executor.execute(future);}</pre>
 *

 * <p>Memory consistency effects: Actions taken by the asynchronous computation

上述,异步计算做的事情是一样的。

 * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
 * actions following the corresponding {@code Future.get()} in another thread.
 *
 * @see FutureTask
 * @see Executor
 * @since 1.5
 * @author Doug Lea
 * @param <V> The result type returned by this Future's <tt>get</tt> method
 */
public interface Future<V> {


    /**
     * 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. If successful,
     * and this task has not started when <tt>cancel</tt> is called,
     * this task should never run.  If the task has already started,
     * then the <tt>mayInterruptIfRunning</tt> parameter determines
     * whether the thread executing this task should be interrupted in
     * an attempt to stop the task.

     *翻译:试图取消任务的执行。如果任务已经完成或者因为一些原因任务没有开始,将会取消失败。

如果取消成功,或者当取消时任务还没开始执行,这个任务将永远不会执行。

如果任务已经开始执行,则mayInterruptIfRunning参数将会决定正在执行的任务是否以停止任务的方式来中断。

     * <p>After this method returns, subsequent calls to {@link #isDone} will
     * always return <tt>true</tt>.  Subsequent calls to {@link #isCancelled}
     * will always return <tt>true</tt> if this method returned <tt>true</tt>.
     *翻译:这个方法返回之后,如果这个方法返回true,则调用isDone方法将会一直都返回true;调用isCancelled,将会一直返回true;
     * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
     * task should be interrupted; otherwise, in-progress tasks are allowed
     * to complete
     * @return <tt>false</tt> if the task could not be cancelled,
     * typically because it has already completed normally;
     * <tt>true</tt> otherwise
     */翻译:mayInterruptIfRunning,任务可以被取消时为true,任务如果不能被取消为false.
    boolean cancel(boolean mayInterruptIfRunning);


    /**
     * Returns <tt>true</tt> if this task was cancelled before it completed
     * normally.
     *如果任务完成前取消,返回true
     * @return <tt>true</tt> if this task was cancelled before it completed
     */
    boolean isCancelled();


    /**
     * Returns <tt>true</tt> if this task completed.
     *如果任务已经完成,返回true
     * Completion may be due to normal termination, an exception, or
     * cancellation -- in all of these cases, this method will return
     * <tt>true</tt>.
     *
     * @return <tt>true</tt> if this task completed
     */
    boolean isDone();


    /**
     * Waits if necessary for the computation to complete, and then
     * retrieves its result.
     *等待任务完成,返回结果
     * @return the computed result
     * @throws CancellationException if the computation was cancelled如果任务取消,抛出CancellationException异常
     * @throws ExecutionException if the computation threw an
     * exception
     * @throws InterruptedException if the current thread was interrupted
     * while waiting如果当前线程被中断在等待,抛出InterruptedException
     */
    V get() throws InterruptedException, ExecutionException;


    /**
     * Waits if necessary for at most the given time for the computation
     * to complete, and then retrieves its result, if available.
     *等待给定的等待最大时间,如果得到结果,返回结果
     * @param timeout the maximum time to wait
     * @param unit the time unit of the timeout argument
     * @return the computed result
     * @throws CancellationException if the computation was cancelled如果任务取消,抛出CancellationException异常
     * @throws ExecutionException if the computation threw an
     * exception如果计算抛出异常,则抛出ExecutionException
     * @throws InterruptedException if the current thread was interrupted
     * while waiting
     * @throws TimeoutException if the wait timed out抛出TimeoutException异常,如果等待超时
     */
    V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值