Locking on field or local variable?

后端 未结 5 574
猫巷女王i
猫巷女王i 2021-01-17 23:18

After I read this question with an answer from Marc....

I sometimes see people locking on a local variable.

Is this code broken?

         


        
5条回答
  •  我在风中等你
    2021-01-17 23:59

    I, personally, don't see any reason of using this, as lock just sets the special field in the instance of the o, to signaled state. So other threads can check the state of that instance, and based on that execute the code inside the lock statement or wait release of it.

    Having local variable every time will allocate a new instance, so for every thread it will be ok.

    Don't see any meaning in this.

提交回复
热议问题