title: How does a Callable work under the hood? How is it possible for a callable object to return a value?
body: <p>I am trying to understand how a <code>Callable</code> is able to return a value when it is run on a different thread. </p>
  
  <p>I am looking in the classes <code>Executors</code>, <code>AbstractExecutorService</code>, <code>ThreadPoolExecutor</code> and <code>FutureTask</code>, all available in <code>java.util.concurrent</code> package.  </p>
  
  <p>You create an ExecutorService object by calling a method in Executors (e.g. <code>newSingleThreadExecutor()</code>). Then you can pass a Callable object with  <code>ExecutorService.submit(Callable c)</code>.</p>
  
  <p>Since the <code>call()</code> method is run by a thread provided by the <code>ExecutorService</code>, where does the returned object "jump" back to the calling thread?</p>
  
  <p>Look at this simple example:</p>
  
  <pre><code>1    ExecutorService executor = Executors.newSingleThreadExecutor();
  2    public static void main(String[] args) {
  3       Integer i = executor.submit(new Callable&lt;Integer&gt;(){
  4           public Integer call() throws Exception {
  5              return 10;
  6           }
  7       }).get();
  8       System.out.print("Returns: " + i + " Thread: " + Thread.currentThread.getName());
  9       // prints "10 main"
  10    }
  </code></pre>
  
  <p>How is it possible that the integer in the call method, which is run by a separate thread, is returned to the Integer object (row 3) so it can be printed by the <code>System.out</code> statement in the main thread (row 7)?</p>
  
  <p>Isn´t it possible for the main thread to be run before the <code>ExecutorService</code> has run its thread, so that the <code>System.out statement</code> prints null?</p>