wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Deadlock 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 "Deadlock" Watch "Deadlock" New topic
Author

Deadlock

Sahil Manchanda
Ranch Hand

Joined: Jul 24, 2012
Posts: 41

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

Joined: Dec 08, 2010
Posts: 1502
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Sahil Manchanda
Ranch Hand

Joined: Jul 24, 2012
Posts: 41

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
 
Don't get me started about those stupid light bulbs.
 
subject: Deadlock
 
Similar Threads
wait() releases all locks?
Deadlock K&B program
Help me this code
Increment
synchronized threads