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) 中的“结果"有什么作用?
基础教程推荐
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01