Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

java - What does 'result' in ExecutorService.submit(Runnable task, T result) do?

Looking at the javadocs it just says

<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

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>?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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;
    }
}

And yes, the generic type of the result should match that of the returned Future.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...