Synchronized Block within Synchronized Method

前端 未结 4 630
一生所求
一生所求 2021-01-02 22:39

I\'m looking at some code in a third party library that contains a synchronized method, and within this method there is a synchronized block that locks on an instance variab

4条回答
  •  长情又很酷
    2021-01-02 22:56

    I will give a real life example to explain what Andy explained through code(to those who are finding it difficult to understand this):

    Suppose You have a 1 BHK Flat .

    1. The way to enter in room is through hall (you have to first enter into hall to enter into room)
    2. You want to restrict some one to use you room , but he can use/enter in the hall.

    In this case if someone enters into room from back door and applies lock from inside. One can only enter into the hall and cant enter into the room until the person inside the room releases the lock .

    Hope this clarifies to the people who are finding it difficult to understand this.

提交回复
热议问题