1

Good example of livelock? gives and excellent example of livelock, which occurs when writing fancy code to avoid deadlock situation. Thread acquires lock A, then tries to acquire lock B. If lock B is not free, it frees lock A, then after random time, acquires lock A and again tries to acquire lock B.
My question is, I have a running code with lots of threads. How can I detect if there is a livelock situation, and which threads are livelocks?
Some folks say there is no tool support. In which case are we consigned to living with livelocks forever? I guess it should be at least, manually possible to detect by reading thread dump? Is it possible to detect if there is a livelock at all, and where is it happening? Is it possible to use a tool, or vm arg, or some kind of debugging to find if my app is undergoing livelock?

Apurva Singh
  • 3,718
  • 3
  • 24
  • 35
  • 2
    Time out the backoff. If you've backed off *N* times, and have still not made progress, then you're probably livelocked. – user15187356 Mar 07 '21 at 20:55
  • A general approach: If something (e.g. a thread) is not responding, there are two possibilities: Either it does something that just takes time or you have a liveness issue. In each case, you should take a look at it. – dan1st Mar 07 '21 at 21:45
  • The easy way to prevent deadlock is to always acquire locks in the same order. Then you don't need deadlock or livelock detection. – user207421 Mar 07 '21 at 23:26

0 Answers0