Comment on Tutorial - wait(), notify() and notifyAll() in Java - A tutorial By Jagan



Comment Added by : Mladen Covic

Comment Added at : 2013-03-28 15:08:04

Comment on Tutorial : wait(), notify() and notifyAll() in Java - A tutorial By Jagan
Unfortunatelly this a BAD example of to communicate between Threads. In general, it is a bad idea to use wait and notify to communicate between Threads ONLY relying on wait and notify. The reason is very simple: nothing guaranties that object waiting will receive the proper notification. In other words it might either :

A) receive notification sent from someone else and NOT Producer.

just add this in main method at the end
synchronized(q)
{
q.notify();
}

or

B) Consumer never receives the notification and hangs

just add in the main method:

synchonized(q)
{
...
// do something forever without releasing the lock on q
}

In general, communication between threads should be done either through Events and (synchronized) EventMulticasters or anchored around VERY protected (encapsulated) synchronized object with with wait and notify only called on that object. Even then there is a pssibkity of messing that object's monitor.


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. thanks for ur j2me discription.....
View Tutorial          By: sivaguru at 2009-12-15 05:13:32

2. I am doing a project to for sending sms from the s
View Tutorial          By: visalakshi at 2010-03-30 20:55:50

3. Plz send code of sending sms from java code...

View Tutorial          By: Shekh at 2014-11-27 14:18:25

4. Following question to this topic:
- when i

View Tutorial          By: Alistair at 2012-01-14 10:59:52

5. Thanks!! Its very helpful.
View Tutorial          By: Tawfiiq at 2012-07-06 05:29:50

6. Hai people plz help it's urgent, i got no errors i
View Tutorial          By: Hopewell at 2011-10-13 22:57:45

7. int myColor = 0xFFFF00;
paint(Graphics grap

View Tutorial          By: krishna mohan at 2010-02-01 05:22:12

8. I have one textbox and three label.i want to parse
View Tutorial          By: Nara at 2011-08-01 06:16:07

9. my question is the same of sajjad.what about (!iss
View Tutorial          By: hossain at 2012-04-26 04:28:56

10. i searched a lot of sites but found the answer her
View Tutorial          By: ALIEN ATTACK at 2012-01-09 09:39:36