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. today Iam getting time ,date of sending and "
View Tutorial          By: raghu at 2012-01-07 14:43:32

2. i am developing a video streaming PC server to j2m
View Tutorial          By: Jamsiya at 2011-08-11 05:07:01

3. This tutorial is good for beginners. If you provid
View Tutorial          By: Sateesh Bobby at 2008-12-09 23:56:18

4. wen i run i get dis error

jdbc:mysq

View Tutorial          By: sandeep at 2009-11-24 08:24:20

5. I want java
View Tutorial          By: A.S.Vtihyatharan at 2009-12-01 03:25:36

6. Copy the javax.comm.properties into your $JAVA_HOM
View Tutorial          By: sophie at 2009-03-11 08:23:25

7. hi,
i am download 5 files and instantiate

View Tutorial          By: ganesh kumar at 2008-09-23 05:27:16

8. but how is the string concatenation done with the
View Tutorial          By: Vipin at 2009-05-09 03:08:51

9. sap crystal active-x design
failed to open

View Tutorial          By: Mubarak at 2012-11-11 17:16:54

10. i am run this code changing the SMSC (ex:+9198860
View Tutorial          By: sknand13 at 2007-11-16 03:27:52