`
- 浏览:
316692 次
- 性别:
- 来自:
深圳
-
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做成成员或者静态成员,上面已经说了,这样做是不安全的。
其实,出现这种问题的代码一般都长得差不多,典型的代码示例如下:
===================================================================================
package test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateFormat
{
public static void main(String[] args)
{
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
Date tomorrow = new Date(today.getTime() + 1000 * 60 * 60 * 24);
System.out.println("当前日期为:" + dateFormat.format(today));
System.out.println("明天日期为:" + dateFormat.format(tomorrow));
new Thread(new TestThreadToday(dateFormat, today)).start();
new Thread(new TestThreadTomorrow(dateFormat, tomorrow)).start();
}
}
class TestThreadToday implements Runnable
{
private DateFormat dateFormat;
private Date currentDate;
public TestThreadToday(DateFormat format, Date date)
{
this.dateFormat = format;
this.currentDate = date;
}
public void run()
{
// TODO Auto-generated method stub
final String currentDateStr = dateFormat.format(currentDate);
while(true)
{
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
}
String tempDateStr = dateFormat.format(currentDate);
System.out.println(Thread.currentThread().getName() + " : 当前日期 : " + tempDateStr);
if (false == tempDateStr.equals(currentDateStr))
{
System.out.println("日期格式化出现异常!!当前日期为:" + tempDateStr);
System.exit(0);
}
}
}
}
class TestThreadTomorrow implements Runnable
{
private DateFormat dateFormat;
private Date tomorrow;
public TestThreadTomorrow(DateFormat format, Date date)
{
this.dateFormat = format;
this.tomorrow = date;
}
public void run()
{
// TODO Auto-generated method stub
final String tomorrowDateStr = dateFormat.format(tomorrow);
while(true)
{
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
}
String tempDateStr = dateFormat.format(tomorrow);
System.out.println(Thread.currentThread().getName() + " : 明天日期 : " + tempDateStr);
if (false == tempDateStr.equals(tomorrowDateStr))
{
System.out.println("日期格式化出现异常!!明天日期为:" + tempDateStr);
System.exit(0);
}
}
}
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
目录: 1.Date 2.DateFormat ...注意这里重写了toString方法按照一定的格式去输出时间(toString把他变成字符串输出) (3)常用方法: 如图: 2.DateFormat (1)所在位置java.text.DateFormat (2)Date
5.8.1 使用继承的注意点 155 5.8.2 利用组合实现复用 156 学生提问:使用组合关系来实现复用时,需要创建两个Animal对象,是不是意味着使用组合关系时系统开销更大? 159 5.9 初始化块 159 5.9.1 使用初始化块 ...
注意:对于Amazon Elasticsearch Service,请考虑使用 当前的维护者:@ cosmo0920 cloud_auth 产生错误 主机 用户,密码,路径,方案,ssl_verify logstash_format logstash_prefix logstash_prefix_...
如果您使用date字段类型,请注意您可以使用dateFormat选项定义日期格式。 var EdiFile = require ( '../index' ) . EdiFile ; var ediFile = new EdiFile ( { "0" : [ { name : 'user_name' , start : 2 , ...
9.3.2 使用BCP或者BULK INSERT时的注意事项 286 9.3.3 外部数据文件位置及文件访问权限问题 287 9.4 在客户端实现SQL Server数据导入与导出处理 288 9.4.1 在Access数据库中实现与SQL Server之间的数据交换 ...
plugins " : [[ " @cerberus/transform " ,{ " modules " : [ " dateformat " ]}]]} 注意: modules中设置的module名称和npm保持一致,在Cerberus库中导出的名字也必须一致。选项type Option={ /** * 不需要被打包的...
9.3.2 使用BCP或者BULK 时的注意事项 286 9.3.3 外部数据文件位置及文件访问权限问题 287 9.4 在客户端实现SQL Server数据导入与导出处理 288 9.4.1 在Access数据库中实现与SQL Server之间的数据交换 288...
4.9 DateFormat类 36 4.10包装类 37 4.11 BigDecimal类 38 4.12 BigInteger类 38 4.13 Collection集合框架 38 4.14 List集合的实现类ArrayList和LinkedList 39 4.15 Iterator迭代器 42 4.16泛型 43 4.17增强型for...
Jeecms是基于Spring注解,在自定义标签时对于实体类和dao service等注意注解的问题。 五 自定义标签及使用自己创建的表的实现过程 下面是我自己定义的标签mycontent_list 首先,在数据库里创建了一个jc_...
投寄箱搜寻索引并搜索存储在Dropbox中的文档内容。... 使用随附的solr / schema.xml文件,注意标记为EDIT dropbox-search 如下编辑环境变量。 npm install下载依赖项(solr,dbox,express,dateformat)。 node inde
compress (默认为false ):通过gzip为所有旋转的日志启用压缩dateFormat (默认为YYYY-MM-DD_HH-mm-ss ):使用的数据格式名称为log文件rotateModule (默认为true ):像其他应用一样旋转rotateModule模块的日志...
在第二章中,全面地讲解Java的基本语法知识,对基本语法的讲解也不是泛泛而谈,而是在其中贯穿各种实际应用中的巧妙用法和注意事项。在第三章和第四章中,透彻系统地讲解了面向对象的思想和应用。在以后的章节中,用...
对于Java中Date日期类的有关问题做下的思维导图,便于深化记忆,希望对你有帮助!注意:文件为.mm后缀建议下载freemind打开,免费的思维导图软件!
DateFormat 返回每月的第一天和最后一天 复制代码 TimeHelper 1.将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间 2.将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间 3.把秒转换...
注意: h:12小时制小时数 H:24小时制小时数 实例代码: import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util....
031311_【第13章:Java类集】_Map接口使用的注意事项笔记.pdf 031312_【第13章:Java类集】_IdentityHashMap类笔记.pdf 031313_【第13章:Java类集】_SortedMap类笔记.pdf 031314_【第13章:Java类集】_集合工具类:...
void setSeriesBarWidth(int series,double width) 设定每个分类的宽度(注意设置不要使某分类被覆盖) WaterfallBarRenderer(BarRenderer)类: void setFirstBarPaint(Paint paint) 第一个柱图的颜色 void ...
=====注意:一共2个压缩分卷,这是第1个!===== DevExpress VCL v2012 vol 1.6 Delphi VCL. 包含所有源码、例子、帮助 支持Metro啦! What's New in DevExpress VCL 12.1.6 Release Notes The following ...
=====注意:一共2个压缩分卷,这是第1个!===== DevExpress VCL v2012 vol 1.6 Delphi VCL. 包含所有源码、例子、帮助 支持Metro啦! What's New in DevExpress VCL 12.1.6 Release Notes The following ...
=====注意:一共2个压缩分卷,这是第2个!===== DevExpress VCL v2012 vol 1.6 Delphi VCL. 包含所有源码、例子、帮助 支持Metro啦! What's New in DevExpress VCL 12.1.6 Release Notes The following ...