How to explain the “deadlock” better?

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

    Another good way to demonstrate a deadlock is with SQL Server.

    Using transactions with different isolation levels, you can demonstrate how one transaction will wait indefinitely for a table which is locked by another transaction.

    The plus here, is you can demonstrate it with SQL Management Studio. I've used this in the past to explain deadlocks to people whilst teaching "Introduction to SQL Server" level training courses.

    Most participants have trouble with the theory, but it all (usually) becomes clear when they see it in action.

    In short: Transaction A (which has not completed) takes an explicit table lock. A second Transaction B attempts to read from the table locked by Transaction A. Transaction B is deadlocked until Transaction A commits or is rolled back.

    You could explain this in code fairly easily enough by creating two separate threads which, in turn, create the Transactions. Hope it helps.

    0 讨论(0)
  • 2020-12-13 01:24

    I'd rather explain it in terms totally unrelated to computers since that's often the best way to get an idea across.

    I have a five-year-old son and a three-year-old daughter. Both want to do the same colouring-in book.

    The daughter grabs the pencils while the son grabs the book. Neither will relinquish what they have until they get the other.

    That's deadlock. It doesn't get any simpler than that.

    Your processes (or children) are stuck waiting for each other and will continue waiting indefinitely until some other superior process (like Dad) comes in and breaks the deadlock.

    At least with the children, you can (sometimes) get one of them to see reason and relinquish their lock. This is not usually possible with computers since the processes are not doing anything except waiting for that resource (although sometimes children enter this state as well).

    Following one rule will guarantee that deadlock cannot occur:

    • Have all threads of execution allocate resources in the same order.

    Following some extra rules will make your threads less likely to slow each other down but keep in mind that the above rule should take precedence over all others:

    • Allocate resources only when you need them.
    • Release them as soon as you're finished with them.
    0 讨论(0)
  • 2020-12-13 01:25

    Imagine a criminal holding a hostage and asking for a ransom. You show up with a suitcase full of money.

    The criminal will never release the hostage before he gets the money. You will never release the money before you get the hostage. Deadlock.


    The analogy here are:

    • You and the criminal are the threads
    • The suitcase full of money and the hostage are the resources
    0 讨论(0)
提交回复
热议问题