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) 中的“结果"有什么作用?
基础教程推荐
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
