In the abstract JDK class Formatter, which is used to format debug logs, the formatMessage method is declared as synchronized.
However, I haven't been able to determine why that is the case.
Obviously, one can write overriding versions that are not thread safe, but I'm wondering why the default implementation is not thread safe.
An early version of java.util.logging.Formatter tried to cache the result of resource bundle get string calls to avoid construction of
java.util.MissingResourceException
. Synchronization was used to guard theHashMap
that was used to cache the lookups.Here is the Copyright 2004 Sun Microsystems, Inc. version 1.16, 12/19/03 source code, note the comments:
When the caching code was commented out the synchronization should have been removed. This issue is filed under JDK-8153666: Possible optimization of Formatter.formatMessage.
From What's New in Javadoc 1.2:
P.S.
record.essage
is an actual typo from the original code.record.getMessage()
called multiple times even though it is stored in a local on first invocation.catalog.getString
which will fail with an NPE.