Livelock in Java Multi-Threading

In a multi-threaded environment 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 a livelock may happen if two or more threads are busy responding to the action of each other and unable to make further progress in the process.

If we take example of three threads where-

  1. Thread-1 is acting on a response from Thread-2
  2. Thread-2 is acting on a response from Thread-3
  3. Thread-3 is acting on a response from Thread-1

So these three threads are busy responding to the action of each other thus unable to make any progress because of livelock.

How livelock is different from deadlock

In case of livelock, just like deadlock, threads don’t make any progress but they are not blocked as the case with deadlock. Threads are active but they are busy responding to each other thus not making any progress.

Java livelock example

In the example there are two threads for customer and shop, customer is waiting for the order to be shipped before paying money where as shop is willing to ship order only after getting amount. So both threads are responding to event but busy there itself unless event condition is fulfilled.

Output

That’s all for the topic Livelock in Java Multi-Threading. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.