Actual implementation of Callable and Future

16,971

Solution 1

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.

Solution 2

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.

Solution 3

Future is an interface. It has no implementation in itself, it just specify method signatures. You can check source of any of class that implements this interface. Some public classes bundled with JVM are:

You can use grepcode to see their implementation.

Share:
16,971
Peter
Author by

Peter

Updated on June 05, 2022

Comments

  • Peter
    Peter almost 2 years

    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

  • Gray
    Gray about 11 years
    This is confusing. Most interfaces have implementations and FutureTask is one such example.
  • Mikita Belahlazau
    Mikita Belahlazau about 11 years
    @Gray What is confusing? FutureTask is only one example. Other implementation may do it in different way and have different logic. E.g. standard JVM library misses future implemenation where you can set value like here: SettableFuture
  • Gray
    Gray about 11 years
    Saying that the Future interface does not have an implementation is confusing. In the same package there is an implementation which is used by the concurrent classes. Maybe you want to reword?
  • Mikita Belahlazau
    Mikita Belahlazau about 11 years
    @Gray ah, yes, it's confusing. Thank you
  • Gray
    Gray over 10 years
    No. A Future is just a job wrapper. If you have a thread-pool with 10 threads, you might submit 100k Future jobs to that thread-pool @raffian.