synchronizing by this vs dummy object

陌路散爱 提交于 2019-12-02 07:26:07

That is exactly the point of lock objects - you can use different locks for different operations. Assuming it makes sense to run addA and addB concurrently (and from the looks of it - it definitely does), you should indeed have two separate locks, one for each method.

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!