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



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. To "need help":

String st

View Tutorial          By: alex at 2010-05-13 09:57:25

2. hi,
i use spinner concept in my project but

View Tutorial          By: lavanya at 2013-03-01 06:25:01

3. thanx nice explanation...
View Tutorial          By: vignesh at 2012-07-15 20:12:22

4. I have ran this code but each port has raised an &
View Tutorial          By: Jawnie at 2012-03-29 07:23:22

5. i want to learn j2me basic to ....
View Tutorial          By: gopiraj at 2012-12-21 11:52:01

6. i get exception NoSuchPortException. i tried on di
View Tutorial          By: harryjoy at 2009-11-22 05:52:48

7. Plz help me......its urgent
i need a code

View Tutorial          By: Nisha at 2010-03-22 00:38:42

8. hey...i have a problem getting the system date in
View Tutorial          By: angel at 2010-04-03 09:27:38

9. can i ask question? on how to make a statement by
View Tutorial          By: alas at 2012-09-27 00:42:08

10. Can u show how to do text to speech with servlet.
View Tutorial          By: Rotika at 2011-01-23 20:54:40