Actual implementation of Callable and Future

I understand the thin utility. Where is Java Callable and Future located in the JVM implemented?

I found a class of the future where it describes the future at a high level in Java lang, I try to find where it is described at a lower level.

To summarize, it would be interesting to find the actual implementation of Future and Callable, for example: the part of the JVM that handles Future.get () or Callable.call () and tells them how they should work.

Waiting for your answers, Akonkagva

+6
source share
3 answers

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 (e.g. Runnable ) is a simple interface that you implement yourself. It completes the task that you want ExecutorService thread pools to execute. You must download the source banks for these classes and take a look at the Java code yourself.

None of these classes contain any JVM black magic or anything else. For example, if you build the Callable class, it will not run in another thread unless you send it to the thread pool. You can use Callable in many different places that have nothing to do with streams.

The black magic JVM around Future and Callable is mainly contained in the Thread class. It contains basic support that does the current job of completing a 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 natural calls that are real magic.

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

+7
source

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

If you are interested in learning how such things are implemented, it may also be interesting to see. Normally, Guava code is very well written.

+4
source

Future is an interface. It itself has no implementation; it simply indicates the method signatures. You can check the source of any class that implements this interface. Some public classes bundled with the JVM:

You can use grepcode to see their implementation.

+3
source

All Articles