0% found this document useful (0 votes)
9 views4 pages

Slides Concurrency Additional Thread Pools Callable Submit and the Future

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Slides Concurrency Additional Thread Pools Callable Submit and the Future

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Difference between Runnable and Callable

What's the difference between Runnable and Callable?


Runnable's Functional Method Callable's Functional Method

void run() V call() throws Exception

Significantly, Callable returns a value.


This means you can get data back from your running threads.

COMPLETE JAVA MASTERCLASS


Additional Thread Pools, Callable, Submit
and the Future
execute vs. submit

On this slide, I'm showing you the method signatures for both the execute and
submit methods.

method signature

execute void execute(Runnable command)

Future<?> submit(Runnable task)

submit <T> Future<T> submit(Runnable task, T result)

<T> Future<T> submit(Callable<T> task)

COMPLETE JAVA MASTERCLASS


Additional Thread Pools, Callable, Submit
and the Future
The Future Interface

A Future represents a result, of an asynchronous


computation.
It's a generic type, a placeholder for a result instance.
It has methods that cancel the task, retrieve the result,
or check if the computation was completed or
cancelled.
The get method returns the result, but you can only
call this get method, when the computation is
complete, otherwise the call will block, until it does
complete.
The overloaded version of the get method allows you
to specify a wait time, rather than blocking.
COMPLETE JAVA MASTERCLASS
Additional Thread Pools, Callable, Submit
and the Future
invokeAll vs. invokeAny

When would you want to use invoke Any vs. invokeAll?

Characteristic invokeAny invokeAll

Tasks Executed At least one, the first to complete. All tasks get executed.
Returns a list of results, as futures, for all
Result of the first task to complete, not
Result of the tasks, once they have all
a Future.
completed.
Use this method when you need a Use this method when you want all the
quick response back from one of tasks to be executed concurrently, and
Use cases
several tasks, and you don't care if all tasks must complete before
some will fail. proceeding.

COMPLETE JAVA MASTERCLASS


Additional Thread Pools, Callable, Submit
and the Future

You might also like