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


B) Consumer never receives the notification and hangs

just add in the main method:

// 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. can u explain about printStackTrace()method.what i
View Tutorial          By: himabindu at 2009-06-10 20:03:14

2. outstanding....
View Tutorial          By: subhasish biswas at 2009-04-19 00:16:50

3. excellent code.....
View Tutorial          By: Anonymous at 2009-05-07 03:09:01

4. How can i use this code to send multiple sms using
View Tutorial          By: Shoaib at 2014-03-07 19:49:11

5. I want to parse a xml content some think like bel
View Tutorial          By: sagar at 2011-04-20 00:59:06

6. I'm sorry but your closing statement...

View Tutorial          By: Al Scherer at 2009-09-15 09:33:24

7. Very good. very clear. thank you
View Tutorial          By: Andrei at 2012-12-16 04:29:36

8. Including the required mail properties should solv
View Tutorial          By: Rajesh Vaddi at 2011-12-13 23:55:15

9. I am very much thankfull to you in providing the c
View Tutorial          By: c vijaychandar at 2010-05-05 10:14:59

10. its really a good post... very informative..
View Tutorial          By: javagava at 2011-12-13 19:49:08