How to explain the “deadlock” better?

前端 未结 15 1029
不知归路
不知归路 2020-12-13 00:52

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

15条回答
  •  难免孤独
    2020-12-13 01:00

    Deadlock is when two threads wait on each other, neither can proceed until the other does first, and so both are stuck.

    Deadlocking requires at least 2 locks, and both threads have to contain code that takes locks and also waits for locks to be released.

    Thread 1 has lock A and wants lock B, so it waits for lock B to be released.

    Thread 2 has lock B and wants lock A, so it waits for lock A to be released.

    Now you have a deadlock. Both threads a waiting for a lock, so neither is executing, so neither can release the lock that the other is waiting for.

提交回复
热议问题