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. good explanation,
Thanks

View Tutorial          By: venkatesh at 2012-01-04 08:43:48

2. thank you
View Tutorial          By: cutiegurl at 2011-01-18 20:23:39

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

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

4. Hi VIKRAM, You cannot use your sim card number the
View Tutorial          By: Ramlak at 2007-03-29 00:38:55

5. good explanation ,it is very very understandable
View Tutorial          By: chaitanya at 2011-10-27 16:21:55

6. hi friends i hav project work in this topic so pls
View Tutorial          By: sullan at 2011-05-08 12:08:56

7. javax.microedition.io.ConnectionNotFoundException:
View Tutorial          By: yellaiah at 2014-12-27 05:18:52

8. hai,
i want to know how the connect

View Tutorial          By: V.Selvi at 2010-02-28 21:01:33

9. Can any one say how to send a message to a particu
View Tutorial          By: Sankar guru.P at 2012-01-05 07:43:44

10. sorry guys: how can i retrieve a report or large t
View Tutorial          By: aidid at 2013-05-04 06:07:42