• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

notify thread

 
jagat thoudam
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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?
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • 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 :



 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • 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
  • 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().

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic