RunnableFuture接口(源碼解讀)

 此接口繼承了Runnable和Future接口。

  • 其中Runnable接口中就只有一個run方法,用於執行任務。
  • 其中Future接口中主要有:

對任務的執行進行取消操作。

判斷任務是否被取消。

判斷任務是否被執行完成。

獲取任務返回的結果。

至此RunnableFuture接口想要其實現類實現的功能就是相當於一個擁有run方法的future接口。

可以執行任務。

可以取消任務。

可以知道任務是否完成。

可以知道任務是否取消。

可以獲取任務返回值。(及設置獲取返回值的等待時間,有參的get方法)

Future接口(源碼解讀):

https://blog.csdn.net/qq_29519041/article/details/100034256

package java.util.concurrent;

/**
 * A {@link Future} that is {@link Runnable}. Successful execution of
 * the {@code run} method causes completion of the {@code Future}
 * and allows access to its results.
 * @see FutureTask
 * @see Executor
 * @since 1.6
 * @author Doug Lea
 * @param <V> The result type returned by this Future's {@code get} method
 */
public interface RunnableFuture<V> extends Runnable, Future<V> {
    /**
     * Sets this Future to the result of its computation
     * unless it has been cancelled.
     */
    void run();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章