从关系图可以看出FutureTask可以当作一个Runnable被线程执行。
Future构造器
public FutureTask(Callable<V> callable) {
}
public FutureTask(Runnable runnable, V result) {
}
示例:
class Runner implements Callable{
@Override
public String call() throws Exception {
return Thread.currentThread().getName()+".call";
}
}
public class Ttttt {
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
Runner runner = new Runner();
FutureTask<String> ft = new FutureTask<String>(runner);
Future<?> result = executor.submit(ft);
executor.shutdown();
try {
System.out.println(ft.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}