Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actual implementation of Callable and Future

I am in the process of understanding fine grain util.concurrency. Where is implementation of the Java Callable and Future located in the JVM ?

I have found the Future class where it describes the future on the high level in Java lang, I am trying to find where it is described on the lower level.

To sum up it would be interesting to find the actual implementation of Future and Callable e.g: the part of the JVM that handles the Future.get() or Callable.call() and prescribes them how they should work.

Looking forward for your replies, Akonkagva

like image 797
Peter Avatar asked Jan 30 '13 17:01

Peter


People also ask

What is Callable and Future?

Observe that Callable and Future do two different things – Callable is similar to Runnable, in that it encapsulates a task that is meant to run on another thread, whereas a Future is used to store a result obtained from a different thread.

How is Future implemented in Java?

The main implementation of the Future interface is the FutureTask class. It is used by the ExecutorService classes to represent a submitted job, etc.. Callable (like Runnable ) is a simple interface that you implement yourself. It wraps a task that you want the ExecutorService thread-pools to execute.

What is valid about call method of Callable interface in Java?

Java Callable Interface DefinitionThe call() method can return a result. If the task is executed asynchronously, the result is typically propagated back to the creator of the task via a Java Future. This is the case when a Callable is submitted to an ExecutorService for concurrent execution.


2 Answers

Where is implementation of the Java Callable and Future located in the JVM ?

The main implementation of the Future interface is the FutureTask class. It is used by the ExecutorService classes to represent a submitted job, etc.. Callable (like Runnable) is a simple interface that you implement yourself. It wraps a task that you want the ExecutorService thread-pools to execute. You should download the source jars for these classes and take a look at the Java code yourself.

Neither of these classes contain any JVM black magic or anything. For example, if you construct a Callable class, it won't run in another thread unless you submit it to a thread-pool. You can use the Callable in many different places that have nothing to do with threads.

The JVM "black magic" around Future and Callable is mostly contained in the Thread class. It has underlying native support which works with the OS threads to do the actual job of running your task in another thread. There is still a lot of Java code in it if you want to see what it does but there are native and OS calls that the real magic.

Here's a good tutorial about how to use the executor services that were added to Java in 1.5.

like image 185
Gray Avatar answered Nov 06 '22 08:11

Gray


The Guava library has its own implementation of Future: AbstractFuture (and subclasses like SettableFuture) which is an alternative to FutureTask.

If you are interested in learning how such things are implemented, this might also be interesting to look at. Usually the Guava code is very well written.

like image 25
Philipp Wendler Avatar answered Nov 06 '22 10:11

Philipp Wendler