Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

one thread can have more than one lock ..

 
suresh seeram
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
suresh seeram
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Valentin,
Now it is clear to me..
Thank you very much for neat and understandable explanation..

regards,
Suresh
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic