This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deadlock

 
Sahil Manchanda
Ranch Hand
Posts: 41
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody explain the deadlock here..? i mean if one thread gets the lock of (a) only he can have the lock of (b). so how is the deadlock possible?
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sahil,

Welcome to CodeRanch!

Please, UseCodeTags. I've formatted the code this time.

Coming to your problem, you are invoking doTransfer method with different object.
1) At line 5, thread (say T1) will get a lock on r1 and then r2.
2) At line 6, thread (say T2) will get a lock on r2 and then r1.

So, deadlock may happen in below scenario:
1) T1 gets lock on r1
2) T2 gets lock on r2
3) T1 tries to get lock on r2, but it's lock is already owned by T2
4) T2 tries to get lock on r1, but it's lock is already owned by T1

Thus, both threads will not be able to proceed - due to deadlock.

I hope this helps.
 
Sahil Manchanda
Ranch Hand
Posts: 41
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:Hi Sahil,

Welcome to CodeRanch!

Please, UseCodeTags. I've formatted the code this time.

Coming to your problem, you are invoking doTransfer method with different object.
1) At line 5, thread (say T1) will get a lock on r1 and then r2.
2) At line 6, thread (say T2) will get a lock on r2 and then r1.

So, deadlock may happen in below scenario:
1) T1 gets lock on r1
2) T2 gets lock on r2
3) T1 tries to get lock on r2, but it's lock is already owned by T2
4) T2 tries to get lock on r1, but it's lock is already owned by T1

Thus, both threads will not be able to proceed - due to deadlock.

I hope this helps.


oh i am sorry i dint see that thankyou so much mate! really appreciate your help
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic