public interface Callable<V>{ V call() throws Exception; } public interface Runnable{ void run(); }
public interface Future<V> { boolean cancel(boolean mayInterruptIfRunning); boolean isCancelled(); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }
//建立FutureTask對象,傳入Callable對象;Callable對象,耗時操做是放在call方法裏面的 import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; public class FutureTaskExample { public static void main(String[] args) { Callable<String> worker = new Callable<String>() { @Override public String call() throws Exception { // TODO Auto-generated method stub System.out.println("do computation"); return "computation result"; } } ; FutureTask<String> future = new FutureTask<String>(worker) { @Override protected void done() { System.out.println("Done task"); } }; new Thread(future) { @Override public void run() { future.run(); } }.start(); try { System.out.println("computation:"+future.get()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
調用FutureTask的run方法就觸發執行,能夠查看FutureTask的源碼獲得解釋:調用run,會致使調用sync.innerRun();而innerRun會調用傳入的Callable對象的call方法:html
public class FutureTask<V> implements RunnableFuture<V> { public FutureTask(Callable<V> callable) { if (callable == null) throw new NullPointerException(); sync = new Sync(callable); } ..... // The following (duplicated) doc comment can be removed once // // 6270645: Javadoc comments should be inherited from most derived // superinterface or superclass // is fixed. /** * Sets this Future to the result of its computation * unless it has been cancelled. */ public void run() { sync.innerRun(); } /** * Synchronization control for FutureTask. Note that this must be * a non-static inner class in order to invoke the protected * <tt>done</tt> method. For clarity, all inner class support * methods are same as outer, prefixed with "inner". * * Uses AQS sync state to represent run status */ private final class Sync extends AbstractQueuedSynchronizer { void innerRun() { if (!compareAndSetState(READY, RUNNING)) return; runner = Thread.currentThread(); if (getState() == RUNNING) { // recheck after setting thread V result; try { result = callable.call(); } catch (Throwable ex) { setException(ex); return; } set(result); } else { releaseShared(0); // cancel } } } }
2.實現例子java
import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; public class FutureTaskExample { public static void main(String[] args) { Callable<String> worker = new Callable<String>() { @Override public String call() throws Exception { // TODO Auto-generated method stub System.out.println("do computation"); return "computation result"; } } ; FutureTask<String> future = new FutureTask<String>(worker) { @Override protected void done() { System.out.println("Done task"); } }; new Thread(future) { @Override public void run() { future.run(); } }.start(); try { System.out.println("computation:"+future.get()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
3.總結api