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



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. Great! Concise, helpful, a real life-saver. Google
View Tutorial          By: Motti Shneor at 2012-07-31 14:29:37

2. Your post was very informative... Thanks a lot. My
View Tutorial          By: Rohit Shaw at 2010-04-12 04:29:02

3. Thanks for your code. Nice working but I have one
View Tutorial          By: Ravinder Kumar at 2012-01-02 18:01:21

4. very use full.....
View Tutorial          By: janarthanam at 2015-08-29 06:53:03

5. I don't like Java for not supporting pointers.
View Tutorial          By: Yannbane at 2012-10-14 12:16:27

6. //Program of Method Overloading with Runtime Value
View Tutorial          By: Subhojit Das at 2014-10-09 07:31:20

7. what's the purpose of indexOf('t',10)
View Tutorial          By: pevi at 2011-02-22 04:23:20

8. how to read a number from a keyboard and write tha
View Tutorial          By: aiswarya at 2015-09-13 03:48:05

9. providing explanation about the topics is very use
View Tutorial          By: vikram naidu at 2010-06-09 23:40:41

10. thanks for this good example. i got error like (th
View Tutorial          By: anandhi at 2009-12-20 09:32:45