Bug: Call to method of static java.text.DateFormat
Pattern id: STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE, type: STCAL, category: MT_CORRECTNESS

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.

上面的英文解释其实应该说得比较清楚,在Java文档中,已经明确说明了DateFormats 是非线程安全的,而在SimpleDateFormat的Jdk 的Source文件中,我们也找到这么一段注释,说明它不是线程安全的。

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

在Sun自己的网站上。在sun的bug database中,Sun Bug #6231579Sun Bug #6178997都可以印证这个问题。

导致SimpleDateFormat出现多线程安全问题的原因,是因为:SimpleDateFormat处理复杂,Jdk的实现中使用了成员变量来传递参数,这就造成在多线程的时候会出现错误。

而Findbugs所说的“Call to static DateFormat”,其实就是一些人:为了渐少new 的次数而把SimpleDateFormat做成成员或者静态成员,上面已经说了,这样做是不安全的。

其实,出现这种问题的代码一般都长得差不多,典型的代码示例如下:

public class Test{
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public void method1(){
dateFormat.format(new Date());
}
public void method2(){
dateFormat.format(new Date());
}
)

再给个详细例子说明问题,看下面代码:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; public class Test{
private SimpleDateFormat dateFormat ;
public static void main(String[] args) {
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
Date tomorrow = new Date(today.getTime()+1000*60*60*24);
System.out.println(today); // 今天是2010-01-11
System.out.println(tomorrow); // 明天是2010-01-11
Thread thread1 = new Thread(new Thread1(dateFormat,today));
thread1.start();
Thread thread2 = new Thread(new Thread2(dateFormat,tomorrow));
thread2.start();
} }
class Thread1 implements Runnable{
private SimpleDateFormat dateFormat;
private Date date;
public Thread1(SimpleDateFormat dateFormat,Date date){
this.dateFormat = dateFormat;
this.date = date;
}
public void run() {
for(;;){// 一直循环到出问题为止吧。
String strDate = dateFormat.format(date);
// 如果不等于2010-01-11,证明出现线程安全问题了!!!!
if(!"2010-01-11".equals(strDate)){
System.err.println("today="+strDate);
System.exit(0);
}
}
}
}
class Thread2 implements Runnable{
private SimpleDateFormat dateFormat;
private Date date;
public Thread2(SimpleDateFormat dateFormat,Date date){
this.dateFormat = dateFormat;
this.date = date;
}
public void run() {
for(;;){
String strDate = dateFormat.format(date);
if(!"2010-01-12".equals(strDate)){
System.err.println("tomorrow="+strDate);
System.exit(0);
}
}
}
}

运行的结果如下:

Mon Jan 11 11:30:36 CST 2010
Tue Jan 12 11:30:36 CST 2010
tomorrow=2010-01-11

终于看到问题了,tomorrow=2010-01-11,错得很明显了。其实要避免这个问题方法很简单,不使用SimpleDateFormat,或者不使用成员变量/静态成员变量的SimpleDateFormat对象即可。

今天解决了一个findbug的错误:

Call to method of static java.text.DateFormat in com.pbn.oss.resource.template.input.ws.service.InputTemplateWSServiceImpl.formatDate(Date)

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.

初始代码如下:

  1. private static final SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
  2. format.format(new Date());
  1. 同一个format 多次调用会导致线性不安全,

可以改为:

    1. private static final String COMMON_DATE = "dd/MM/yyyy";
    2. SimpleDateFormat format = new SimpleDateFormat(COMMON_DATE);
    3. format.format(new Date());

参考资料

【1】.《使用SimpleDateFormat必须注意的问题

