Avoid synchronized(this) in Java?

后端 未结 22 1191
予麋鹿
予麋鹿 2020-11-22 01:23

Whenever a question pops up on SO about Java synchronization, some people are very eager to point out that synchronized(this) should be avoided. Instead, they c

22条回答
  •  无人及你
    2020-11-22 01:41

    I think points one (somebody else using your lock) and two (all methods using the same lock needlessly) can happen in any fairly large application. Especially when there's no good communication between developers.

    It's not cast in stone, it's mostly an issue of good practice and preventing errors.

提交回复
热议问题