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. Good examples for beginners.
View Tutorial          By: Sagar at 2009-10-16 01:04:46

2. hi i m deepika mittal . i m working on the same pr
View Tutorial          By: deepika Mittal at 2009-05-30 06:55:22

3. very easy way for understanding the java programmi
View Tutorial          By: praveen chandrakar at 2010-10-14 20:26:53

4. Really a appriciatable one. Can u plz send the cod
View Tutorial          By: Jeevan at 2009-04-11 12:39:45

5. Cool! Downloading the whole file with the HttpCon
View Tutorial          By: Luddite at 2010-06-26 12:26:26

6. This is awesum !!!
easy to understand ....

View Tutorial          By: Sud at 2011-01-19 08:37:55

7. I have download that 5 files .But how to run this
View Tutorial          By: Unnati at 2011-07-07 06:07:26

8. give an recursive example using string
View Tutorial          By: sauk at 2011-08-22 16:16:41

9. hi all m getting following error ,
can anyo

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

10. Good job. Very clear and concise and a great help.
View Tutorial          By: Eduardo at 2012-02-08 19:18:50