title: Producer-Consumer with ExecutorService.newFixedThreadPool - How many threads are created?
body: <pre><code>public class MainClass {
  
  private static final int producerPoolSize = 10;
  private static final int consumerPoolSize = 20;    
  
  private ExecutorService prodExec = Executors.newFixedThreadPool(producerPoolSize);
  private ExecutorService consExec = Executors.newFixedThreadPool(consumerPoolSize);
  
  //main method here, which calls start() below
  
  private void start(String[] args) {
  
      // Get list of ids, split them in to n(producerPoolSize) chunks
  
      for (int index = 0; index &lt; producerPoolSize; index++) {
          Runnable producer = new Producer(consExec, chunkOfIdsForThisProducer);
          prodExec.execute(producer);
      }
  
  }
  
  public class Producer implements Runnable {
  
      private ExecutorService consExec;
      private List&lt;Long&gt; list;
  
      public Producer(ExecutorService exec, List&lt;Long&gt; list) {
          this.consExec = exec;
          this.list = list;
      }
  
      public void run() {
  
         for (Long id: list) {
            data = get data from db for the id
            consExec.execute(new Consumer(data));
         }
      }
   }
  
   public class Consumer implements Runnable {
  
       public void run() {
          // call web service
       }
   }
  </code></pre>
  
  <p>In the above code, I have two thread pools - one each for Producers and Consumers. I get a number of IDs from the database,split them in to equal chunks so that they are handed out to Producer threads to process. A producer thread receives a list of IDs and processes each sequentially, retrieving data for each of of the IDs and submitting that data to a Consumer thread to process. Now my question is this:</p>
  
  <p>I create 10 producer threads above. And I want the size of the Consumer thread pool to be 20. But, while processing each ID, the Producer creates a new Runnable (Consumer) and submits (execute) it to the Consumer executor service. My understanding of the ExecutorService is that the Runnable that you submit to it,gets wrapped in a Worker thread and then executed. So, in the above code, if the number of IDs each producer gets is 50, am I actually creating 50*10=500 Consumer threads? Is it too many? </p>
  
  <p>Or does the pool size actually means the number of worker threads? So in the above code I am creating 500 tasks on the Consumer executor which would actually be queued and executed by 20 worker threads? I may not be explaining this correctly, but slightly confused here around the internal implementation of the executor and worried if I am creating too many Consumer threads.</p>
  
  <p>If this isn't the way to implement this, can someone suggest a better approach? Thanks.</p>