Locking on field or local variable?

后端 未结 5 449
猫巷女王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:35

    You are creating the o object every time your method is called. So, lock won't work. I mean other threads won't wait for the lock to be non signaled and grab control over a resource which this lock controls. Usually a lock objects is a private variable in a class so that all methods look into the same object.

提交回复
热议问题