aspose file tools*
The moose likes Threads and Synchronization and the fly likes How to remove Deadlock ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to remove Deadlock ?" Watch "How to remove Deadlock ?" New topic
Author

How to remove Deadlock ?

Neeraj jain
Ranch Hand

Joined: Jun 18, 2013
Posts: 105
I have a situation in which 1 thread is trying to acces the synchronized method on another object and second thread is also doing the same thing on another object


I have a deadlock situation ..... Please tell me how should i remove it
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42944
    
  68
Don't make the "getSum" method synchronized. Nothing much happens in it that would need to be synchronized anyway. To ensure that the value of the field "sum" is seen correctly between threads you should declare it volatile, though.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Neeraj jain wrote:I have a deadlock situation ..... Please tell me how should i remove it

Ulf's basically told you, but another thing to remember about synchronization is that it should be minimal. Your sum calculation goes through an enormous loop that holds onto a monitor for its entire execution, which is completely unnecessary. See if you can work out the difference with this:
HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Heena Agarwal
Ranch Hand

Joined: Dec 25, 2013
Posts: 261
    
    4
And I think we shouldn't subclass Thread to create our threads unless
we need to provide a custom implementation of methods other than the run
method. Implementing Runnable should be the way to create threads for
all other cases.

Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

To avoid deadlock, one of the rules-of-thumb is:
If a thread, during its flow, is acquiring locks L1 and L2 in a specific order, then all the threads should acquire the locks in the same order.

Currently, in OP's code, first thread locks r1 and then attempts to lock r2. However second thread does exactly opposite - this is classic scenario of deadlock.

Of course, suggestions by Ulf and Winston would work, and the rule mentioned above is not silver-bullet solution to fix deadlock issues, but following that rule has saved a lot of my time(and I could identify potential deadlocks before they actually occurred).

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Neeraj jain
Ranch Hand

Joined: Jun 18, 2013
Posts: 105
Anayonkar Shivalkar yes you are completely right but by using your way my program would be reached in an another world because basically i am trying to calculate personal sum in both diffrent threads diffrently then trying to get sum of another thread ... so for this problem your solution upto i think will not work . please correct me if i am wrong
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

Neeraj jain wrote:Anayonkar Shivalkar yes you are completely right but by using your way my program would be reached in an another world because basically i am trying to calculate personal sum in both diffrent threads diffrently then trying to get sum of another thread ... so for this problem your solution upto i think will not work . please correct me if i am wrong

Well, in that case, as mentioned, Ulf and Winston's suggestions would work.

Btw, I guess you need to do some extra coding to ensure the ordering (e.g. if first thread is calculating the sum, and what if second thread is not yet started, or finished calculating its own sum?)
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
Neeraj, please avoid writing the entire text in bold font. It is sometimes an eyesore.

I have corrected it this time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to remove Deadlock ?