0% found this document useful (0 votes)
15 views4 pages

Class: ( ( (Valueset)

Uploaded by

shaikafrozu115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Class: ( ( (Valueset)

Uploaded by

shaikafrozu115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

22x51a3839 22357yfu

// A correct implementation of a producer and consumer.

class Q

int n;

boolean valueSet = false;

synchronized int get()

while(!valueSet)

try

wait();

catch(InterruptedException e)

System.out.println("InterruptedException caught");

System.out.println("Got: " + n);

valueSet = false;

notify();

return n;

synchronized void put(int n)

while(valueSet)

try

wait();
22x51a3839 22357yfu

catch(InterruptedException e)

System.out.println("InterruptedException caught");

this.n = n;

valueSet = true;

System.out.println("Put: " + n);

notify();

class Producer implements Runnable

Q q;

Producer(Q q)

this.q = q;

new Thread(this, "Producer").start();

public void run()

int i = 0;

while(true)

q.put(i++);

}
22x51a3839 22357yfu

class Consumer implements Runnable

Q q;

Consumer(Q q)

this.q = q;

new Thread(this, "Consumer").start();

public void run()

while(true)

q.get();

class PCFixed

public static void main(String args[])

Q q = new Q();

new Producer(q);

new Consumer(q);

System.out.println("Press Control-C to stop.");

}
22x51a3839 22357yfu

OUT PUT:

You might also like