Producer Consumer Problem > Java Program

Producer Consumer Problem > Java Program

Operating Systems


import java.util.Vector;


public class S3 {



    public static void main(String args[]) {
        Vector v = new Vector();
        int size = 4;
        Thread p = new Thread(new Producer(v, size), "Producer");
        Thread c = new Thread(new Consumer(v, size), "Consumer");
        p.start();
        c.start();
    }
}

class Producer implements Runnable {


    private final Vector v;

    private final int SIZE;

    public Producer(Vector v, int size) {

        this.v = v;
        this.SIZE = size;
    }

    public void run() {

        for (int i = 0; i < 9; i++) {
            System.out.println("Produced: " + i);
            try {
                produce(i);
            } catch (InterruptedException ex) {
            
            }

        }

    }

    private void produce(int i) throws InterruptedException {


        //wait if queue is full

        while (v.size() == SIZE) {
            synchronized (v) {
                System.out.println("Queue is full " + Thread.currentThread().getName()
                                    + " is waiting , size: " + v.size());

                v.wait();

            }
        }

        //producing element and notify consumers

        synchronized (v) {
            v.add(i);
            v.notifyAll();
        }
    }
}

class Consumer implements Runnable {


    private final Vector v;

    private final int SIZE;

    public Consumer(Vector v, int size) {

        this.v = v;
        this.SIZE = size;
    }

      public void run() {

        while (true) {
            try {
                System.out.println("Consumed: " + consume());
                Thread.sleep(50);
            } catch (InterruptedException ex) {
               // Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

    private int consume() throws InterruptedException {

        //wait if queue is empty
        while (v.isEmpty()) {
            synchronized (v) {
                System.out.println("Queue is empty " + Thread.currentThread().getName()
                                    + " is waiting , size: " + v.size());

                v.wait();

            }
        }

        //Otherwise consume element and notify waiting producer

        synchronized (v) {
            v.notifyAll();
            return (Integer) v.remove(0);
        }
    }
}

/*Output:

Produced: 0
Produced: 1
Produced: 2
Produced: 3
Produced: 4
Queue is full Producer is waiting , size: 4
Consumed: 0
Produced: 5
Queue is full Producer is waiting , size: 4
Produced: 6
Queue is full Producer is waiting , size: 4
Consumed: 1
Consumed: 2
Produced: 7
Queue is full Producer is waiting , size: 4
Produced: 8
Queue is full Producer is waiting , size: 4
Consumed: 3
Consumed: 4
Consumed: 5
Consumed: 6
Consumed: 7
Consumed: 8
Queue is empty Consumer is waiting , size: 0
*/

Comments

Popular posts from this blog

Intermediate Code Generation > C Program