title: Java: notify main class when all threads in threadpool are finished / same instance of object in different threads
body: <p>How do I notify my main class which instantiates a <code>ThreadPoolExecutor</code> when all threads within the <code>ThreadPoolExecutor</code> are completed?</p>
  
  <pre><code>ThreadPoolExecutor threadPool = null;
  ThreadClass threadclass1;
  ThreadClass threadclass2;
  final ArrayBlockingQueue&lt;Runnable&gt; queue = new ArrayBlockingQueue&lt;Runnable&gt;(maxPoolSize);
  
  puclic MyClass(){
          threadPool = new ThreadPoolExecutor(poolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, queue);
  
          threadClass1 = new ThreadClass;
          threadClass2 = new ThreadClass;
  
          threadPool.execute(threadClass1);
          threadPool.execute(threadClass2);
  
          //Now I would like to do something until the threadPool is done working
          //The threads fill a ConcurrentLinkedQueueand I would like to poll
          //the queue as it gets filled by the threads and output 
          //it to XML via JAX-RS
  
  }
  </code></pre>
  
  <h2>EDIT 1</h2>
  
  <p>Wile my threads fetch data from somewhere and fill this information into a ConcurrentLinkedQueue I basically would like to perform some action in MyClass to update the XML output with the results. When all threads are terminated I would like to return true to the JAX-RS webservice which instantiated MyClass so the webservice knows all data has been fetched and it can now display the final XML file</p>
  
  <h2>EDIT 2</h2>
  
  <p>I am passing a <code>Queue</code> to threads so they can add items to the queue. When one <code>driver</code> is done adding items to the <code>articleQueue</code> I want to perform an action within my main class, polling the entity from the <code>Queue</code> and handing it over to the <code>response</code> object to display it in some way.</p>
  
  <p>When I pass the queue to the threads, are they working with the same object or with a "copy" of the object so that changes within the thread do not effect the main object? That is not the behavior I want. When I check the size of the <code>articleQueue</code> within the <code>Driver</code> it is <code>18</code>, the size of the <code>articleQueue</code> in the <code>DriverController</code> is <code>0</code>.</p>
  
  <p>Is there a nicer way to react when a thread has added something to the queue other than my while loop? How do I have to modify my code to acces the same object within different classes?</p>
  
  <h2>DriverController</h2>
  
  <pre><code>public class DriverController {
  
      Queue&lt;Article&gt; articleQueue;
  
      ThreadPoolExecutor threadPool = null;
      final ArrayBlockingQueue&lt;Runnable&gt; queue = new ArrayBlockingQueue&lt;Runnable&gt;(
              maxPoolSize);
  
      public DriverController(Response response) {
  
          articleQueue = new ConcurrentLinkedQueue&lt;Article&gt;();
          threadPool = new ThreadPoolExecutor();
          Driver driver = new Driver(this.articleQueue);
  
          threadPool.execute(driver);
          // More drivers would be executed here which add to the queue
  
          while (threadPool.getActiveCount() &gt; 0) {
              // this.articleQueue.size() gives back 0 here ... why?
              if(articleQueue.size()&gt;0){
                  response.addArticle(articleQueue.poll());
              }
          }
  
      }
  }
  </code></pre>
  
  <h2>Driver</h2>
  
  <pre><code>public class Driver implements Runnable{
  
      private Queue&lt;Article&gt; articleQueue;
  
      public DriverAlliedElectronics(Queue articleQueue) {
          this.articleQueue = articleQueue;
      }
  
      public boolean getData() {
          // Here would be the code where the article is created ...
  
          this.articleQueue.offer(article);
          return true;
      }
  
      public void run() {
          this.getData();
          // this.articleQueue.size() gives back 18 here ...
  
      }
  }
  </code></pre>