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. Turning off hostname checking also turns off all s
View Tutorial          By: eric at 2014-01-30 18:09:05

2. Hi sir,
please send some sample code for sp

View Tutorial          By: hindu at 2013-03-21 06:00:09

3. This is a great idea if I am targeting the require
View Tutorial          By: Shanthamurthy Hanumantharayappa at 2015-09-14 16:45:22

4. i Want the Flow Chart and code for the mobile Cale
View Tutorial          By: Sudip Khatiwara at 2010-03-15 04:10:41

5. Thnx A Lot...very healpful...........
View Tutorial          By: vinay at 2012-07-19 09:21:20

6. When stateless bean not maintains any state and no
View Tutorial          By: Mallikarjun at 2015-09-18 15:42:46

7. sir i need some simple example of jdbc i cannot un
View Tutorial          By: Velkumar.s at 2013-05-15 07:08:37

8. Dear any buddy i use Nokia N73 and connect with Bl
View Tutorial          By: zstranger at 2012-03-29 07:43:12

9. sir, i want get the SCJA certification. how to rig
View Tutorial          By: john basha at 2012-12-16 07:13:46

10. I've tried this code but i am gettting error at li
View Tutorial          By: Iqra at 2012-12-13 10:43:12