public Future<?> submit(Runnable task) {
//任务为空 直接抛空指针异常
if (task == null) throw new NullPointerException();
RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask);
return ftask;
}
3.1 newTaskFor方法
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
return new FutureTask<T>(runnable, value);
}
public FutureTask(Runnable runnable, V result) {
this.callable = Executors.callable(runnable, result); //Callable<V>
this.state = NEW; // ensure visibility of callable
}
//弄了个适配器 把传进来的runnable任务转换成了callable任务 这样就可以返回执行结果了
public static <T> Callable<T> callable(Runnable task, T result) {
if (task == null)
throw new NullPointerException();
return new RunnableAdapter<T>(task, result);
}
RunnableAdapter(Runnable task, T result) {
this.task = task;
this.result = result;
}
网友评论