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. this code is very and help in our project BUT
View Tutorial          By: bhamre sandeep at 2010-02-28 03:02:53

2. hi all m getting following error ,
can anyo

View Tutorial          By: raj at 2008-11-04 21:46:12

3. Very helpful and easily understandable!
View Tutorial          By: Teerna at 2014-11-12 23:55:49

4. does anyone know what exactly to use to desgn a ph
View Tutorial          By: andrew willy at 2012-01-29 14:51:13

5. I think 19 floor's code doesn't fix the problem.Al
View Tutorial          By: Benson at 2012-11-28 08:04:26

6. can help me for program in java Tomorrow I have ex
View Tutorial          By: blasm at 2013-06-21 06:59:41

7. thanks for this good example. i got error like (th
View Tutorial          By: anandhi at 2009-12-20 09:32:45

8. where i put this rar file to include comm api
View Tutorial          By: shubham bansal at 2015-02-20 12:13:16

9. Hi IDK,
You close any applications includin

View Tutorial          By: Steven at 2008-12-30 03:14:33

10. right to point.. thanks man
View Tutorial          By: manish at 2012-07-28 15:01:26