Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

Block synchronization and 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. However, it is more secure to synchronize on an internal private lock object instead of a more accessible lock object (see . See CON04-J. Use the private lock object idiom instead of the Class object's intrinsic locking mechanism) for more information.

Compliant Solution (ReentrantLock)

...