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. super explanation
View Tutorial          By: pree at 2013-12-30 16:41:32

2. good.....not bad. .show me more programs.
View Tutorial          By: saranya at 2011-12-07 08:49:39

3. i manage to download all the 5 part and compile th
View Tutorial          By: Selva at 2008-04-09 07:53:09

4. How can i download the comm.API .
pls tel

View Tutorial          By: Sajeer kc at 2015-01-16 04:12:28

5. Finally a tutorial that clearly explains why Hasht
View Tutorial          By: Lisa Ander at 2011-10-27 20:33:15

6. its nice explanation.
View Tutorial          By: rahul ahuja at 2010-06-13 22:33:35

7. Thx for the explanation, I understand what recursi
View Tutorial          By: Tina at 2008-12-15 16:39:27

8. i am not getting output by using this code..after
View Tutorial          By: dhaval at 2011-01-03 03:42:28

9. I think it is the best article for freshers.thanks
View Tutorial          By: srikanth at 2010-11-01 23:27:53

10. Hi,

I am new to html, and javascrip

View Tutorial          By: Tom Theriault at 2010-12-14 14:54:09