An application runs in timely manner with multiple threads, which we can call as concurrent behavior of an application. But there are some problems which can occur when application deals with multiple threads. These problems are called as Deadlock, starvation and livelock. Lets see it one by one.
Deadlock is a situation where two threads waits for each other and result of which both the threads goes into the blocked state. In simple way it is a situation like two gentlemen while going inside the bus asking each other to enter first and bus leaves. These kind of deadlock happens in case of multi threading only i.e. a program is using multiple threads and these thread uses multiple locks. So program with one lock or a program with single thread cant have a deadlock.
Starvation as a name suggest is the case where a thread do not get access to the shared resource and due to that affect the progress of a thread. This happens mainly because some big or greedy threads gain the access of resources for long time. So starvation happens because of following reasons :
1. High priority threads uses resources for long time and due to which affects the access of lower priority threads.
2. If thread which is waiting on an object waits for indefinite time.
3. Third and most important is the synchronize method which takes lot of time to return.
As we all know threads have mechanism to respond between them and take action according to that. If one thread is acting in response to another thread, and the other thread is also acting in accordance with different thread then these threads gets busy in handling responses of each other and which affect their progress seriously. This situation is called as a Livelock. It is a situation something opposite to deadlock for some extent as instead of waiting for each other these threads goes into the loop where they gets busy in responding each other.
- Blogger Comment
- Facebook Comment