• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

notify thread

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class TestThread extends Thread{
public void restart(){
startMe();
}
public static void startMe(){
synchronized(TestThread.class){
TestThread.notifyAll();
System.out.println("Trying to Notify");
}
}

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.restart();

}

}

Why is the thread not notified?
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
since you are calling wait() on instance object, to get your thread notified you have to call notify on the instance object. Problem with your code is you are calling notifyAll() on the class object.

change your code as followes :



 
Marshal
Posts: 80666
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

I am not convinced that wait and notify are beginners' topics, so I shall move this thread.
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
startMe() is getting executed prior to run(). put main thread to sleep for some time, to make sure run() is executed prior to startMe().

 
reply
    Bookmark Topic Watch Topic
  • New Topic