Hi,
I would like to know the difference in effect for calling wait() with
or without explicit object reference.
If o.wait() being called, I know that the calling thread is waiting on
object instance o. How about calling wait(), like below example ? :)
public class Test {
public static void main(String argv[]) {
TThread t1 = new TThread(), t2 = new TThread();
t1.start();
t2.start();
}
}
class TThread extends Thread {
public void run() {
nowait();
}
public synchronized void nowait() {
while (true) {
try {
wait();
} catch (InterruptedException ie) {}
}
}
}
Steve Horsley - 05 Mar 2004 20:52 GMT
> Hi,
>
[quoted text clipped - 24 lines]
> }
> }
Since TThread extends Thread, wait() ends up calling Thread's wait()
method, which waits for someone to notify itself.
I really do advise that you give up trying to extend Thread. It is
causing undue confusion.
Steve
lonelyplanet999 - 06 Mar 2004 06:33 GMT
> Since TThread extends Thread, wait() ends up calling Thread's wait()
> method, which waits for someone to notify itself.
You mean the thread instances t1 & t2 just wait on themselves ? If so,
who can wake them up ?
> I really do advise that you give up trying to extend Thread. It is
> causing undue confusion.
>
> Steve
Tony Morris - 06 Mar 2004 11:34 GMT
> Hi,
>
[quoted text clipped - 24 lines]
> }
> }
Calling any method without a reference from a non-static context implies the
reference is 'this'.
In your case, wait() is equivalent to this.wait().
There is a deeper understanding (deeper than understanding the implicit
reference for a non-qualified method call for example) that is essential to
the use of wait/notify/notifyAll on thread monitors.

Signature
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)