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. I want to use the same code in c++,
Can a

View Tutorial          By: vijay at 2009-09-26 00:47:02

2. thanks
View Tutorial          By: nitin at 2010-03-29 01:02:57

3. awesome.........
View Tutorial          By: tar at 2013-03-14 07:36:56

4. Working !

Nice example, thanks !

View Tutorial          By: Akh at 2015-01-14 14:22:24

5. hi i need moving clock javascript code...please se
View Tutorial          By: madhavan at 2008-10-19 21:59:45

6. Thanx................!!!!!!!!!!!
View Tutorial          By: ankita at 2012-01-21 19:16:54

7. Can you tell me what will happen when I use a free
View Tutorial          By: Neo at 2012-04-04 16:21:35

8. When JVM find two string object with de same conte
View Tutorial          By: ku at 2009-05-11 10:17:51

9. how this program is work , please, tell
View Tutorial          By: rohit kumar at 2015-10-30 12:22:05

10. hi,Very Nice tutorials and iwant to know how to ch
View Tutorial          By: vimala at 2010-07-08 05:05:17