Originally posted by vikram hegde:
Hi Java pals,
Can any one help me in understanding the working calling wait() and calling Thread.currentThread().wait().
Case1>
...
synchronized(this){
this.wait();
}
...
Case2>
...
synchronized(this){
Thread.currentThread.wait();
}
...
Among the above specified code snippets, case1 invokes wait() on itself, which internally, puts the current executing thread on it into wait queue and releases the lock. In case2, we get the handle to the current executing thread by holding monitor on the same object and issue wait on that thread.
So, The question is are both the code achive the same or are they different? Please explain.
Thanks in advance,
Vikram Hegde
There is no wait() call defined on the Thread class. Case 2 will fail because you have not obtained the lock on the object Thread.currentThread() which is not the same as the object 'this'. Not to mention case 2 wont compile.
Try it out and you will see the difference immediately when it fails. But conceptually, the only difference is you are trying to wait on different objects. This has absolutely nothing to do with the fact that the one object happens to be an instance of Thread. A thread of execution and an instance of the Thread class are not the same thing, though one is used to manipulate the other.
[ March 02, 2007: Message edited by: Mr. C Lamont Gilbert ]