gpt4 book ai didi

java - 为什么该类名为 RunnableFuture 而不是 CallableFuture

转载 作者:行者123 更新时间:2023-12-02 01:23:50 26 4
gpt4 key购买 nike

以下代码来自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 吗?我在这里缺少什么?

最佳答案

RunnableFuture 的要点是:

  1. 这是 future
  2. 它是一个可运行

它将您已有的Callable转换为FutureRunnable。它涵盖了它想要涵盖的确切用例。如果您有一个 Callable 并且需要一个 Future,可以使用 FutureTask 构造函数。

关于java - 为什么该类名为 RunnableFuture<T> 而不是 CallableFuture<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11428151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com