java 不同线程中的 DecimalFormat.format(double)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4387170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DecimalFormat.format(double) in different threads
提问by Ralph
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
:
我必须在许多线程中并行打印许多格式化的十进制值。为了格式化十进制值,我使用java.text.DecimalFormat
由模式配置的。我知道来自 java doc 的警告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.
但我不知道这个警告是否适用于我的场景:我java.text.DecimalFormat
在应用程序启动时配置了一次(并将其存储Formatter
在最后一个字段中)。之后我只使用该format(double)
方法。
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.
我想要这样做的原因是:我不想在DecimalFormat
每次需要打印格式化数字时创建一个新实例而损失性能。
I looked at the DecimalFormat.format(double)
code and it looks to be thread safe, but I am not sure.
我查看了DecimalFormat.format(double)
代码,它看起来是线程安全的,但我不确定。
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?
您能否DecimalFormat.format(double)
在不更改格式化程序的配置时确认使用最终是线程安全的,或者解释为什么不是?
采纳答案by mfx
While the current implementation may be eventually thread-safe, there is no such guarantee for coming implementations, or for other JREs.
虽然当前的实现可能最终是线程安全的,但对于即将到来的实现或其他 JRE 没有这样的保证。
Have you verified that avoiding new DecimalFormat()
is a measurable performance gain in your application?
您是否已确认避免new DecimalFormat()
在您的应用程序中是一种可衡量的性能提升?
回答by Dariusz
Just use this thread-safe snippet for NumberFormat
:
只需将此线程安全代码段用于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:
或者在 Java 8 中,正如 Jesper 在评论中所说:
private static ThreadLocal<NumberFormat> numberFormatter =
ThreadLocal.withInitial(() -> new DecimalFormat("00000"));
回答by Guillaume
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.
如果您不在此实例上调用其他方法,则 DecimalFormat 的当前热点实现将调用 DecimalFormat.format(double) 线程安全。但是强烈建议不要依赖这种(可能)临时行为。
Have you considered using a ThreadLocal
variable to avoid too many new DecimalFormat()
?
您是否考虑过使用ThreadLocal
变量来避免太多new DecimalFormat()
?