I am trying to write a mail utility that places mails in a queue, and it is later consumed by a consumer thread.
I am trying to implement a typical producer-consumer
ExecutorService.submit schedules a Runnable or Callable for one execution. Your output shows that MailProducer and MailConsumer both executed once, so everything works like it should.
You should place the inside of your Producer and Consumer methods in loops:
import java.util.concurrent.*;
public class Executor {
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");
}
static class MailProducer implements Runnable {
@Override
public void run() {
while (true) {
System.out.println("inside mail Producer");
System.out.println("Thread executing = " +
Thread.currentThread().getName());
}
}
}
static class MailConsumer implements Runnable {
@Override
public void run() {
while (true) {
System.out.println("inside mail Consumer");
System.out.println("Thread executing = " +
Thread.currentThread().getName());
}
}
}
}
This gives the output you expect.