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! great tutorial!
View Tutorial          By: Gracia at 2013-01-10 07:58:06

2. Wow Its awesome.
Thank you for giving such

View Tutorial          By: sukumar maji at 2010-07-25 12:47:30

3. i fixed it already...

how can I get

View Tutorial          By: kathleen ashley at 2010-08-08 04:51:26

4. nice one
View Tutorial          By: priyaaaaaaaaaaaaaaa at 2011-06-21 08:04:39

5. Hi Blackpanther, the mode is either 0 or 1. If you
View Tutorial          By: Ramlak at 2008-04-01 02:24:14

6. great tutorial but for me blob handling is still s
View Tutorial          By: Sarcina Saptamani at 2011-11-10 09:15:07

7. hi Ashray P Shetty,
I want assistance in th

View Tutorial          By: Shabbir Manasawala at 2010-11-19 02:02:43

8. Hi guys
i am a beginer in java.. pls help

View Tutorial          By: Jeevan at 2009-12-19 09:35:06

9. Excellent!!!!!!!!!!!!
View Tutorial          By: Ramana at 2009-01-02 19:31:58

10. nice example . and good job.
View Tutorial          By: mohit sinha at 2011-04-07 03:19:01