DateFormat的使用注意点

xiaoxiao2021-03-01  10

[size=large]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); } } } } [/size]
转载请注明原文地址: https://www.6miu.com/read-3650088.html

最新回复(0)