I have to print many formatted decimal values in many threads in parallel. To format the decimal values I use a java.text.DecimalFormat
configured by a pattern.
I am aware of the warning from the java doc of DecimalFormat
:
Decimal formats are generally not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
But I don’t know if this warning applies to my scenario:
I configure the java.text.DecimalFormat
once when the application starts (and store the Formatter
in a final field). After that I ONLY use the format(double)
method.
The reason why I want to do this is: I don’t want to lose performance by creating a new DecimalFormat
instance every time I need to print a formatted number.
I looked at the DecimalFormat.format(double)
code and it looks to be thread safe, but I am not sure.
Could you please confirm that the usage of DecimalFormat.format(double)
is eventually thread safe, when not changing the configuration of the formatter, or explain why it is not?