The synchronized
keyword is used to acquire a mutual-exclusion lock so that no other thread can acquire the lock while it is being held by the executing thread. There are two ways to synchronize access to shared mutable variables, variable: method synchronization and block synchronization.
...
If these restrictions are not met, the object's intrinsic lock is not trustworthy. If all conditions are satisfied, then the object gains no significant security from using a private final lock object, and may synchronize using its own intrinsic lock. However, because classes may be used by trusted code in unexpected waysit is still best to use block synchronization using a private final lock object instead of method synchronization when the method contains non-atomic operations that either do not require any synchronization or can use a more fine-grained locking scheme involving multiple private final lock objects. Non-atomic operations can be decoupled from those that require synchronization and executed outside the synchronized block. For this reason, and for maintainability reasons, block synchronization using a private final lock object is generally recommended.
...