Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

deadlocks  RSS feed

 
eswar kumar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is deadlock? how it can be avoided ? between how many processes u can achive deadlock?
 
Kethy Seiren
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deadlock is a situation which occurs
when two or more threads are waiting to gain control of a resource,Due to some reason the condition on which waiting threads rely on to gain control doesnot happen.
eg.
thread A must acess method1 before it can release Method2,but ThreadB cannot release Method1 until it gets hold of method2
Thread A
synchronized method2()
{
synchronized method1()
{
.....
.....
}
}
Thread B
synchronized method1()
{
synchronized method2()
{
.....
.....
}
}
if u want detail study of deadlock ,read Books on operating System Concepts
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer to avoiding deadlock is to always access resources in the same order. If I'm holding A and waiting for B and you're holding B and waiting for A we're deadlocked. But if we both get A first then B we'll be sequenced properly.
To hide knowledge about the proper sequence, you might encapsulate it in a command object or a method that everybody always uses instead of direct access to the resources.
I recently read a neat web paper about a clever manager that takes a list of resources, sorts it, and gets locks in the sorted order. Then you can pass it any two or ten things and it will get the locks in the right order. I'll see if I can find that again.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!