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. where to copy the file " speech.properties&qu
View Tutorial          By: Mohan at 2012-09-16 01:47:32

2. Excellent example! Thanks a ton:-)
One ques

View Tutorial          By: Andrew at 2009-04-11 09:15:19

3. can help me for program in java Tomorrow I have ex
View Tutorial          By: blasm at 2013-06-21 06:59:41

4. hi sir can you help me with this problem

View Tutorial          By: Lee at 2015-10-01 09:08:26

5. my query is like this: " accept a string, cou
View Tutorial          By: samtopper at 2014-07-31 17:40:25

6. would you please send me a code that can be run pl
View Tutorial          By: putra at 2008-12-17 13:26:26

7. it is working....great
View Tutorial          By: mahesh maduranga at 2011-03-27 23:01:57

8. my host is 202.148.202.3.Could not connect to SMT
View Tutorial          By: Anbuselvam at 2012-05-04 04:52:17

9. we can create reference variable for an abstract c
View Tutorial          By: siva at 2012-08-30 06:03:50

10. public class zcxzcStack
{
//push fun

View Tutorial          By: eissen at 2014-09-22 13:32:20