synchronizing by this vs dummy object

后端 未结 2 1879
生来不讨喜
生来不讨喜 2021-01-25 02:02

I have seen usage of synchronized block by this so far but recently I learned that using dummy object is preferable. I found the following topic related to this.

Java sy

2条回答
  •  隐瞒了意图╮
    2021-01-25 02:33

    You are correct. In this case you need two different objects to synchronize on them separately.

    For locking purpose the easiest way is to create Object objects.

    Object lock1 = new Object();
    Object lock2 = new Object();
    

提交回复
热议问题