多執行緒中的Runnable、Callable、Future、FutureTask的作用
阿新 • • 發佈:2019-01-06
在Java多執行緒程式設計中,建立啟動一個執行緒可以繼承Thread類,也可以實現Runnable介面,但是想要獲取執行緒的結果,則需要實現Callable介面,獲取結果則需要使用Future介面。下面分別介紹這幾個介面或者類的實現。
Runnable介面
Runnable介面只有一個run方法,其執行緒執行邏輯在run方法中實現。但是該方法沒有返回值,所以不能獲得執行緒執行的結果。其介面定義如下:
- publicinterface Runnable {
- /**
-
* When an object implementing interface <code>Runnable</code> is used
- * to create a thread, starting the thread causes the object's
- * <code>run</code> method to be called in that separately executing
- * thread.
- * <p>
- *
- * @see java.lang.Thread#run()
- */
- publicabstractvoid run();
- }
Callable介面
Callable介面和Runnable介面很相似,但是其call方法有返回值,所以可以獲取執行緒執行的返回值。
- publicinterface Callable<V> {
- /**
- * Computes a result, or throws an exception if unable to do so.
- *
- * @return computed result
- * @throws Exception if unable to compute a result
- */
- V call() throws Exception;
- }
Future介面
Future就是對於具體的Runnable或者Callable任務的執行結果進行 取消、查詢是否完成、獲取結果、設定結果操作。其get方法會阻塞,
直到超時,或者獲得任務執行結果。
- /**
- * @see FutureTask
- * @see Executor
- * @since 1.5
- * @author Doug Lea
- * @param <V> The result type returned by this Future's <tt>get</tt> method
- */
- publicinterface 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. *
- */
- boolean cancel(boolean mayInterruptIfRunning);
- /**
- * Returns <tt>true</tt> if this task was cancelled before it completed
- * normally.
- */
- boolean isCancelled();
- /**
- * Returns <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
- */
- 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
- */
- V get(long timeout, TimeUnit unit)
- throws InterruptedException, ExecutionException, TimeoutException;
- }
FutureTask
FutureTask是一個RunnableFuture<V>介面實現,而RunnableFuture<V>實現了Runnable和Future兩個介面,同時,它還可以包裝
Runnable和Callable介面,由構造函式注入:
- public FutureTask(Callable<V> callable) {
- if (callable == null)
- thrownew NullPointerException();
- this.callable = callable;
- this.state = NEW; // ensure visibility of callable
- }
- public FutureTask(Runnable runnable, V result) {
- this.callable = Executors.callable(runnable, result);
- this.state = NEW; // ensure visibility of callable
- }
由於FutureTask實現了Runnable介面和Future介面,同時包裝了Callable,所以它既可以傳遞給Thread類來執行,也可以提交給
ExecuteService來執行。並且還可以直接通過get函式來獲取執行結果。