title: Wait until child Thread Finishe, But Do Not Block Main Thread
body: <p>I have been working on a java GUI application that on a particular action do some task. I have been doing this task using ExecutorService. Now the working with multiple threads works fine but the problem is that I do not want to block my GUI. User may want to cancel the current operation and may request for another one. But while using the ExecutorService, my main Thread gets blocked. I want to wait for my child threads to finish, invoked using ExecutorService, while still being able to work on GUI.</p>
  
  <p>The updated code is:</p>
  
  <pre><code>    ExecutorService child = Executors.newCachedThreadPool();
  ExecutorService es = Executors.newCachedThreadPool();
  
  @Action
  public void doAction() {
      /** Some Code goes here */
      if(Condition){
          if(!(es.isTerminated()||es.isShutdown())){
              es.shutdownNow();
              es = Executors.newCachedThreadPool();
          }
          es.execute(new Runnable() {
              public void run() {
                  if(!(child.isTerminated()||child.isShutdown())){
                      child.shutdownNow();
                      child = Executors.newCachedThreadPool();
                  }
                  for(loopConditions){
                      child.execute(new Runnable() {
                          public void run() {
                              //Perform Some Task Here
                          }
                      });
                  }
                  child.shutdown();
                  try {
                      boolean finshed = child.awaitTermination(5, TimeUnit.MINUTES);
                  } catch (InterruptedException ex) {
                      child.shutdownNow();
                      Logger.getLogger(MySearchView.class.getName()).log(Level.SEVERE, null, ex);
                  }
                  System.out.println("All Child Threads finished Execution");
              }
          });
          es.shutdown();
          try {
              boolean finshed = es.awaitTermination(5, TimeUnit.MINUTES);
          } catch (InterruptedException ex) {
              es.shutdownNow();
              Logger.getLogger(MySearchView.class.getName()).log(Level.SEVERE, null, ex);
          }
          System.out.println("All Threads finished Execution");
          /**
          * Code that should run after all Threads finishes their Execution
          */
      }
  }
  </code></pre>