title: how not to overwhelm java executorservice task queue?
body: <p>I have the below code snippet, which runs fine. But the problem is it creates and put over 2000 tasks on the executor queue right off the bat. </p>
  
  <p>I need to have a check if the tasks already in the executor queue are complete, only then give it more tasks. It doesnt have to be exact, ie if the queue has &lt;10 tasks left, add 50 more.</p>
  
  <p>So the executor task queue doesnt have so many pending tasks, which will also allow shutdown() to work in a timely manner, otherwise even if called, the executor will still trying to complete all 2000 tasks in its queue first.</p>
  
  <p>What is the best way to accomplish this?  thank you</p>
  
  <pre><code>executor = Executors.newFixedThreadPool(numThreads);
  
  while(some_condition==true)
  {
      //if(executor &lt; 10 tasks pending)  &lt;---- how do i do this?
      //{                             
          for(int k=0;k&lt;20;k++)
          {  
              Runnable worker = new MyRunnable();
              executor.execute(worker);
          }
      //}
      //else 
      //{
      //      wait(3000);
      //}
  } 
  </code></pre>
  
  <hr>
  
  <p>Update using semaphore: </p>
  
  <pre><code>private final Semaphore semaphore = new Semaphore(10)
  executor = new ThreadPoolExecutorWithSemaphoreFromJohnExample();
  
  while(some_condition==true)
  {
  
          Runnable worker = new MyRunnable();
          //So at this point if semaphore is full, then while loop would PAUSE(??) until
          //semaphore frees up again.
            executor.execute(worker);   
  } 
  </code></pre>