天天看點

Java常見面試題—Future與FutureTask

Future

Future就是對于具體的Runnable或者Callable任務的執行結果進行取消、查詢是否完成、擷取結果等操作。必要時可以通過get方法擷取執行結果,該方法會阻塞直到任務傳回結果。

Future類位于java.util.concurrent包下,它是一個接口:

/** 
* @see FutureTask 
 * @see Executor 
 * @since  
 * @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.     * 
     */  
    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;  
}  
           

在Future接口中聲明了5個方法,下面依次解釋每個方法的作用:

cancel()

方法用來取消任務,如果取消任務成功則傳回true,如果取消任務失敗則傳回false。參數mayInterruptIfRunning表示是否允許取消正在執行卻沒有執行完畢的任務,如果設定true,則表示可以取消正在執行過程中的任務。如果任務已經完成,則無論mayInterruptIfRunning為true還是false,此方法肯定傳回false,即如果取消已經完成的任務會傳回false;如果任務正在執行,若mayInterruptIfRunning設定為true,則傳回true,若mayInterruptIfRunning設定為false,則傳回false;如果任務還沒有執行,則無論mayInterruptIfRunning為true還是false,肯定傳回true。

isCancelled()

方法表示任務是否被取消成功,如果在任務正常完成前被取消成功,則傳回 true。

isDone()

方法表示任務是否已經完成,若任務完成,則傳回true;

get()

方法用來擷取執行結果,這個方法會産生阻塞,會一直等到任務執行完畢才傳回;

get(long timeout, TimeUnit unit)

用來擷取執行結果,如果在指定時間内,還沒擷取到結果,就直接傳回null。

也就是說Future提供了三種功能:

1)判斷任務是否完成;

2)能夠中斷任務;

3)能夠擷取任務執行結果。

因為Future隻是一個接口,是以是無法直接用來建立對象使用的,是以就有了下面的FutureTask。

FutureTask

FutureTask的實作:

public class FutureTask<V> implements RunnableFuture<V>

           

FutureTask類實作了RunnableFuture接口,RunnableFuture接口:

public interface RunnableFuture<V> extends Runnable, Future<V> {  
    /** 
     * Sets this Future to the result of its computation 
     * unless it has been cancelled. 
     */  
    void run();  
}  
           

可以看出RunnableFuture繼承了Runnable接口和Future接口,而FutureTask實作了RunnableFuture接口。是以它既可以作為Runnable被線程執行,又可以作為Future得到Callable的傳回值。

FutureTask提供了2個構造器:

public FutureTask(Callable<V> callable) {  
    if (callable == null)  
        throw new 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  
}  
           

可以看到,Runnable注入會被Executors.callable()函數轉換為Callable類型,即FutureTask最終都是執行Callable類型的任務。該适配函數的實作如下:

public static <T> Callable<T> callable(Runnable task, T result) {  
    if (task == null)  
        throw new NullPointerException();  
    return new RunnableAdapter<T>(task, result);  
} 
           

RunnableAdapter擴充卡

/** 
 * A callable that runs given task and returns given result 
 */  
static final class RunnableAdapter<T> implements Callable<T> {  
    final Runnable task;  
    final T result;  
    RunnableAdapter(Runnable task, T result) {  
        this.task = task;  
        this.result = result;  
    }  
    public T call() {  
        task.run();  
        return result;  
    }  
}  
           

FutureTask是Future接口的一個唯一實作類。

FutureTask實作了Runnable,是以它既可以通過Thread包裝來直接執行,也可以送出給ExecuteService來執行。

FutureTask實作了Futrue可以直接通過get()函數擷取執行結果,該函數會阻塞,直到結果傳回。

執行個體:

Callable+Future擷取執行結果

public class Test {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newCachedThreadPool();
        Task task = new Task();
        Future<Integer> result = executor.submit(task);
        executor.shutdown();

        try {
            Thread.sleep();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        System.out.println("主線程在執行任務");

        try {
            System.out.println("task運作結果"+result.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        System.out.println("所有任務執行完畢");
    }
}
class Task implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {
        System.out.println("子線程在進行計算");
        Thread.sleep();
        int sum = ;
        for(int i=;i<;i++)
            sum += i;
        return sum;
    }
}
           

執行結果:

子線程在進行計算
主線程在執行任務
task運作結果
所有任務執行完畢
           

Callable+FutureTask擷取執行結果

public class Test {
    public static void main(String[] args) {
        //第一種方式
        ExecutorService executor = Executors.newCachedThreadPool();
        Task task = new Task();
        FutureTask<Integer> futureTask = new FutureTask<Integer>(task);
        executor.submit(futureTask);
        executor.shutdown();

        //第二種方式,注意這種方式和第一種方式效果是類似的,隻不過一個使用的是ExecutorService,一個使用的是Thread
        /*Task task = new Task();
        FutureTask<Integer> futureTask = new FutureTask<Integer>(task);
        Thread thread = new Thread(futureTask);
        thread.start();*/

        try {
            Thread.sleep();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        System.out.println("主線程在執行任務");

        try {
            System.out.println("task運作結果"+futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        System.out.println("所有任務執行完畢");
    }
}
class Task implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {
        System.out.println("子線程在進行計算");
        Thread.sleep();
        int sum = ;
        for(int i=;i<;i++)
            sum += i;
        return sum;
    }
}
           

繼續閱讀