Can I use wait() and notifyAll() in a static method ?? These are non-static methods of the Object class. Since static methods can't call non-static methods, how is it possible to use these methods in a static method?? Can someone explain in detail.. Thanks Regards Balaji
In your static method, you have declared the object. Like in the main method: TestClass t = new TestClass(); t.execute(); The execute() method can be non-static. Also, wait() and notify() can only be used in synchronized code block.
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!!!
Paul Anil, Thanks for such a great explanation. I am so confused about synchronized threads , deadlocks situations (like below). Can u PLEASE give an short examples for these & help me understand....??? Awaiting ur response. Aruna
I think wait and notify can also be used in method which are not synchronized. I have read it somewhere. [This message has been edited by Milind Deodhar (edited December 08, 2000).] [This message has been edited by Milind Deodhar (edited December 08, 2000).]
Milind, Did your program run ok? Normally, if you use <code>wait()</code> or <code>notifyAll()</code> in an unsynchronized method the code will compile ok but produces an IllegalMonitorStateException at Runtime.
------------------ Jane The cure for boredom is curiosity. There is no cure for curiosity. -- Dorothy Parker
Core Java Vol.2 explains the Synchronization/deadlock in detail. It explains both before/after Synchronization with simple example. Unfortunately, there is no detailed explanation for static synchronized methods.. Balaji
Thanks, Michael. I meant start() but wrote run()!! Never put untested code Anyway, I fixed it and I put a sleep() and a println() to make the output more clear. Deadlock will happen if you instert that wait() as nobody is calling a notify()/notifyAll(). Well, it's not actually a deadlock. In crude, deadlock means two threads are waiting for each other to release something that they want. Classic example is T1 accuires a Printer and then waits for a Scanner without releasing the Printer. T2 accuires a Scanner and then waits for a Printer without releasing the Scanner. And in this case, neither of them is holding on to anything. Any third party can also call a notify()/notfyAll() (as the lock is already relesed by them). But they are stuck anyway. -Paul. ------------------ Get Certified, Guaranteed! (Now Revised for the new Pattern) www.enthuware.com/jqplus
This is the result when I execute the program with c.wait() uncommented. Of course the deadlock occurs after that. But howcome Thread2 can enter into the synchronized code and able to print Thread2 as output? Thread1 Thread2
Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad: