Don't forget to create account on our site to get access to more material made only for free registered user.  

Don't forget to create account on our site to get access to more material made only for free registered user.  

 

Deadlock: Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.

 

Deadlock is a condition in which two threads wait for each other to take action which allows them to move further. It's a serious issue because when it happens, your program hangs and doesn't do the task it is intended for. In order for deadlock to happen, following four condition must be true:

 

  • Mutual Exclusion: At least one resource must be held in a non-shareable mode. Only one process can use the resource at any given instant of time.

  • Hold and Wait: A process is currently holding at least one resource and requesting additional resources which are being held by other processes.

  • No Pre-emption: The operating system must not de-allocate resources once they have been allocated; they must be released by the holding process voluntarily.

  • Circular Wait: A process must be waiting for a resource which is being held by another process, which in turn is waiting for the first process to release the resource.

 

Avoid Deadlock: Easiest way to avoid deadlock is to prevent Circular wait, and this can be done by acquiring locks in a particular order and releasing them in reverse order, so that a thread can only proceed to acquire a lock if it held the other one. Some quick tips :

 

  • don't use multiple threads (like Swing does, for example, by mandating that everything is done in the EDT)

  • Don’t hold several locks at once. If you do, always acquire the locks in the same order

  • don't execute foreign code while holding a lock

  • use interruptible locks

 

 

Starvation and Livelock: Starvation and livelock are much less common a problem than deadlock, but are still problems that every designer of concurrent software is likely to encounter.

 

Starvation: Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This happens when shared resources are made unavailable for long periods by "greedy" threads. For example, suppose an object provides a synchronized method that often takes a long time to return. If one thread invokes this method frequently, other threads that also need frequent synchronized access to the same object will often be blocked.

 

Livelock: A thread often acts in response to the action of another thread. If the other thread's action is also a response to the action of another thread, then livelock may result. As with deadlock, livelocked threads are unable to make further progress. However, the threads are not blocked — they are simply too busy responding to each other to resume work. This is comparable to two people attempting to pass each other in a corridor: Alphonse moves to his left to let Gaston pass, while Gaston moves to his right to let Alphonse pass. Seeing that they are still blocking each other, Alphone moves to his right, while Gaston moves to his left. They're still blocking each other, so. Livelock is a special case of resource starvation. A real-world example of livelock occurs when two people meet in a narrow corridor, and each tries to be polite by moving aside to let the other pass, but they end up swaying from side to side without making any progress because they both repeatedly move the same way at the same time.

You have no rights to post comments

Don't forget to create account on our site to get access to more material made only for free registered user.