本质就是继承自 Runable 和 Future, 就是一个可以获取运行结果的线程任务.
Future就是运行结果
Future<String> future
= executor.submit(new Callable<String>() {
public String call() {
return searcher.search(target);
}});
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
调用下面的方法运行.
@Nullable
public static <V> V runTask(FutureTask<V> task, Logger logger)
{
try
{
task.run();
return task.get();
}
catch (ExecutionException executionexception)
{
logger.fatal("Error executing task", (Throwable)executionexception);
}
catch (InterruptedException interruptedexception)
{
logger.fatal("Error executing task", (Throwable)interruptedexception);
}
return (V)null;
}
评论区