1. 程式人生 > >Future類原始碼解析翻譯

Future類原始碼解析翻譯

     * <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;
}