ExecutorService.submit(Runnable task, T result) 中的“结果"有什么作用?

What does #39;result#39; in ExecutorService.submit(Runnable task, T result) do?(ExecutorService.submit(Runnable task, T result) 中的“结果有什么作用?)

本文介绍了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) 中的“结果"有什么作用?

基础教程推荐