• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

notifyAll() not working

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friends
In this program I'm not able to bring the thread t1 out of the waiting state. Isn't notifyAll() supposed to wake up all waiting threads. Am I doing anything wrong ?
class TestThread extends Thread {
public void startMe()
{
synchronized(this)
{

notifyAll();
System.out.println("Not");
}
}
public void run() {
try
{
synchronized(this)
{
wait();
System.out.println("Notified");
}
}
catch(InterruptedException e)
{}
}
public static void main(String[] args) {
TestThread t1 = new TestThread();
t1.start();
t1.startMe();
}
}
Thanks
Vidya
[ April 23, 2003: Message edited by: Vidya Venkatesh ]
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem arises because your t1.startMe() method is being invoked before your t1 thread runs. Consequently, the call to notifyAll() is invoked before the call to wait(). If this happens (i.e. notify is called before wait), the thread could wait forever unless another notify call is made on the same object.
It is good practice therefore to put in a test condition before calling wait to avoid the problem. I have ammended your code to provide an example of such a test condition.

Alternatively, if you want your program to run as is AND output "Notified", you could invoke the Thread.sleep() method on your main thread (i.e. the one that calls t1.startMe()) with a time parameter, directly after the call to t1.start(). This would give thread t1 a chance to run and call wait() before the call to notifyAll().
Hope this helps.
[ April 23, 2003: Message edited by: Rory French ]
[ April 23, 2003: Message edited by: Rory French ]
[ April 23, 2003: Message edited by: Rory French ]
 
Vidya Venkatesh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much. Now it is clear to me
Vidya
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The solution offered by Rory prevents an eternal wait, but does not show that notifyAll() wakes all waiting threads.
If you just run the program a number of times you will see that wait() is never called.
It is not difficult to see why.
The main thread is already running. Consequently method startMe() will already have run by the time the second thread is executing.
To show that notifyAll() really wakes up the second thread call join on the second thread with a timeout of about 1 second, depending on the speed of your processor.
The following is a modifed version what shows this point:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic