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. please provide Example instanceof With Collection
View Tutorial          By: siddardha at 2010-08-18 00:30:38

2. Hi Maybelline, This is the <a href="http:/
View Tutorial          By: Daniel Malcolm at 2008-08-21 06:57:31

3. thanks a lot ..
View Tutorial          By: indra at 2009-03-25 15:40:03

4. Thank you so much. I have migrated from the .NET t
View Tutorial          By: kanika at 2011-05-31 12:23:33

5. thanks a lot it was really helpful...........for d
View Tutorial          By: Joshi Rocky at 2010-01-29 14:13:40

6. better if it is explained with output of the progr
View Tutorial          By: dinesh at 2013-05-05 13:57:21

7. Good work. Although it would be nicer if you can p
View Tutorial          By: frisked09 at 2011-05-26 23:52:34

8. i want to know how to connect one page to another
View Tutorial          By: mahesh at 2009-08-26 01:31:40

9. hi,

i have a problem. I have the ti

View Tutorial          By: Felipe at 2010-02-18 05:03:51

10. i want to know about simple java structure with br
View Tutorial          By: Rambahaz at 2009-09-07 06:41:25