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

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

Java program to check if user input is an even number

Archived Comments

1. Seriously very nice explanation.... Thank You
View Tutorial          By: Pulkit agrawal at 2012-03-20 10:28:10

2. dear! can you show me about "How to start as
View Tutorial          By: vatha chhean at 2011-05-31 07:34:42

3. Really a great article!
I have some questio

View Tutorial          By: ron at 2010-03-30 01:37:12

4. this is i got error msg when i run a my program .
View Tutorial          By: anbu at 2009-04-08 02:41:17

5. Thanks..it really worked well...!!!!
View Tutorial          By: Neelanjana at 2012-09-14 06:07:06

6. wow...great MAN
View Tutorial          By: Er Sounderarajan at 2013-03-13 07:29:58

7. Helpfull!
View Tutorial          By: touraj at 2010-08-11 13:40:33

8. Hii.... We are B.Tech 4th year students. We are wo
View Tutorial          By: Shipra Malik at 2010-08-17 03:32:34

9. Hi Blackpanther, the mode is either 0 or 1. If you
View Tutorial          By: Ramlak at 2008-04-01 02:24:14

10. Comment: Just for future readers: (apart from the
View Tutorial          By: Anonymous at 2013-05-17 08:53:20