Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added JLS quote

...

Code Block
bgColor#FFcccc
synchronized(getClass()) { ... }

Wiki Markup
This idea is sometimes easy to miss, especially when one goes by the Java Language Specification \[[JLS 05|AA. Java References#JLS 05]\] section 4.3.2 "The Class Object", that describes how method synchronization works:

A class method that is declared synchronized synchronizes on the lock associated with the Class object of the class.

Compliant Solution

Explicitly define the name of the class (superclass here) in the synchronization block.

...