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



Ask a Question

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. but how is the string concatenation done with the
View Tutorial          By: Vipin at 2009-05-09 03:08:51

2. Hi all

I am using getAttribute but

View Tutorial          By: Suvarna at 2012-01-12 06:56:55

3. This tutorial is very much useful for me to develo
View Tutorial          By: R. Lawrance at 2009-07-26 22:03:15

4. Hi Steven. It looks like your example program does
View Tutorial          By: Ben Hallinan at 2008-04-25 08:36:42

5. file reader is good but file writer is worst
View Tutorial          By: sachinmani at 2010-03-01 04:34:22

6. Managing several submissions can be a lot more sim
View Tutorial          By: Curran Cline at 2012-12-18 05:43:44

7. Thanks :)
View Tutorial          By: Anuj Bhatnagar at 2015-05-06 14:02:19

8. Oh! Emiley, thank you very much! There are two lo
View Tutorial          By: Alexey at 2009-04-09 14:17:31

9. Please provide web.xml file for this example
View Tutorial          By: Girish at 2009-03-08 22:53:03

10. it is good
View Tutorial          By: kamal at 2009-11-01 19:58:54