When would you ever use nested locking?

前端 未结 3 1027
轮回少年
轮回少年 2021-01-07 23:22

I was reading in Albahari\'s excellent eBook on threading and came across the following scenario he mentions that \"a thread can repeatedly lock the same object in a nested

3条回答
  •  既然无缘
    2021-01-07 23:53

    Lets say you have two public methods, A() and B(), which both need the same lock.

    Furthermore, let's say that A() calls B()

    Since the client can also call B() directly, you need to lock in both methods.
    Therefore, when A() is called, B() will take the lock a second time.

提交回复
热议问题