以下代码来自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);
}我不明白为什么从newTaskFor()返回的对象的类将被称为RunnableFuture而不是CallableFuture?这里我漏掉了什么?
发布于 2012-07-11 16:34:40
RunnableFuture表示一个特定的概念:一个将来的结果,它的计算可以由工作线程通过调用run()显式地执行。
因为工作线程通常对它们执行的计算结果不感兴趣,所以它们使用不返回结果的run()。只要工作线程完成了计算,对这些结果感兴趣的线程就可以从get()获取它们。
发布于 2012-07-11 16:45:42
RunnableFuture的意义在于:
It‘s a Future
Runnable,
它将您已经拥有的Callable转换为既是Future又是Runnable的东西。它涵盖了它打算涵盖的确切用例。如果你有一个Callable并且需要一个Future,有FutureTask构造函数。
发布于 2012-07-11 16:04:21
因为它是Runnable,即具有public void run()方法,而不是Callable,后者将具有<T> public <T> run()方法。
https://stackoverflow.com/questions/11428151
复制相似问题