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 #6231579 ,Sun 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对象即可。
分享到:
相关推荐
eclipse,findbug, 插件,找錯eclipse,findbug, 插件,找錯eclipse,findbug, 插件,找錯eclipse,findbug, 插件,找錯eclipse,findbug, 插件,找錯
findbug的错误日志文档... 上面的英文解释其实应该说得比较清楚,在Java文档中,已经明确说明了DateFormats 是非线程安全的,而在SimpleDateFormat的Jdk 的Source文件中,我们也找到这么一段注释,说明它不是线程安全的
findbug3,代码检查工具,支持myeclipse2014,jdk1.7下通过
FindBug检查问题指南.docx,findbug常见问题,包括大多数的常见问题
Multithreaded correctness: 多线程正确性 Performance:性能 Security:安全 Dodgy: 欺骗性代码 2、 常见Bug以及处理办法 a) 不需要处理 May expose internal representation by incorporating reference to mutable...
这个经典的插件就不用多说了,当然还有checkstyle。让我们的代码更漂亮吧。 插件安装时,我们只需在eclipse根目录下新建一个【links】目录,然后在里面创建【.link】扩展名的文件,在里面写上类似于【path=F:\\IDE\\...
findbug2,代码检查工具,myeclipse10下通过,2014的话换findbug3
findbug插件,自动排查简单的代码bug
静态代码分析工具,查找代码中的问题,代码优化的好工具
将类引入项目,在不想用findbug检查的代码类或方法上,添加该注释
使用myeclipse8,不支持新版findbug3插件,故共享此插件供大家使用。
NULL 博文链接:https://zxiang.iteye.com/blog/726978
发现程序中的bug,此html对此用stanford的软件进行介绍分析
【Find Bugs——eclipse之BUG检测插件】是一个用于检查类或者JAR文件,以发现可能存在的问题的静态分析工具,可检测出来大量隐含问题,可在编码或单元测试时使用。
Findbug使用指南.docx
excilpse findbug插件
myeclipse插件java代码质量检测 findbug pmd可以发现代码中的问题,实现自动的检测和优化代码,sourcecounter可以统计代码量