• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can we prevent deadlock in Multithreading?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
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

Vineet Tyagi wrote:


Vineet, please read the HowToAskQuestionsOnJavaRanch page. Thoroughly.

Simply bashing out vague subject titles and putting a silly smiley in the post won't endear you to the folks here.

Winston
 
Vineet Tyagi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if i was wrong somewhere.. i just want someone to tell me some ways to implement in my code to prevent deadlock as java provides nothing to prevent a deadlock in case of multithreading(as per my knowledge)..
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you read about threading? Have you read the Java Tutorials or Brian Goetz’s book?
 
Vineet Tyagi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What have you read about threading? Have you read the Java Tutorials or Brian Goetz’s book?



I know my question was not specific and not according to the rules of forum. I will do home work and search for it. Then i will be back with the exact problem.
 
Winston Gutkowski
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

Vineet Tyagi wrote:I know my question was not specific and not according to the rules of forum. I will do home work and search for it. Then i will be back with the exact problem.


Great. Thanks for listening, and I look forward to helping you out when you do.

Winston
 
Vineet Tyagi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Vineet Tyagi wrote:I know my question was not specific and not according to the rules of forum. I will do home work and search for it. Then i will be back with the exact problem.


Great. Thanks for listening, and I look forward to helping you out when you do.

Winston


Thanks Winston to guide me about forum rules.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we can prevent deadlock in multithreading by using synchronization keyword
 
Vineet Tyagi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

BalaMurali dhar wrote:we can prevent deadlock in multithreading by using synchronization keyword



But i think synchronization the root of deadlock.. synchronisation is used to prevent concurrency problems
 
Winston Gutkowski
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

Vineet Tyagi wrote:

BalaMurali dhar wrote:But i think synchronization the root of deadlock.. synchronisation is used to prevent concurrency problems


Simple answer is: You can only prevent deadlock by using prevention measures.

In a situation involving only one call to a synchronized method it can't occur; the problem usually occurs when you have calls to more than one sync'ed method in the same piece of code; and the reason is that synchronized on a method doesn't have sufficient granularity to prevent it.

The old database trick I was taught around the K-T extinction event was to always, always, always obtain locks in the same sequence.
You can simulate this by creating object locks (or indeed using ReentrantLock's, which seems to be favoured now) for each "protected" piece of code, and using synchronized blocks rather than methods. It has to be said, that this can sometimes cause you to obtain unnecessary locks, but it is safe.

Other than that: analysis, analysis, analysis.

Winston

 
Winston Gutkowski
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

BalaMurali dhar wrote:we can prevent deadlock in multithreading by using synchronization keyword


Actually, that's what causes it. No synchronization: no deadlock.

Winston
 
Vineet Tyagi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Vineet Tyagi wrote:

BalaMurali dhar wrote:But i think synchronization the root of deadlock.. synchronisation is used to prevent concurrency problems


Simple answer is: You can only prevent deadlock by using prevention measures.

In a situation involving only one call to a synchronized method it can't occur; the problem usually occurs when you have calls to more than one sync'ed method in the same piece of code; and the reason is that synchronized on a method doesn't have sufficient granularity to prevent it.

The old database trick I was taught around the K-T extinction event was to always, always, always obtain locks in the same sequence.
You can simulate this by creating object locks (or indeed using ReentrantLock's, which seems to be favoured now) for each "protected" piece of code, and using synchronized blocks rather than methods. It has to be said, that this can sometimes cause you to obtain unnecessary locks, but it is safe.

Other than that: analysis, analysis, analysis.

Winston



Yes you are very correct. It always needs a lot of analysis. It totally depends on programmer's wisdom. So first i need to create some deadlock prone real code and experiment the things you told me on that.
Thanks for such a useful info.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There should be some deadlock prone code in the tutorials link I sent you earlier.
 
Vineet Tyagi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There should be some deadlock prone code in the tutorials link I sent you earlier.



Yep i am working on that tutorial right now. Thanks Campbell for your favour.
 
Winston Gutkowski
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

Vineet Tyagi wrote:Thanks for such a useful info.


I should perhaps add that your pieces of "protected code" will actually be dealing with 'things' (ie, objects) that you want to protect, and that's where the analysis comes in. If you have 4 things that you want to synchronize: A, B C and D, create a lock for each, and just make sure that every piece of code that has to update them (or read "verified" values) obtains the locks in the same sequence.

Assuming everyone follows the rules, you can ignore locks below the scope; so, assuming that A,B,C,D is the order in which you lock, if you have a piece of code that needs to update C, you can ignore D, but you must include A and B.
As you can imagine, this means that determining the best sequence also takes a bit of analysis. I believe it's called the "paranoid" rule.

It's also highly likely that there are methodologies more suited to Java code, but it's the one I was taught, and it's dead simple to remember.

Winston
 
Vineet Tyagi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Vineet Tyagi wrote:Thanks for such a useful info.


I should perhaps add that your pieces of "protected code" will actually be dealing with 'things' (ie, objects) that you want to protect, and that's where the analysis comes in. If you have 4 things that you want to synchronize: A, B C and D, create a lock for each, and just make sure that every piece of code that has to update them (or read "verified" values) obtains the locks in the same sequence.

Assuming everyone follows the rules, you can ignore locks below the scope; so, assuming that A,B,C,D is the order in which you lock, if you have a piece of code that needs to update C, you can ignore D, but you must include A and B.
As you can imagine, this means that determining the best sequence also takes a bit of analysis. I believe it's called the "paranoid" rule.

It's also highly likely that there are methodologies more suited to Java code, but it's the one I was taught, and it's dead simple to remember.

Winston



very helpful post..
I am feeling good to know that Paranoid Rule..
Now its time for me to implement that rule in real code..
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic