Producer Consumer using SemApp


  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
119
120
121
122
123
package producerconsumersemapp;

import com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException;
import java.util.Random;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Student
 */
public class ProducerConsumerSemApp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Stock s = new Stock();
        Producer p1 = new Producer(s, "Producer I");
        Consumer c1 = new Consumer(s, "Consumer I");
        
        p1.start();
        c1.start();
    }
    
}

class Stock{
    
    private int  QOH = 0;
    static Semaphore semCon = new Semaphore(0);
    static Semaphore semPro = new Semaphore(1);

    public void setQOH(int a){
        
        
        //System.out.println("\nQuentity in hand " + QOH + "Produce " + a);
        
        try{
            semPro.acquire();
            System.out.println("Producer aquir the lock...");
            
        }
        catch(InterruptedException e) {
            System.out.println("InterruptedException");
        
        }
        QOH +=a;  
        System.out.println("Producer produce : "+ a + "Now in had " + QOH);
        semPro.release();
        System.out.println("Sempho release the lock....");
            
    }
    
    public void getQOH(int a){
        //System.out.println("\nQuentity in hand " + QOH + "Produce " + a);
        
        try{
            semCon.acquire();
            System.out.println("Consumer aquir the lock...");
            //System.out.println("Cunsumer ait for the quntity to be produc /n");
            if(isAvailable(a)){
                QOH -= a;
                System.out.println("Consumer consumes " + a + "Now in had : "+ QOH);
            }    
            
        }catch(InterruptedException e){
           System.out.println("InterruptedException");
        }
        semPro.release();
        System.out.println("Consumer release the lock....");
    }
    
    public boolean isAvailable(int x){
        if(QOH> x){
            return true;
        }
        else return true;
    }


}

class Producer extends Thread{
    Stock stock = null;
    private static Random generator = new Random();
    private String proName;
    
    public Producer(Stock s, String pname){
    this.stock = s;
    this.proName = pname;
    }

    public void run(){
    for(int i=0; i<10; i++){
    stock.setQOH(generator.nextInt(2000));
    }

    }

}


class Consumer extends Thread{
    Stock stock = null;
    private String cname;
    private static Random generator = new Random();
    
    public Consumer(Stock s, String cname){
    this.stock = s;
    this.cname = cname;
    }
    @Override
    public void run(){
        for(int i=0; i<5; i++){
        stock.getQOH(generator.nextInt(2000));
    }

    }

}


EmoticonEmoticon