coderbot

Infinite Buffer Producer Consumer Problem Java

Jul 15th, 2014
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. /*
  2.     Author: Jatin Thakur
  3.         coderbots.blogspot.com
  4. */
  5.  
  6. import java.util.Arrays;
  7. import java.util.concurrent.*;
  8.  
  9. class Consumer implements Runnable {
  10.  
  11.     private final Scenario scenario;
  12.  
  13.     Consumer(Scenario s) {
  14.         scenario = s;
  15.         Arrays.fill(buffer, 0);
  16.     }
  17.  
  18.     int buffer[] = new int[10];
  19.  
  20.     volatile int current = 0;
  21.     volatile int total = 0;
  22.  
  23.     public void run() {
  24.         try {
  25.             while (!Thread.interrupted()) {
  26.                 synchronized (this) {
  27.                     while (buffer[current] == 0) {
  28.                         wait();
  29.                     }
  30.                 }
  31.                 synchronized (scenario.producer) {
  32.                     System.out.println("Consuming item: " + current);
  33.                     buffer[current++] = 0;
  34.  
  35.                     ++total;
  36.                     scenario.producer.notifyAll();
  37.                 }
  38.                 if (current == 10) {
  39.                     current = 0;
  40.                 }
  41.                 Thread.yield();
  42.             }
  43.         } catch (InterruptedException e) {
  44.             System.out.println("Consumer interrupted!");
  45.  
  46.         }
  47.     }
  48. }
  49.  
  50. class Producer implements Runnable {
  51.  
  52.     volatile int total = 0;
  53.     private final Scenario scenario;
  54.  
  55.     Producer(Scenario s) {
  56.         scenario = s;
  57.     }
  58.     volatile int current = 0;
  59.  
  60.     public void run() {
  61.         try {
  62.             while (!Thread.interrupted()) {
  63.                 synchronized (this) {
  64.                     while (scenario.consumer.buffer[(current == 9 ? -1 : current) + 1] == 1) {
  65.                         wait();
  66.                     }
  67.                 }
  68.                 synchronized (scenario.consumer) {
  69.                     System.out.println("Producing item: " + current);
  70.                     scenario.consumer.buffer[current++] = 1;
  71.                     scenario.consumer.notifyAll();
  72.  
  73.                     ++total;
  74.                 }
  75.                 if (current == 10) {
  76.                     current = 0;
  77.                 }
  78.                 Thread.yield();
  79.             }
  80.         } catch (InterruptedException e) {
  81.             System.out.println("Producer interrupted!");
  82.         }
  83.     }
  84. }
  85.  
  86. public class Scenario {
  87.  
  88.     Consumer consumer = new Consumer(this);
  89.     Producer producer = new Producer(this);
  90.  
  91.     ExecutorService exec = Executors.newCachedThreadPool();
  92.  
  93.     Scenario() {
  94.         exec.execute(consumer);
  95.         exec.execute(producer);
  96.     }
  97.  
  98.     public static void main(String[] args) throws InterruptedException {
  99.         Scenario sc = new Scenario();
  100.         TimeUnit.MILLISECONDS.sleep(5);
  101.         sc.exec.shutdownNow();
  102.         System.out.println("Total items produced: " + sc.producer.total);
  103.         System.out.println("Total items consumed: " + sc.consumer.total + "\n\n");
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment