Imagine you’re talking with your friend on your mobile phone and the call drops. You try to reconnect with her, but her phone is busy (because she’s trying to call you!). You wait for a few moments and try to reconnect, only to discover that her phone is still busy, because she waited for exactly the same duration before trying to reconnect again. If you compare yourself and your friend with threads, you both are in a livelock. You and your friend aren’t blocked—both of you are responding but aren’t being able to do what you both intend to (talk with each other).
Threads in a livelock aren’t blocked; they’re responding to each other, but they aren’t able to move to completion.
Because a livelock depends on the exact time of execution of threads, the same set of threads might not always livelock.
Livelock describes situation where two threads are busy responding to actions of each other. They keep repeating a particular code so the program is unable to make further progress:
Thread 1 acts as a response to action of thread 2
Thread 2 acts as a response to action of thread 1
Unlike deadlock, threads are not blocked when livelock occurs. They are simply too busy responding to each other to resume work. In other words, the program runs into an infinite loop and cannot proceed further.
A Livelock Example:
1)
Let’s see an example: a criminal kidnaps a hostage and he asks for ransom in order to release the hostage. A police agrees to give the criminal the money he wants once the hostage is released. The criminal releases the hostage only when he gets the money. Both are waiting for each other to act first, hence livelock.