title: Java: return results from Runnable
body: <p>Suppose the following simplified example. Let B represent a class processing some raster data:</p>
  
  <pre><code>import java.awt.image.BufferedImage;
  
  public class B implements Runnable{
      private boolean c;
      private Runnable f;
  
      public B (boolean c_, Runnable f_) { c = c_; f = f_;}
  
      public BufferedImage process() {
              //Some operations
              BufferedImage output = null;
              if (c) output = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
              return output;
      }
  
      public void run() { process();}
  }
  </code></pre>
  
  <p>The <strong>process()</strong> method may but may not create an output rater. Due to the computational cost, the procedure runs in a separate thread.</p>
  
  <p>Let A represent a class inside which the procedure will be run. It also contains some post process steps waiting until the thread is finished:</p>
  
  <pre><code>import java.awt.image.BufferedImage;
  
  public class A {
      public A(){}
      public void compute() {
              boolean c = true;
              B b = new B( c, new Runnable() {
                      public void run() {
                              //Post process, after the thread has been finished
                              BufferedImage img = ??? //Get resulting raster, how?
                              if (img != null) {
                                      //Perform some steps
                              }
                      }
              });
  
              Thread t = new Thread(b);
              t.start ();  //Run  procedure
      }
  }
  </code></pre>
  
  <p>However, how to get resulting raster created using the <strong>process()</strong> method of B "inside" the <strong>run()</strong> method in A? </p>
  
  <p>Avoid the model, when the output image represents a data member of B together with </p>
  
  <pre><code>b.getImage();
  </code></pre>
  
  <p>I read a post about callbacks </p>
  
  <p><a href="https://stackoverflow.com/questions/32771412/return-value-from-runnable">Return value from Runnable</a></p>
  
  <p>but how to implement it here? Thanks for your help and a short example.</p>