Thursday 23 July 2015

Producer Consumer using wait(), notify and notifyAll()

Producer.java
import java.util.Vector;

public class Producer implements Runnable {
     private final Vector<Integer> sharedQueue;
     private final int SIZE;
     public Producer(Vector<Integer> sharedQueue, int size) {
          this.sharedQueue = sharedQueue;
          this.SIZE = size;
     }

     @Override
     public void run() {
          for (int i = 0; i < 10; i++) {
                 try {
                        produce(i);
                 } catch (InterruptedException e) {
                        e.printStackTrace();
                 }
          }
     }

     private void produce(int i) throws InterruptedException {
          while(sharedQueue.size()==SIZE) {
                synchronized (sharedQueue) {
                    System.out.println("Producer-->Shared Queue size is full.");
                    try {
                        sharedQueue.wait();
                    } catch (InterruptedException e) {
                        throw e;
                    }
                }
          }

          synchronized (sharedQueue) {
                if(sharedQueue.size()<SIZE) {
                     System.out.println("Producer:"+
                               Thread.currentThread().getName()+ ":"+i);
                     sharedQueue.add(i);
                     sharedQueue.notifyAll();
                }
          }
    }
}

Consumer.java
import java.util.Vector;

public class Consumer implements Runnable {
     private final Vector<Integer> sharedQueue;
     private final int SIZE;
     public Consumer(Vector<Integer> sharedQueue, int size) {
          this.sharedQueue = sharedQueue;
          this.SIZE = size;
     }

     @Override
     public void run() {
          for (int i = 0; i < 10; i++) {
              try {
                     consumer(i);
              } catch (InterruptedException e) {
                     e.printStackTrace();
              }
          }
     }

     private void consumer(int i) throws InterruptedException {
           while(sharedQueue.size()==0) {
                 synchronized (sharedQueue) {
                       System.out.println("Consumer-->Shared Queue size is empty.");
                       try {
                           // wait for shared queue to filled
                           // (will release sharedQueue resource)
                            sharedQueue.wait(); 
                       } catch (InterruptedException e) {
                            throw e;
                       }
                 }
           }
           synchronized (sharedQueue) {
                 if(sharedQueue.size()>0) {
                        System.out.println("Consumer:"+
                            Thread.currentThread().getName()+":"+sharedQueue.get(0));
                        sharedQueue.remove(0);
                        sharedQueue.notifyAll();
                 }
          }
    }
}

TestProdConsum.java
import java.util.Vector;

public class TestProdConsum {

     public static void main(String[] args) {
          final Vector<Integer> sharedQueue = new Vector<Integer>();
          int size = 4;
          Thread producer1 = new Thread(new Producer(sharedQueue, size), "p1");
          Thread consumer1 = new Thread(new Consumer(sharedQueue, size), "c1");
          Thread producer2 = new Thread(new Producer(sharedQueue, size), "p2");
          Thread consumer2 = new Thread(new Consumer(sharedQueue, size), "c2");
          producer1.start();
          consumer1.start();
          producer2.start();
          consumer2.start();

     }
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...