标签:trace one not 分享 应用 print sync .com call
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方法:
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.实现例子
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.总结
标签:trace one not 分享 应用 print sync .com call
原文地址:http://www.cnblogs.com/ttylinux/p/7189776.html