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



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 or the infromation about switch statement
View Tutorial          By: mikko at 2008-09-01 23:48:44

2. This code is only for send sms? can it also alert
View Tutorial          By: abdul kadir at 2009-10-23 05:37:46

3. The coding which is given is easy to understand bu
View Tutorial          By: Chandra Sekhar Bodireddy at 2013-03-08 06:02:17

4. If the database server goes down, should the conne
View Tutorial          By: Naveen Gowd at 2012-02-02 07:17:38

5. Hi VIKRAM, You cannot use your sim card number the
View Tutorial          By: Ramlak at 2007-03-29 00:38:55

6. can u be more specific like put up some more headi
View Tutorial          By: Jass at 2012-10-29 03:51:29

7. awesome. thanks for the info
View Tutorial          By: Darren at 2010-05-09 19:55:22

8. this code runs permanently!!! created by diko(Turk
View Tutorial          By: ali at 2013-08-12 07:56:01

9. Thanks.I was searching the internet for a lot of t
View Tutorial          By: Habi at 2011-12-22 07:43:51

10. I want to say, that you have to give complete code
View Tutorial          By: Jasvir Yadav at 2009-09-05 07:23:15