How to explain the “deadlock” better?

前端 未结 15 996
不知归路
不知归路 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:03

    (Slightly over-simplified) There are two people, screwing nuts onto bolts.

    The procedure (same for both) is:

    1. Pick up a nut or a bolt
    2. Pick up a bolt or a nut (whichever you don't already have)
    3. Screw the nut onto the bolt
    4. Place the finished assembly into the "Finished" pile.
    5. if nuts and bolts remain, go to step 1

    So what happens when there is just a nut and a bolt left? The first person picks up a nut, the second grabs a bolt. So far so good, but now they are stuck, each having a resource the other needs.

    Without special instructions they will sit there deadlocked forever.

    Or you could just show them this video

提交回复
热议问题