We would like to know how to implement Producer consumer pattern.
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /* w ww. ja v a 2s. c o m*/ public class Main { public static void main(String[] args) { int NTHREADS = 25; ExecutorService exec = Executors.newFixedThreadPool(NTHREADS); exec.submit(new MailConsumer()); exec.submit(new MailProducer()); System.out.println("inside main"); } } class MailConsumer implements Runnable { @Override public void run() { while (true) { System.out.println("inside mail Consumer"); System.out.println("Thread executing = " + Thread.currentThread().getName()); } } } class MailProducer implements Runnable { @Override public void run() { while (true) { System.out.println("inside mail Producer"); System.out.println("Thread executing = " + Thread.currentThread().getName()); } } }
The code above generates the following result.