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. very good introduction.thanx to the authr of this
View Tutorial          By: anjali at 2011-06-17 02:14:48

2. Hi dude's it's working fine for me.first i got no
View Tutorial          By: raavi.surendra at 2011-11-16 10:10:31

3. need to be place taglig in the <jsp-config>
View Tutorial          By: Shaik Basha at 2013-01-30 03:11:41

4. very nice content
View Tutorial          By: sarat at 2012-08-22 12:17:26

5. it's very usefull to the users who were in startin
View Tutorial          By: Harichandana at 2013-02-28 04:37:17

6. vry nice example....thanx..
View Tutorial          By: rekha at 2010-09-14 11:55:38

7. hi!!!!!!!!! MR. RAMLAK will u plz tell the methode
View Tutorial          By: balwant at 2008-11-10 17:19:44

8. Can u please tell me how to compile and execute a
View Tutorial          By: RJ at 2010-04-20 01:30:10

9. this is the thread object and thread name
View Tutorial          By: narain at 2011-12-06 07:33:07

10. i run above code in my netbean ide, i din't get ou
View Tutorial          By: manish sharma at 2013-04-03 05:28:40