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



Ask a Question

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 friends i hav project work in this topic so pls
View Tutorial          By: sullan at 2011-05-08 12:08:56

2. what about the remaining explanation......do anyth
View Tutorial          By: raki at 2011-11-07 10:47:25

3. Your example is clear crisp and very helpful..
View Tutorial          By: sridhar at 2009-08-06 08:41:14

4. Hagti hain.. the complete program has been copy pa
View Tutorial          By: Macho Man at 2012-06-21 07:58:03

5. i have same error sir..
Error applying tran

View Tutorial          By: CRUDZ at 2013-10-13 05:22:24

6. The smiple easy and best way is :)
--------

View Tutorial          By: Sandhya at 2012-06-21 06:08:25

7. Please assist me in getting the code to read messa
View Tutorial          By: Jaydatta at 2009-04-05 23:24:57

8. the difference is of great important for new comme
View Tutorial          By: ramesh shetty at 2013-03-12 15:27:23

9. Thank You So Much ^,^
I get rid of that err

View Tutorial          By: moji junk at 2009-02-24 07:48:19

10. thanks a lot..please tell me how to implement this
View Tutorial          By: agnas at 2008-04-26 12:26:52