Comment on Tutorial - wait(), notify() and notifyAll() in Java - A tutorial By Jagan



Comment Added by : Hdm-Student

Comment Added at : 2010-06-16 08:10:51

Comment on Tutorial : wait(), notify() and notifyAll() in Java - A tutorial By Jagan
Hello,

your solution is kinda overkill.
When used correctly, notify and wait don't need workarounds such as valueSet.

Here's the improved version:
----------------------
package producer;

// A correct implementation of a producer and consumer.
class Q {
int n;

synchronized int get() {
try {
notify();
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Got: " + n);
return n;
}

synchronized void put(int n) {
try {
notify();
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.n = n;
System.out.println("Put: " + n);
}
}

public 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++);
}
}
}

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.");
}
}

-------------------------


View Tutorial



Ask a Question

Subscribe to Tutorials

Related Tutorials

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. whoo... its very hard
View Tutorial          By: Anonymous at 2009-01-13 20:17:20

2. Good work!

Few changes: polish oper

View Tutorial          By: Karol Z. at 2010-06-02 09:09:52

3. its cool, you've cleared me.
View Tutorial          By: zealous at 2013-09-10 17:40:26

4. This is very useful for java beginners
View Tutorial          By: Syed Musthafa at 2011-11-20 07:11:50

5. My code runs smoothly. I have taken some steps for
View Tutorial          By: Amit Shrivastava at 2015-01-25 13:48:46

6. how to execute all these five programms tell me st
View Tutorial          By: shweta at 2008-09-24 07:35:22

7. thanks. that was fair enough. :)
View Tutorial          By: Mustafa at 2010-05-16 10:12:33

8. thank you very much....
View Tutorial          By: divya at 2013-01-06 15:50:43

9. Rattling explanation .....thnx brother
View Tutorial          By: zorro at 2011-06-16 05:05:43

10. When stateless bean not maintains any state and no
View Tutorial          By: Mallikarjun at 2015-09-18 15:42:46