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. Hi there! the coding are SUPERB..bt i do nd help i
View Tutorial          By: lily sandra at 2008-01-15 18:30:33

2. Thanx.. it helps me a lot, now.. i have 1 more que
View Tutorial          By: GuzZpaWn at 2012-01-15 17:19:29

3. Hi,
this is excellent stuff and I highly ap

View Tutorial          By: Nanda at 2007-11-02 04:58:30

4. super article for freshers and good
View Tutorial          By: santhosh at 2011-11-03 14:32:44

5. Thanks.. worked perfectly :)
View Tutorial          By: DJ at 2012-04-05 19:40:43

6. hai, i tried the abvoe code, m getting the follwin
View Tutorial          By: anith at 2011-03-13 06:02:31

7. pls tell me how to insert data in database .
View Tutorial          By: sankar at 2009-10-22 05:37:45

8. Hi.
How we can create list with menuitem in

View Tutorial          By: omid at 2008-09-08 02:09:50

9. hi...i want a code to send sms from GSM to PC.
View Tutorial          By: siri at 2011-12-19 07:39:02

10. nice explanation.but one thing in retrieving proce
View Tutorial          By: venkateswarlu at 2013-04-06 09:03:46