Interesting indeed!
You can actually do that, although in a round about way...
When you synch. a static method, the lock is obtained on the 'class' object for that class.
Although contrieved, in above code you can see that you can actually call a wait() on that 'class' object. Without the wait(), it prints "Thread1" and "Thread2" but when you insert wait(), it prints only "Thread1". Because, the first
thread calls a wait() in a synch. block. that means, it starts waiting for a lock which is actually owned by itself!!!
The situation is:
The first thread has the lock and then idiotically waits for somebody else to release that lock!
Second thread is stuck because the lock is with the first thread!
So neither of them can run. They're in a deadlock.
It shows two point:
1. You can use wait()/notify()/notifyAll() in static methods.
2. BEWARE OF DEADLOCKS!!!
HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new
Pattern)
www.enthuware.com/jqplus
[This message has been edited by Paul Anil (edited December 08, 2000).]
[This message has been edited by Paul Anil (edited December 09, 2000).]