FindBug:Call to static DateFormat
来源:互联网 发布:天津大悦城it电话 编辑:程序博客网 时间:2024/05/18 16:15
今天在重构代码的过程中碰到了一个问题。使用SimpleDateFormat的问题。
本人今天写了一个类,主要是提供很多静态的方法由其他接口调用,过程中多个方法使用到了日期的格式化,所以我讲SimpleDateFormat声明为了static 变量,结果在使用findBug插件对文件进行检索的时候,碰到了问题。
STCAL: Call to static DateFormat (STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE)
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.
For more information on this see Sun Bug #6231579 and Sun Bug #6178997.
Sun早已发布了这个bug,是由于使用的SimpleDateFormat非线程安全造成的。
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");public String someMethod() { return DATE_FORMAT.format(new Date());}
原来因为采用上面的写法,有的时候可能会造成Bug。 Sun公司的bug记录中也有提到,当服务超载的情况下,就会出现bug。
现在将其改为下面的写法
private static final String DATE_FORMAT = "yyyy-MM-dd";public String someMethod() { return new SimpleDateFormat(DATE_FORMAT).format(new Date());}
现在采用这种写法,每次都会生成新的SimpleDateFormat对象,就不会出现错了。使用FindBug检查一遍,发现问题解决。
0 0
- FindBug:Call to static DateFormat
- 关于Call to static DateFormat 的Findbug警告思考
- 【Findbugs缺陷库】Call to static DateFormat
- Call to method of static java.text.DateFormat
- 解决Call to method of static java.text.DateFormat
- 解决Call to method of static java.text.DateFormat
- FindBugs分析记录[H STCAL]Call to static DateFormat
- findbug
- DateFormat
- DateFormat
- Dateformat
- DateFormat
- DateFormat
- DateFormat
- How to terminate call
- Call to lnusertemp failed
- findbug笔记
- FindBug介绍
- QT 查询数据库
- JAVA基础点汇总(四)
- hbase shell 基本操作命令
- linux CentOS6.5安装mysql-5.6.4
- [objective-c]编译出错 -- NS_REQUIRES_NIL_TERMINATION Error
- FindBug:Call to static DateFormat
- iOS正则表达式(三)
- javascript深入理解js闭包
- Struts2 的ModelDriven 二——ModelDrive
- UVA 494 Kindergarten Counting Game 单词统计
- python操作xml文件详细介绍
- UVa 10651 Pebble Solitaire(状态压缩DP)
- SVN版本控制推荐使用方法
- 一个使用超简单C++封装的网络库(包含服务器端和客户端,跨平台,比较适用于移动终端)