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

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

Java program to check if user input is an even number

Archived Comments

1. javax.comm.NoSuchPortException
at javax.co

View Tutorial          By: nikhil at 2009-07-22 05:58:55

2. Timely info right when I needed it. Thanks.
View Tutorial          By: Electric Vehicles at 2008-12-10 15:15:25

3. Great article! Simple and precise. Thanks!
View Tutorial          By: dz at 2014-10-06 04:36:56

4. does anyone know what exactly to use to desgn a ph
View Tutorial          By: andrew willy at 2012-01-29 14:51:13

5. nice article..
View Tutorial          By: yummy at 2010-01-05 21:18:35

6. class thrs
{
public void run()

View Tutorial          By: vivek tiger at 2012-04-19 10:19:56

7. /*Web.xml File */

<?xml vers

View Tutorial          By: Pavan Barot at 2010-07-11 23:32:35

8. hi i unable to retrive am getting below excepti
View Tutorial          By: vignesh at 2015-03-25 17:14:58

9. give some good examples very bad i dont lke dis ex
View Tutorial          By: abinaya banu at 2014-07-14 06:04:28

10. I have no mounted the sd card, and I have the &quo
View Tutorial          By: Cgamboa at 2012-12-01 06:39:14