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. The only thing that really was the solution to my
View Tutorial          By: nick marinho at 2010-07-24 17:45:59

2. Very Great thanks for the code
View Tutorial          By: Alvin Funelas at 2014-07-12 00:42:01

3. I'm having trouble figuring out how to sign my mid
View Tutorial          By: Patrick Somers at 2011-04-04 14:54:45

4. Hi Jgan,
Its very useful ad nice article .

View Tutorial          By: pramod at 2012-01-16 13:21:35

5. i fixed it already...

how can I get

View Tutorial          By: kathleen ashley at 2010-08-08 04:51:26

6. hey..
shouldn't the 2nd java code have an o

View Tutorial          By: izzie at 2010-04-06 11:34:34

7. thanks or the infromation about switch statement
View Tutorial          By: mikko at 2008-09-01 23:48:44

8. How to download more than one files. If I use for
View Tutorial          By: Riyas at 2012-01-08 07:55:03

9. i'm doing project on hotel management...
an

View Tutorial          By: prashant patil at 2012-08-19 05:16:20

10. hi guys,
its working in a good manner impre

View Tutorial          By: saran at 2012-12-18 13:15:12