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, good afternoon
i don,t know how to use

View Tutorial          By: Hemanshu at 2012-01-17 06:49:10

2. Hi,
really nice article. but still have on

View Tutorial          By: Yogesh Rishipathak at 2009-06-04 02:25:06

3. how to connect the database in servelet use to sta
View Tutorial          By: sudhir singh tomar at 2011-04-28 03:10:34

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

5. I want to using swith with string...
How to

View Tutorial          By: anuj at 2015-03-04 18:22:29

6. Thanks for providing detailed information .
View Tutorial          By: CM Mankar at 2008-03-18 04:37:03

7. Hi, I have the following element:

&

View Tutorial          By: gardek at 2009-09-02 14:40:37

8. i m new in java plz send some programs of java usi
View Tutorial          By: supriyo at 2009-09-02 02:03:38

9. import java.io.*;
class lastindexof

View Tutorial          By: arya at 2012-11-07 08:37:59

10. This is pretty straightforward and got me to under
View Tutorial          By: Renoir Boulanger at 2009-09-01 09:49:24