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. As a beggnar, i find it varry varry usefuull.... P
View Tutorial          By: Rameeza at 2011-05-17 03:14:50

2. sir i need some simple example of jdbc i cannot un
View Tutorial          By: sumit at 2011-11-26 18:02:14

3. I am getting following error, I am using samsung m
View Tutorial          By: Ashok at 2013-08-17 05:29:02

4. wow !!! worked well
View Tutorial          By: Rajesh at 2012-08-30 10:54:53

5. clear concept explanation..
View Tutorial          By: ASHOK at 2013-01-16 07:31:35

6. Hi Ramlak
I am trying to run your code.

View Tutorial          By: jainitya at 2011-05-04 12:26:16

7. well explained.. Thanq :)
View Tutorial          By: Aparna at 2013-03-01 06:08:35

8. code givr an error when sending the Email:

View Tutorial          By: Ravi Shanker Yadav at 2013-05-08 12:36:59

9. Comment: yes i understood too:D:D perfect and brei
View Tutorial          By: eman at 2009-04-21 23:57:26

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