thread的真正执行体是runnable,一般使用可以new Thread(new Runnable)就可以
而FutureTask<V> implements RunnableFuture<V>, 并且interface RunnableFuture<V> extends Runnable, Future<V> 。
因此futtureTask通过实现Future可以获取状态。 并且因为实现了Runnable,也可以new Thread(new FutureTask)
FurtureTask有两个构造api
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 } 而Executors.callable(runnable, result);
public static <T> Callable<T> callable(Runnable task, T result) { if (task == null) throw new NullPointerException(); return new RunnableAdapter<T>(task, result); }
RunnableAdapter<T> implements Callable<T>
private static final class RunnableAdapter<T> implements Callable<T> { private final Runnable task; private final T result; RunnableAdapter(Runnable task, T result) { this.task = task; this.result = result; } public T call() { task.run(); return result; } }