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. Hi Rahul,
I am struggling to send

View Tutorial          By: sravani at 2011-02-11 22:56:58

2. Hii I m Ravi. This is a good example.
Here

View Tutorial          By: Ravi at 2011-02-17 09:09:06

3. Thanks for the description. Am loading my actionse
View Tutorial          By: sridevi at 2012-01-13 10:15:40

4. It is very useful ....Thank U sooo much dear...
View Tutorial          By: Augustine Francis at 2014-06-24 16:55:21

5. Can you explain with increment and decrement opera
View Tutorial          By: Sathish Kumar at 2013-09-01 07:40:42

6. Such a nice site for clear and explains the concep
View Tutorial          By: Sandeep at 2011-05-22 06:34:42

7. Hi, I need the complete working code for sending a
View Tutorial          By: Balaji Srikaanth at 2007-03-10 01:22:35

8. I have this line of code but i don't understand
View Tutorial          By: Xris at 2011-11-08 06:01:01

9. I am new to this concept..It is really very useful
View Tutorial          By: karthiga at 2013-01-02 11:20:38

10. i am trying to create a handy and powerful barcode
View Tutorial          By: incoming barcode scanner at 2014-06-23 04:23:42