• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Join vs isAlive() and wait()

 
Monmohan Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , Please look at this code
---------------------
public class TestThreadJoin {
//private MyThread t=new MyThread();
final Thread main=Thread.currentThread();
public static void main(String args[]) throws Exception{

Thread t =new Thread(){
public void run(){
System.out.println("Run for some time");
int i=0;
while(i<10){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

};
t.start();
System.out.println("Join");
while(t.isAlive()){
System.out.println("Checking if the thread is live");
t.wait(0);
}
System.out.println("Main thread Over>>>>");


}
}

It throws an IllegalMonitor exception
j.t.wait(0);


I can't understand the reason esp when replacing the same code with
join(0) works
 
soumya ravindranath
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java API

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:

Another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method.
The timeout period, specified by timeout milliseconds plus nanos nanoseconds arguments, has elapsed.
The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

This method should only be called by a thread that is the owner of this object's monitor.

You can call wait() from a synchronized context only.
 
Monmohan Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops ..sorry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!