Java examples for Thread:Producer Consumer
Producer and consumer with two threads manipulating an unsynchronized buffer.
import java.security.SecureRandom; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; class Consumer implements Runnable { private static final SecureRandom generator = new SecureRandom(); private final Buffer sharedLocation; // reference to shared object public Consumer(Buffer sharedLocation) { this.sharedLocation = sharedLocation; }/*from ww w . j ava2s . c o m*/ // read sharedLocation's value 10 times and sum the values public void run() { int sum = 0; for (int count = 1; count <= 10; count++) { // sleep 0 to 3 seconds, read value from buffer and add to sum try { Thread.sleep(generator.nextInt(3000)); sum += sharedLocation.blockingGet(); System.out.printf("\t\t\t%2d%n", sum); } catch (InterruptedException exception) { Thread.currentThread().interrupt(); } } System.out.printf("%n%s %d%n%s%n", "Consumer read values totaling", sum, "Terminating Consumer"); } } interface Buffer { public void blockingPut(int value) throws InterruptedException; public int blockingGet() throws InterruptedException; } class UnsynchronizedBuffer implements Buffer { private int buffer = -1; // shared by producer and consumer threads public void blockingPut(int value) throws InterruptedException { System.out.printf("Producer writes\t%2d", value); buffer = value; } public int blockingGet() throws InterruptedException { System.out.printf("Consumer reads\t%2d", buffer); return buffer; } } class Producer implements Runnable { private static final SecureRandom generator = new SecureRandom(); private final Buffer sharedLocation; // reference to shared object public Producer(Buffer sharedLocation) { this.sharedLocation = sharedLocation; } // store values from 1 to 10 in sharedLocation public void run() { int sum = 0; for (int count = 1; count <= 10; count++) { try // sleep 0 to 3 seconds, then place value in Buffer { Thread.sleep(generator.nextInt(3000)); // random sleep sharedLocation.blockingPut(count); // set value in buffer sum += count; // increment sum of values System.out.printf("\t%2d%n", sum); } catch (InterruptedException exception) { Thread.currentThread().interrupt(); } } System.out.printf("Producer done producing%nTerminating Producer%n"); } } public class Main { public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); Buffer sharedLocation = new UnsynchronizedBuffer(); System.out.println("Action\t\tValue\tSum of Produced\tSum of Consumed"); System.out.printf("------\t\t-----\t---------------\t---------------%n%n"); // execute the Producer and Consumer, giving each // access to the sharedLocation executorService.execute(new Producer(sharedLocation)); executorService.execute(new Consumer(sharedLocation)); executorService.shutdown(); // terminate app when tasks complete executorService.awaitTermination(1, TimeUnit.MINUTES); } }