aspose file tools*
The moose likes Threads and Synchronization and the fly likes notify thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "notify thread" Watch "notify thread" New topic
Author

notify thread

jagat thoudam
Greenhorn

Joined: Jul 20, 2009
Posts: 1
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

Joined: Aug 26, 2008
Posts: 263
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 :




SCJP 1.5(97%) My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
Welcome to JavaRanch

I am not convinced that wait and notify are beginners' topics, so I shall move this thread.
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
startMe() is getting executed prior to run(). put main thread to sleep for some time, to make sure run() is executed prior to startMe().

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: notify thread