Inq*_*ive 4 java java.util.concurrent
以下代码来自AbstractExecutorService:
/**
* Returns a <tt>RunnableFuture</tt> for the given callable task.
*
* @param callable the callable task being wrapped
* @return a <tt>RunnableFuture</tt> which when run will call the
* underlying callable and which, as a <tt>Future</tt>, will yield
* the callable's result as its result and provide for
* cancellation of the underlying task.
* @since 1.6
*/
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return new FutureTask<T>(callable);
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么返回的对象的类newTaskFor()会被称为 RunnableFuture 而不是 CallableFuture?我在这里错过了什么?
a的要点RunnableFuture是:
FutureRunnable它将Callable您已经拥有的东西转换为既是 aFuture又是 a 的东西Runnable。它涵盖了它打算涵盖的确切用例。如果您有 aCallable并且需要 a Future,则有FutureTask构造函数。
RunnableFuture代表一个特定的概念:一个未来的结果,其计算可以由工作线程通过调用显式执行run()。
由于工作线程通常对它们执行的计算结果不感兴趣,因此它们使用run()不返回结果。并且对这些结果感兴趣的线程可以get()在工作线程完成计算后立即获取它们。