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. Just Clean Project(deleting all compiled code) and
View Tutorial          By: Shivakumar at 2015-05-04 08:20:38

2. static concept was clearly explained with some exa
View Tutorial          By: Rajkumar s ,m.p.patti,rasipuram. at 2012-03-28 16:17:31

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

4. My Friend,

Thank you so much for ex

View Tutorial          By: Bikash at 2010-05-30 00:15:26

5. I am java developer and interested to write scjp e
View Tutorial          By: Parvathi at 2013-07-05 14:24:08

6. I HAVE ONE DOUBT.

public class Anim

View Tutorial          By: georgy at 2009-10-22 02:46:10

7. DO NOT USE QSORT IN A C++ CODE.

If

View Tutorial          By: paercebal at 2008-08-13 05:06:45

8. not good not bad...............
View Tutorial          By: mahesh at 2010-10-05 00:45:54

9. the program is writen beautifly and it is easy to
View Tutorial          By: sania cheema at 2009-05-04 12:14:19

10. The code gives the following exception :-
<

View Tutorial          By: VIKRAM at 2007-03-11 22:56:16