不建议调用静态 java.text.DateFormat 方法? [英] Call to method of static java.text.DateFormat not advisable?

查看:18
本文介绍了不建议调用静态 java.text.DateFormat 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个 Find Bugs 错误 - 调用静态 java.text.DateFormat 的方法 和我不知道为什么在下面做这些事情不好/不可取.

I am receiving a Find Bugs error - call to method of static java.text.DateFormat and I don't know the reason why it's not good / advisable to be doing these things below.

private static final Date TODAY = Calendar.getInstance().getTime();
private static final DateFormat yymmdd = new SimpleDateFormat("yyMMdd"); 

private String fileName = "file_" + yymmdd.format(TODAY);

推荐答案

DateFormats 不是线程安全的,这意味着它们维护状态的内部表示.如果多个线程同时访问同一个实例,在静态上下文中使用它们会产生一些非常奇怪的错误.

DateFormats are not thread-safe, meaning that they maintain an internal representation of state. Using them in a static context can yield some pretty strange bugs if multiple threads access the same instance simultaneously.

我的建议是让你的变量在你使用它们的地方是本地的,而不是让它们成为类的静态属性.看起来您在初始化类时可能会这样做,因此您可以在构造函数中执行此操作:

My suggestion would be to make your variables local to where you're using them instead of making them static properties of the class. It looks like you might be doing this when you're initializing the class, so you could do this in the constructor:

public class MyClass {
    private String fileName;

    public MyClass() {
        final Date today = Calendar.getInstance().getTime();
        final DateFormat yymmdd = new SimpleDateFormat("yyMMdd"); 

        this.fileName = "file_" + yymmdd.format(TODAY);
    }
    ...
}

如果您需要在多个地方使用格式化程序,您可能只需将模式设为 static final 并在需要时创建一个新的本地 DateFormat:

And if you need to use the formatter in multiple places, you might just make the pattern static final and create a new local DateFormat when needed:

public class MyClass {
    private static final String FILENAME_DATE_PATTERN = "yyMMdd";

    public void myMethod() {
        final DateFormat format = new SimpleDateFormat(FILENAME_DATE_PATTERN);
        // do some formatting
    }
}

针对该问题的 FindBugs 文档 说:

正如 JavaDoc 所述,DateFormats 是对于多线程来说本质上是不安全的利用.检测器已发现呼叫一个 DateFormat 的实例,它有通过静态字段获得.这看起来很可疑.

As the JavaDoc states, DateFormats are inherently unsafe for multithreaded use. The detector has found a call to an instance of DateFormat that has been obtained via a static field. This looks suspicous.

有关这方面的更多信息,请参阅 Sun错误 #6231579 和 Sun 错误 #6178997.

For more information on this see Sun Bug #6231579 and Sun Bug #6178997.

Javadoc for DateFormat 建议:

日期格式不同步.它建议单独创建为每个线程格式化实例.如果多线程访问一个格式同时,它必须是同步的外部.

Date formats are 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.

Jack Leow 的回答 也有一个关于TODAY"静态使用的语义的好点.

Jack Leow's answer also has a good point about the semantics of your static use of "TODAY".

顺便说一句,我在高流量的生产环境中确实看到过这种情况,而且一开始调试是一件非常令人困惑的事情;所以根据我的经验,FindBugs 警告实际上是一个有用的建议(不像其他一些静态分析规则,有时看起来很挑剔).

As an aside, I've actually seen this happen in a high-traffic production environment, and it's a very confusing thing to debug at first; so in my experience the FindBugs warning is actually a useful suggestion (unlike some other static analysis rules, which sometimes seem to be nitpicky).

这篇关于不建议调用静态 java.text.DateFormat 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