Advertisement
Shailrshah

Producer-Consumer Problem

Oct 23rd, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. import java.util.Random;
  2. import java .util.*;
  3. import java.util.concurrent.Semaphore;
  4. class QUsingSemaphore{
  5.    private boolean isProduced = false;
  6.    private final Semaphore bsemaphore = new Semaphore(1);
  7.    private static int n;
  8.    public synchronized boolean isProduced(){
  9.       return isProduced;
  10.    }
  11.    public synchronized void setProduced(boolean isProduced){
  12.       this.isProduced = isProduced;
  13.    }
  14.    public void produce(int n, int id){
  15.       try{
  16.          if((!isProduced() || this.n!=10) && n > 0){
  17.             bsemaphore.acquire();
  18.             setN(n);
  19.             System.out.println("Produced : "+1+" Item by P"+id+"\nStock="+n+"\n");
  20.             setProduced(true);
  21.          }
  22.       }
  23.       catch(InterruptedException ie){
  24.          ie.printStackTrace();
  25.       }
  26.       finally{
  27.          bsemaphore.release();
  28.       }
  29.       try{
  30.          Thread.sleep(new Random().nextInt(5)*1000);
  31.       }
  32.       catch(InterruptedException ie){
  33.          ie.printStackTrace();
  34.       }
  35.    }
  36.    public void consume(int id){
  37.       try{
  38.             if(isProduced()){
  39.                bsemaphore.acquire();
  40.                int temp=resetN();
  41.                System.out.println("Consumed : "+1+" Item by C"+id+"\nStock="+temp+"\n");
  42.                if(temp==0)
  43.                   setProduced(false);
  44.             }
  45.       }
  46.       catch(InterruptedException ie){
  47.          ie.printStackTrace();
  48.       }
  49.       finally{
  50.          bsemaphore.release();
  51.       }
  52.       try{
  53.          Thread.sleep(new Random().nextInt(5)*1000);
  54.       }
  55.       catch(InterruptedException ie){
  56.          ie.printStackTrace();
  57.       }
  58.    }
  59.    public synchronized int resetN(){
  60.       this.n--;
  61.       return this.n;
  62.    }
  63.    public synchronized int getN(){
  64.       return n;
  65.    }
  66.    public synchronized void setN(int n){
  67.       this.n= n;
  68.    }
  69. }
  70. class Consumer implements Runnable{
  71.    private final QUsingSemaphore q;
  72.    int id;
  73.    public Consumer(QUsingSemaphore q, int id){
  74.       this.q=q;
  75.       this.id=id;
  76.    }
  77.    public void run(){
  78.       while(true)
  79.          q.consume(id);
  80.    }  
  81. }
  82. class Producer implements Runnable{
  83.    private final QUsingSemaphore q;
  84.    int id;
  85.    static int count=0;
  86.    public Producer(QUsingSemaphore q, int id){
  87.       this.q=q;
  88.       this.id=id;
  89.    }
  90.    public void run(){
  91.       while(count++ < 20){
  92.          int j = q.getN();
  93.          q.produce(++j, id);
  94.          System.out.println("Total items  produced: "+count+"\n");
  95.       }
  96.       System.out.println("20 items produced. Now exiting.");
  97.       System.exit(0);
  98.    }
  99. }
  100. class ProducerConsumerTest{
  101.    public static void main(String args[]){
  102.       //Scanner sc=new Scanner(System.in);
  103.       //System.out.println("enter the no. of producers and consumers one by one");
  104.       int np=2, nc=2, i, j;
  105.       QUsingSemaphore q = new QUsingSemaphore();
  106.       for(i=1; i<=np; i++) new Thread(new Producer(q, i)).start();;
  107.       for (i=1; i<=nc; i++ ) new Thread(new Consumer(q,i)).start();;
  108.    }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement