What does #39;result#39; in ExecutorService.submit(Runnable task, T result) do?(ExecutorService.submit(Runnable task, T result) 中的“结果有什么作用?)
问题描述
看看它刚刚说的 javadocs
Looking at the javadocs it just says
<代码><T>未来<T>submit(Runnable task, T result)
提交 Runnable 任务以执行并返回代表该任务的 Future.Future 的 get 方法将在成功完成后返回给定的结果.
参数:
task - 要提交的任务
result - 要返回的结果
<T> Future<T> submit(Runnable task, T result)
Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return the given result upon successful completion.
Parameters:
task - the task to submit
result - the result to return
但是它对结果有什么影响呢?它在那里存储任何东西吗?它只是使用结果的类型来指定Future<T>
的类型吗?
but what does it do with result? does it store anything there? does it just use the type of result to specify the type of Future<T>
?
推荐答案
它对结果没有任何作用 - 只是保持它.任务成功完成后,调用future.get()
会返回你传入的结果.
It doesn't do anything with the result - just holds it. When the task successfully completes, calling future.get()
will return the result you passed in.
这里是Executors$RunnableAdapter的源码,显示任务运行后,返回原来的结果:
Here is the source code of Executors$RunnableAdapter, which shows that after the task has run, the original result is returned:
static final class RunnableAdapter<T> implements Callable<T> {
final Runnable task;
final T result;
RunnableAdapter(Runnable task, T result) {
this.task = task;
this.result = result;
}
public T call() {
task.run();
return result;
}
}
是的,结果的泛型类型应该与返回的 Future 匹配.
And yes, the generic type of the result should match that of the returned Future.
这篇关于ExecutorService.submit(Runnable task, T result) 中的“结果"有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ExecutorService.submit(Runnable task, T result) 中的“结果"有什么作用?


- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 获取数字的最后一位 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 转换 ldap 日期 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01