title: JavaFX - waiting for task to finish
body: <p>I have a JavaFX application which instantiates several <a href="http://docs.oracle.com/javafx/2/api/javafx/concurrent/Task.html">Task</a> objects.</p>
  
  <p>Currently, my implementation (see below) calls the behavior <strong>runFactory()</strong> which performs computation under a Task object. Parallel to this, <strong>nextFunction()</strong> is invoked. Is there a way to have <strong>nextFunction()</strong> "wait" until the prior Task is complete?</p>
  
  <p>I understand <strong>thread.join()</strong> waits until the running thread is complete, but with GUIs, there are additional layers of complexity due to the event dispatch thread.
  As a matter of fact, adding <strong>thread.join()</strong> to the end of the code-segment below only ceases UI interaction.</p>
  
  <p>If there are any suggestions how to make <strong>nextFunction</strong> wait until its prior function, <strong>runFactory</strong> is complete, I'd be very appreciative.</p>
  
  <p>Thanks,</p>
  
  <pre><code>// High-level class to run the Knuth-Morris-Pratt algorithm.
  public class AlignmentFactory {
      public void perform() {
          KnuthMorrisPrattFactory factory = new KnuthMorrisPrattFactory();
          factory.runFactory();   // nextFunction invoked w/out runFactory finishing.
          // Code to run once runFactory() is complete.
          nextFunction()   // also invokes a Task.
          ...
      }
  }
  
  // Implementation of Knuth-Morris-Pratt given a list of words and a sub-string.
  public class KnuthMorrisPratt {
     public void runFactory() throws InterruptedException {
         Thread thread = null;
         Task&lt;Void&gt; task = new Task&lt;Void&gt;() {
  
             @Override public Void call() throws InterruptedException {
             for (InputSequence seq: getSequences) {
                  KnuthMorrisPratt kmp = new KnuthMorrisPratt(seq, substring);
                  kmp.align();
  
              }
              return null; 
          }
      };
      thread = new Thread(task);
      thread.setDaemon(true);
      thread.start();
  }
  </code></pre>