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. good one nice
View Tutorial          By: megha at 2011-07-16 15:54:29

2. Great work... keep it up :)
View Tutorial          By: Kushan at 2010-07-05 23:36:12

3. Hi Jim, to get the code for this clock, right clic
View Tutorial          By: Rajan at 2008-04-11 08:25:03

4. I want simple programs using BuffuredReader for m
View Tutorial          By: Why ask my name? at 2013-06-01 04:56:00

5. Bitwise OR is a pipe (|), not a logical NOT (!)
View Tutorial          By: Martijn at 2011-06-21 10:49:29

6. Thanks for the digital shell clock: very cool, and
View Tutorial          By: Lol loller at 2012-04-11 15:40:45

7. Hey thnx. Solved my "inconvinience" ;P
View Tutorial          By: Hesam Ossanloo at 2013-12-19 15:13:31

8. Superb

many thanks to you Mashoud.

View Tutorial          By: orchidouest at 2009-05-10 12:58:53

9. i want very simple & easy notes on php which u
View Tutorial          By: deepa at 2011-07-12 09:04:30

10. i wanted to know the rules how they are used like
View Tutorial          By: shlok gupta at 2012-03-24 11:44:24