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. excellent..... thanks it help me a lot.......
View Tutorial          By: REJAPUTRA at 2009-10-23 09:19:25

2. Of course you can use this in a web application. A
View Tutorial          By: Ramlak at 2008-09-17 08:24:21

3. I was trying to copy your coding and execute it, b
View Tutorial          By: Guest at 2012-01-02 04:08:21

4. Good example. However as someone said, it would be
View Tutorial          By: Neo Cambell at 2010-03-10 11:02:06

5. pritnt
1 2 3 4 5

View Tutorial          By: osama at 2013-02-05 03:16:15

6. thanks!!
View Tutorial          By: eoko at 2011-05-10 23:34:29

7. I get theses errors and apparrently it cannot find
View Tutorial          By: Abhaya at 2013-02-20 22:42:18

8. Hi Andi, Stig and Derick!

Nice tur

View Tutorial          By: Lasse Christiansen at 2010-05-01 01:58:14

9. what is the data type of the variable direction di
View Tutorial          By: [email protected] at 2009-12-25 03:07:46

10. Hi There Busy doing a project and found that i cou
View Tutorial          By: Steven Owen Nell at 2010-09-05 13:32:01