Actual implementation of Callable and Future

Peter picture Peter · Jan 30, 2013 · Viewed 16.3k times · Source

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

Answer

Gray picture Gray · Jan 30, 2013

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.