I am struggling to explain \"deadlock\" in threads in easy words, so please help. What could be the best example of \"deadlock\" (say, in Java), and how it does happen in st
A lock chain occurs when a worker is blocked by another worker. A cannot continue because of B. The chain can be longer: A is blocked by B, B is blocked by C, C is blocked by D.
A deadlock is when the lock chain forms a loop. A is blocked by B, B by C, C by A and the chain had formed a loop, no progress is possible.
The typical way of preventing deadlocks is to use lock hierachies: if locks are always aquired by every worker in the same order, then deadlocks are not possible because each blocking occurs between a worker than holds locks ranked X and waits for resources ranked Y, where X > Y always. A loop cannot form in this case, as it would require at least one worker to go against the hierarchy in order to close the loop. That how the theory goes, at least. In prcatice is very very hard to come up with realistic hierarchies (and no, address of resource does not work).
If deadlocks cannot be avoided (eg. database systems) then the solution is to have dedicated threads that check the deadlock chains in search of loops and kill one of the participants to free the loop.