• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to remove Deadlock ?

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 262
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?)
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic