One thing to remember though: if several thread are to grab the lock of several object, make sure all threads try to get the lock of the objects in the SAME sequence without what a deadlock situation may occur. For instance, if thread 1 grabs first the lock of o1 and then o2 and thread 2 the opposite, first the lock of o2 and then the lock of o1. Then depending on the execution, the first thread may grab the lock of o1, then the second thread gets to run and grabs the lock of o2. The situation is now that the thread 1 is waiting to get the lock of o2 (owned by thread 2) and thread 2 waits for the lock of o1 (owned by thread 1) => DEADLOCK !!! HIH
------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform
Dear Valentin, Thanks for fast response... See the following code.. I modified your code.. From your explanation the following code will cause deadlock.. Correct me if I am wrong..
Thanks in advance, regards, Suresh [This message has been edited by Marilyn deQueiroz (edited October 19, 2001).]
Joined: Aug 26, 2001
Exactly that's right although this WILL NOT cause deadlock but MAY cause deadlocks since it all depends on the scheduling mechanisms of the underlying platform, but it's good practice not to write such code ! Beware of the deadlocks !
------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform [This message has been edited by Valentin Crettaz (edited October 19, 2001).]
Joined: Oct 06, 2001
Dear Valentin, Now it is clear to me.. Thank you very much for neat and understandable explanation..
.....although this WILL NOT cause deadlock but MAY cause deadlocks since it all depends on the scheduling mechanisms of the underlying platform....
Yah, very few things can be said on thread as WILL instead of MAY.... ------------------ Muhammad Ashikuzzaman (Fahim) Sun Certified Programmer for the Java� 2 Platform --When you learn something, learn it by heart!
Ashik Uzzaman Senior Software Engineer, TubeMogul, Emeryville, CA, USA.