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. its a big help!..thankz
View Tutorial          By: mezer at 2013-07-17 05:34:07

2. This article is help full for beginners.now i will
View Tutorial          By: shobha B at 2012-08-31 06:23:31

3. Sir , I am doing BCS . I want to know more about
View Tutorial          By: Sachin deshmukh at 2010-08-04 02:24:59

4. 3221225477 is C0000005 in hex notation, which is j
View Tutorial          By: Stan at 2009-06-29 12:14:25

5. Hi sir,
please send some sample code for sp

View Tutorial          By: hindu at 2013-03-21 06:00:09

6. realy it was a nice basic example...and better def
View Tutorial          By: amit kumar at 2011-03-14 08:11:31

7. i have an exception unknown host.
please te

View Tutorial          By: saddam hussain at 2011-03-22 10:46:48

8. good
View Tutorial          By: rajesh sharma at 2011-07-18 05:20:56

9. Hi Shiva,
Comparator interface is used to o

View Tutorial          By: Srinath at 2015-04-19 08:23:20

10. hi can any one help me how to do basic pc actions
View Tutorial          By: karthikeyan at 2008-03-14 04:18:52