不同线程中的 DecimalFormat.format(double)

发布于 2024-10-07 02:23:37 字数 656 浏览 0 评论 0原文

我必须在许多线程中并行打印许多格式化的十进制值。为了格式化十进制值,我使用由模式配置的java.text.DecimalFormat。 我知道 DecimalFormat 的 java 文档发出警告:

十进制格式通常不是 同步。建议 创建单独的格式实例 每个线程。如果是多线程 同时访问一种格式,它必须 外部同步。

但我不知道这个警告是否适用于我的场景: 我在应用程序启动时配置java.text.DecimalFormat一次(并将Formatter存储在最终字段中)。之后我只使用 format(double) 方法。

我想这样做的原因是:我不想每次需要打印格式化数字时都创建一个新的 DecimalFormat 实例来损失性能。

我查看了 DecimalFormat.format(double) 代码,它看起来是线程安全的,但我不确定。

您能否确认,在不更改格式化程序配置的情况下,DecimalFormat.format(double) 的使用最终是线程安全的,或者解释一下为什么不是?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情出卖 2024-10-14 02:23:38

虽然当前的实现最终可能是线程安全的,但对于未来的实现或其他 JRE 并没有这样的保证。

您是否已验证避免使用 new DecimalFormat() 可以在您的应用程序中带来可衡量的性能提升?

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?

内心激荡 2024-10-14 02:23:38

如果您不在该实例上调用其他方法,DecimalFormat 的当前 Hotspot 实现将使对 DecimalFormat.format(double) 的调用成为线程安全的。但是,强烈建议不要依赖这种(可能)临时行为。

您是否考虑过使用 ThreadLocal 变量来避免过多的 new DecimalFormat() ?

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()?

七七 2024-10-14 02:23:37

只需将这个线程安全代码片段用于 NumberFormat

static ThreadLocal<NumberFormat> numberFormat = new ThreadLocal<NumberFormat>() {
    @Override
    public NumberFormat initialValue() {
        return new DecimalFormat("00000");
    }
};

或者在 Java 8 中,正如 Jesper 在评论中所说:

private static ThreadLocal<NumberFormat> numberFormatter = 
                  ThreadLocal.withInitial(() -> new DecimalFormat("00000"));

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"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文