title: How to handle RejectedExecutionException with ThreadPoolExecutor in java
body: <p>What is the best way to handle <code>RejectedExecutionException</code> while using a <code>ThreadPoolExecutor</code> in Java?</p>
  
  <p>I want to ensure that the task submitted should not be overlooked and should surely get executed. As of now there are no hard real time requirements to get the task done.</p>
  
  <p>One of the things I thought could be done was waiting in a loop till I know that there is space in the runnable queue, and then go on and add it to the queue.</p>
  
  <p>Would be glad if people can share their experiences.</p>
  
  <p>Adding the possible solution I though of:</p>
  
  <pre><code>while(executor.getQueue().remainingCapacity &lt;= 0){
  // keep looping
  Thread.sleep(100);
  };
  //if the loop exits ,indicates that we have space in the queue hence 
  //go ahead and add to the queue 
  executor.execute(new ThreadInstance(params));
  </code></pre>