title: Implementing Producer consumer pattern
body: <p>I am trying to write a mail utility that places mails in a queue, and it is later consumed by a consumer thread.</p>
  
  <p>I am trying to implement a typical producer-consumer pattern, but something is going wrong.</p>
  
  <p>I just wrote a skeleton , and the skeleton is not working as expected.</p>
  
  <p><strong>MailProducer.java</strong></p>
  
  <pre><code>public class MailProducer implements Callable&lt;Void&gt;
   {
  
  @Override
  public Void call() throws Exception
  {
      System.out.println("inside mail Producer");
      System.out.println("Thread executing = " +
                             Thread.currentThread().getName());
      return null;
  }
  
  }
  </code></pre>
  
  <p><strong>MailConsumer.java</strong></p>
  
  <pre><code>public class MailConsumer implements Callable&lt;Void&gt;
  {
  
  @Override
  public Void call() throws Exception
  {
      System.out.println("inside mail consumer");
      System.out.println("Thread executing = " + 
                          Thread.currentThread().getName());
      return null;
  }
  
   }
  </code></pre>
  
  <p>and finally the Executor</p>
  
  <p><strong>MailExecutor.java</strong></p>
  
  <pre><code>  public class MailExecutor
    {
  
  private static final int NTHREADS = 25;
  private static final ExecutorService exec = 
                  Executors.newFixedThreadPool(NTHREADS);
  
  public static void main(String[] args)
  {
      exec.submit(new MailConsumer());
      exec.submit(new MailProducer());
  
      System.out.println("inside main");
  
  }
  
    }
  </code></pre>
  
  <p>Now when I run the program, <strong>I expect it to go back and forth the producer and consumer to keep printing what is written in the respective classes</strong>. But instead , the program hangs/does nothing after printing the below lines . What is going wrong ? Am I missing something?</p>
  
  <p><strong>Output</strong>   ...(Output is not what I had expected. What is going wrong ?)</p>
  
  <pre><code>   inside mail consumer
     inside main
     Thread executing = pool-1-thread-1
     inside mail Producer
     Thread executing = pool-1-thread-2
  </code></pre>