Call to static DateFormat的更多相关文章

  1. FindBug:Call to static DateFormat

    今天在重构代码的过程中碰到了一个问题.使用SimpleDateFormat的问题. 本人今天写了一个类,主要是提供很多静态的方法由其他接口调用,过程中多个方法使用到了日期的格式化,所以我讲Simple ...

  2. DateFormat 中间加别的字符

    private static DateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

  3. Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(4) DateFormat

    本章主要介绍DateFormat. DateFormat 介绍 DateFormat 的作用是 格式化并解析“日期/时间”.实际上,它是Date的格式化工具,它能帮助我们格式化Date,进而将Date ...

  4. 详解Java中格式化日期的DateFormat与SimpleDateFormat类

    DateFormat其本身是一个抽象类,SimpleDateFormat 类是DateFormat类的子类,一般情况下来讲DateFormat类很少会直接使用,而都使用SimpleDateFormat ...

  5. Date、Calendar、DateFormat、SimpleDateFormat、Timer、TimerTask类

    类 Date 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值. 它也允许格式化和解析日期字符串.不过,这些函数的 API 不易于实现国际化.从 ...

  6. java中Dateformat类的详细使用(详解)

    DateFormat其本身是一个抽象类,SimpleDateFormat 类是DateFormat类的子类,一般情况下来讲DateFormat类很少会直接使用,而都使用SimpleDateFormat ...

  7. Java—时间的原点 计算时间所使用的 Date类/DateFormat类/Calendar类

    Date类 类 Date 表示特定的瞬间,精确到毫秒. 毫秒概念:1000毫秒=1秒 毫秒的0点: System.currentTimeMillis()  返回值long类型参数 用于获取当前日期的毫 ...

  8. 从接口、抽象类到工厂模式再到JVM来总结一些问题

    俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习! 涉及到的知识点总结如下: 为什么使用接口? 接口和抽象类的区别 简单工厂模式总结 Java中new和newInstance的区别 J ...

  9. Java中一些常用的方法

    1.计算程序运行时常 long start = System.currentTimeMillis(); … … … long end = System.currentTimeMillis(); Sys ...

随机推荐

  1. 【转载】GCC 预处理器选项

    预处理器选项(Preprocessor Option) 下列选项针对C预处理器,预处理器用在正式编译以前,对C 源文件进行某种处理. 如果指定了`-E'选项, GCC只进行预处理工作.下面的某些选项必 ...

  2. Sql性能检测工具:Sql server profiler和优化工具:Database Engine Tuning Advisor

    原文:Sql性能检测工具:Sql server profiler和优化工具:Database Engine Tuning Advisor 一.工具概要     数据库应用系统性能低下,需要对其进行优化 ...

  3. 【java】java反射 Field类的研究使用

    java反射 Field类的研究使用 user.getClass().getFields() 和 user.getClass().getDeclaredFields(); 的区别是什么?

  4. javascript 对象属性的添加,删除,json对象和字符串转换方法等

    1:动态添加 对象属性 var obj = new Object(); console.log (obj.username); obj.username = "haha"; con ...

  5. axure产品原型图,元件库导入方法

    axure是公司产品经理常用的软件,用来绘制产品原型图. 为了画出漂亮的原型图,需要各色各样的元件.因此导入各类元件库很有用处: 打开元件库视图: 载入元件库,选择事先下载好的元件 元件库下载:htt ...

  6. LINQ获取两个List的交集

    1.调用: UserList = UserList.ToList().Intersect(userIDList, new MyUserComparer()).AsQueryable(); 2.须要重写 ...

  7. Elasticsearch 基础使用

    使用 cURL 执行 REST 命令 可以对 Elasticsearch 发出 cURL 请求,这样很容易从命令行 shell 体验该框架. “Elasticsearch 是无模式的.它可以接受您提供 ...

  8. android-SQLite数据库MVC关联实例源码(三层架构)

    前两天布置下了一个期末练习,其中的重点是两个表之间的SQLite关联操作. 拿到题目,首先需要建库和关联表,下面是代码. 我使用简单的表插入,将数据的提交卸载onCreate方法中,这样不会发生写在主 ...

  9. hibernate学习系列-----(2)hibernate核心接口和工作机制

    在上一篇文章hibernate学习系列-----(1)开发环境搭建中,大致总结了hibernate的开发环境的搭建步骤,今天,我们继续了解有关hibernate的知识,先说说这篇文章的主要内容吧: C ...

  10. Win7梦幻桌面字体有问题怎么办

      1:首先,下载我提供的压缩文件,解压"Win7中文界面梦幻桌面补丁",得到若干文件,这时可能需要管理员权限,详细的压缩包里有说明.      2: 然后在桌面上单击右键,可以发 ...