title: How to stop all the running threads, if one of those throws an Exception?
body: <p>In one of my application I'm using the <code>ExecutorService</code> class to create a fixed thread pool and <code>CountDownLatch</code> to wait for the threads to complete. This is working fine if the process didn't throw any exception . If there is an exception occurred in any of the threads, I need to stop all the running thread and report the error to the main thread. Can any one please help me to solve this? </p>
  
  <p>This is the sample code I'm using for executing multiple threads.</p>
  
  <pre><code>    private void executeThreads()
      {
          int noOfThreads = 10;
          ExecutorService executor = Executors.newFixedThreadPool(noOfThreads);     
          try      
         {
          CountDownLatch latch = new CountDownLatch(noOfThreads);
          for(int i=0; i&lt; noOfThreads; i++){
           executor.submit(new ThreadExecutor(latch));
          }
          latch.await();           
         }
         catch(Exception e)
         {
          e.printStackTrace();
         }
         finally
         {
          executor.shutDown();
         }
     }
  </code></pre>
  
  <p>This is the Executor Class</p>
  
  <pre><code>     public class ThreadExecutor implements Callable&lt;String&gt; {
          CountDownLatch latch ;
          public ThreadExecutor(CountDownLatch latch){
              this.latch = latch;
          }   
  
      @Override
      public String call() throws Exception
      {
          doMyTask(); // process logic goes here!
          this.latch.countDown();
          return "Success";
      }
  </code></pre>
  
  <p>=============================================================================</p>
  
  <p>Thank you all :)</p>
  
  <p>I have corrected my class as given below and that is working now.</p>
  
  <pre><code>private void executeThreads()
      {
          int noOfThreads = 10;
          ExecutorService executor = Executors.newFixedThreadPool(noOfThreads);     
         ArrayList&lt;Future&lt;Object&gt;&gt; futureList = new ArrayList&lt;Future&lt;Object&gt;&gt;(noOfThreads );
      try
      {
          userContext = BSF.getMyContext();
          CountDownLatch latch = new CountDownLatch(noOfComponentsToImport);
  
          for(ImportContent artifact:artifactList){
              futureList.add(executor.submit(new ThreadExecutor(latch)));
          }
  
          latch.await();
  
          for(Future&lt;Object&gt; future : futureList)
          {
                    try
                    {
                        future.get();                 
                     }
                     catch(ExecutionException e)
                     {   //handle it               
                      }
          }           
  
      }
      catch (Exception e) {
         //handle it
      }
      finally
      {
          executor.shutdown();      
  
          try
          {
              executor.awaitTermination(90000, TimeUnit.MILLISECONDS);
          }
          catch (InterruptedException e)
          {
             //handle it
          }
      }
     }
  </code></pre>
  
  <p>Executor Class :</p>
  
  <pre><code>public class ThreadExecutor implements Callable&lt;String&gt; {
          private static volatile boolean isAnyError;
          CountDownLatch latch ;
          public ThreadExecutor(CountDownLatch latch){
              this.latch = latch;
          }   
  
      @Override
      public String call() throws Exception
      {
  
        try{
              if(!isAnyError)
              { 
                 doMyTask(); // process logic goes here!
              }
       }
       catch(Exception e)
       {
          isAnyError = true ;
          throw e;
        }
        finally
        {
          this.latch.countDown();
         }
          return "Success";
      }
  </code></pre>