title: Insert into Database when all the tasks of ThreadPoolExecutor have finished executing there jobs
body: <p><strong>Problem Definiton:-</strong></p>
  
  <p>I need to <code>insert some values into Database</code> as soon as all the tasks of <code>ExecutorService</code> have finished exectuing there jobs. In other words, I can <code>insert into database</code> only when all the tasks have finished executing there jobs as the things that I need to insert into db, is dependent on all the threads finishing there tasks..</p>
  
  <p><strong>So how I can check whether all the tasks of <code>ExecutorService</code> have been finished executing and then start inserting into database.</strong></p>
  
  <p>Below is the code in which I am creating task using <code>ThreadPoolExecutor</code>.</p>
  
  <pre><code>executorService = new ThreadPoolExecutor(
      noOfThreads, 
      noOfThreads, 
      500L, 
      TimeUnit.MILLISECONDS,
      new ArrayBlockingQueue&lt;Runnable&gt;(noOfThreads), 
      new ThreadPoolExecutor.CallerRunsPolicy()
  );
  
  
  // Running for particular duration of time
  while(System.currentTimeMillis() &lt;= endTime) {
      Command newCommand = getNextCommand();
      Task nextRunnable = new Task(newCommand, existId, newId);
      executorService.submit(nextRunnable); // Submit it for execution
  }
  
  /* 
      Previously I was inserting into database here, but it was wrong as it might be 
      possible that some threads are still in progress. And If I am inserting right here 
      into database, then some information I will be loosing for sure. So how can I check 
      whether all the tasks have finished executing and then I can insert into database.
  */      
  executorService.shutdown();
  if (!executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS)) {
      executorService.shutdownNow();
  }
  </code></pre>
  
  <p>The code that I will be using for insertion is below-</p>
  
  <pre><code>// Inserting into Database when all the task have finished executing, currently I
  for (Entry&lt;Integer, LinkedHashMap&lt;Integer, String&gt;&gt; entry : GUID_ID_MAPPING.entrySet()) {
      pstatement = db_connection.prepareStatement(LnPConstants.UPSERT_SQL);
      pstatement.setInt(1, entry.getKey());
      pstatement.setString(2, entry.getValue().get(LnPConstants.CGUID_ID));
      pstatement.setString(3, entry.getValue().get(LnPConstants.PGUID_ID));
      pstatement.executeUpdate();
  }
  </code></pre>
  
  <p>So I need to put this for loop somewhere after all the tasks have finished executing.</p>
  
  <p><strong>Update:-</strong></p>
  
  <p>So Something like this-</p>
  
  <pre><code>executorService.shutdown();
  if (!executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS)) {
      executorService.shutdownNow();
  }
  
  // Now Insert into Database when all the task have finished executing
  for (Entry&lt;Integer, LinkedHashMap&lt;Integer, String&gt;&gt; entry : GUID_ID_MAPPING.entrySet()) {
      pstatement = db_connection.prepareStatement(PDSLnPConstants.UPSERT_SQL);
      pstatement.setInt(1, entry.getKey());
      pstatement.setString(2, entry.getValue().get(PDSLnPConstants.CGUID_ID));
      pstatement.setString(3, entry.getValue().get(PDSLnPConstants.PGUID_ID));
      pstatement.executeUpdate();
  }
  </code></pre>