Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: reworded a bit

...

Block synchronization is preferable over method synchronization because it enables reduction of the duration for which the lock is held. Although this code example has no reduction of duration, synchronizing on a block permits safe operations to be performed in the method outside of the synchronized block in the future.This is because statements that do not require synchronization can be safely moved out of the synchronized block. This compliant solution requires all statements to be synchronized and consequently, is comparable to the previous compliant solution with respect to performance.

Block synchronization when used in conjunction with a private internal lock object Block synchronization also protects against denial of service attacks. Block synchronization does not require synchronizing on an internal private lock object instead of the intrinsic lock of the class's object (this reference). However, it is more secure to synchronize on an internal private lock object instead of a more accessible lock object. See CON04-J. Use the private lock object idiom instead of the Class object's intrinsic locking mechanism for more information.

...