DecimalFormat.format(double) in different threads
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?
Just use this thread-safe snippet for NumberFormat
:
static ThreadLocal<NumberFormat> numberFormat = new ThreadLocal<NumberFormat>() {
@Override
public NumberFormat initialValue() {
return new DecimalFormat("00000");
}
};
Or in Java 8, as Jesper said in comment:
private static ThreadLocal<NumberFormat> numberFormatter =
ThreadLocal.withInitial(() -> new DecimalFormat("00000"));
While the current implementation may be eventually thread-safe, there is no such guarantee for coming implementations, or for other JREs.
Have you verified that avoiding new DecimalFormat()
is a measurable performance gain in your application?
Current Hotspot implementation for DecimalFormat make the call to DecimalFormat.format(double) thread-safe if you do not call other methods on this instance. However it is strongly advised not to rely on this (maybe) temporary behaviour.
Have you considered using a ThreadLocal
variable to avoid too many new DecimalFormat()
?
精彩评论