aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes one thread can have more than one lock .. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "one thread can have more than one lock .." Watch "one thread can have more than one lock .." New topic
Author

one thread can have more than one lock ..

suresh seeram
Ranch Hand

Joined: Oct 06, 2001
Posts: 42

Dear friends,
Can any explain whether one thread can hold two or more locks simultaneously.. on different objects..
If so please explain with example.
thanks in advance..
regards
Suresh
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Yes it can, look at the following code

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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
suresh seeram
Ranch Hand

Joined: Oct 06, 2001
Posts: 42
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).]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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).]
suresh seeram
Ranch Hand

Joined: Oct 06, 2001
Posts: 42
Dear Valentin,
Now it is clear to me..
Thank you very much for neat and understandable explanation..

regards,
Suresh
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Originally posted by Valentin :
.....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 Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: one thread can have more than one lock ..