Producer Consumer using Lock and Condition


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package producerconsumer_lock_condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mahi
 */
public class ProducerConsumer_Lock_Condition {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        Stock myStock = new Stock();
        
        Producer p1 = new Producer(myStock);
        Producer p2 = new Producer(myStock);
        Consumer c1 = new Consumer(myStock);
        Producer p3 = new Producer(myStock);
        
        p1.start();
        //p2.start();
        c1.start();
        p3.start();
    }
    
}

class Stock {
    private int QOH=0;
    private final Lock myLock = new ReentrantLock();
    private final Condition myCondition = myLock.newCondition();
    
    public void setQOH(int amount){
    
        myLock.lock();
        System.out.println("Now the QOH variable is lock for adding stock");
        System.out.println("Now adding " + amount + " to QOH");
        
        try{
            if(amount > 0){
                this.QOH += amount;
                myCondition.signalAll();
            }
        }
        finally{
            myLock.unlock();
        }
    }
    
    public void getQOH(int reduceAmount){
    
        myLock.lock();
        System.out.println("QOH is lock for redusing stock");
        System.out.println("Now redusing " + reduceAmount + " from QOH");
        
        System.out.println("Now QOH is  -: "+ this.QOH);
        
        try{
            while(reduceAmount > QOH){
                System.out.println("Requested amount is > available stock");
                System.out.println("Now Consumer is waiting....");
                try {
                    myCondition.await();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Stock.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(QOH >= reduceAmount){
                QOH -= reduceAmount;
                System.out.println("Consumes " + reduceAmount + " from QOH");
                System.out.println("Now the QOH is " + QOH);
            }
        }
        finally{
            myLock.unlock();
        }
    }
    
}

class Producer extends Thread{

    private Stock myStock;
   
    //public Producer(String proString, int produceAmount){
    public Producer(Stock st){
        this.myStock = st;
    }

    @Override
    public void run(){
        myStock.setQOH(50);
    }
}

class Consumer extends Thread{
    private Stock myStock;
    
    private String proName;
    private int consumeAmount;
    
    public Consumer(Stock st){
        this.myStock = st;
    }

    @Override
    public void run(){
        myStock.getQOH(60);
    }
}


EmoticonEmoticon