Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to remove Deadlock ?

 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1089
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeraj, please avoid writing the entire text in bold font. It is sometimes an eyesore.

I have corrected it this time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic