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. Thanks a lot of!! at last I understood how use Arr
View Tutorial          By: Juan Carlos Aviles at 2011-03-11 05:22:03

2. In the case of Basic validation is it must to spec
View Tutorial          By: Uma shankar at 2014-07-09 00:58:05

3. Hey thanks for your tutorial.

But i

View Tutorial          By: Matze at 2013-07-19 16:08:46

4. Thanks Friend it was very helpfull.
View Tutorial          By: UMAR FAROOQ at 2015-03-01 07:46:43

5. i9s this such a hard ?

my head was

View Tutorial          By: shadow_5566 at 2009-05-14 01:32:01

6. why do't you create ActionErrors object like this<
View Tutorial          By: veera reddy at 2011-07-15 12:14:57

7. could u pls tell me whcih class is implemted by it
View Tutorial          By: AMIT at 2009-10-04 00:21:30

8. hi i have used this code in my site. and it says <
View Tutorial          By: Jawad at 2011-01-16 06:31:00

9. Thank you very much. This is the easiest and also
View Tutorial          By: Mehmet Üsküp at 2013-06-17 21:08:08

10. can any body help me,i need to send a sms from sim
View Tutorial          By: vinayaka DJ at 2011-11-14 12:44:01