• Post Reply Bookmark Topic Watch Topic
  • New Topic

wait without notify  RSS feed

 
Varun Nayudu
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tellme hw the below code works......




public class Mangler{
public static void main(String[] args)throws InterruptedException{

ak a = new ak();
a.start();
synchronized(a){a.wait();}
System.out.println("asds");

}
}


class ak extends Thread{
public void run(){
System.out.println("asdsadsadsa");
}
}

Output:
asdsadsadsa
asds
 
Anton Pechsherov
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your output shows all.. how does it works...
But if you want Main thread waiting ak(thread) , make waiter in ak:

I dont understand Your question..
 
Henry Wong
author
Sheriff
Posts: 22860
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tellme hw the below code works......


I remember the last time I explained this, the poster didn't believe me... ... but here goes...

It is an implementation detail of the join() method. When you call join() on a thread it will check to see if the thread is alive, and while it is, it will call the wait() method, on the thread object.

For this to work, there is cleanup code on the thread object. Basically, when the run() method finish, it (the code that call run()) will do cleanup on the thread, including calling notifyAll() to wakeup all the joining threads. And since you are waiting on the thread object, you get woken up too.


Again, this is an implementation detail of the join() method. Do not depend on this. It is not documented in the JavaDoc, and hence, may change in the future.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!