前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >DateFormat 线程不安全

DateFormat 线程不安全

作者头像
囚兔
发布2018-02-08 10:51:17
6670
发布2018-02-08 10:51:17
举报
文章被收录于专栏:IT杂记

一、测试

测试代码如下: 

代码语言:javascript
复制
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) throws ParseException{
        for (int i = 0; i < 2; i++) {
            Thread t = new Thread(new TestRunnable(), "t-" + i);
            t.start();
        }
    }

    static class TestRunnable implements Runnable {

        @Override
        public void run() {
            while(true) {

                long dateTime = 0;
                String dateStr = "2016-05-09 08:21:02";

                try{
                    Date dt = sdf.parse(dateStr);
                    dateTime = dt.getTime();
                } catch(Exception e){
                    e.printStackTrace();
                }

                if(dateTime < 0) {
                    System.out.println(dateTime);
                }
            }
        }
    }

输出结果:

代码语言:javascript
复制
java.lang.NumberFormatException: For input string: ""
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Long.parseLong(Long.java:453)
	at java.lang.Long.parseLong(Long.java:483)
	at java.text.DigitList.getLong(DigitList.java:194)
	at java.text.DecimalFormat.parse(DecimalFormat.java:1316)
	at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2088)
	at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1455)
	at java.text.DateFormat.parse(DateFormat.java:355)
-125744830738000
-61916917138000
-61916917138000
	at com.tiza.ngp.rp.sanhui.util.Utils$TestRunnable.run(Utils.java:214)
	at java.lang.Thread.run(Thread.java:745)
java.lang.NumberFormatException: For input string: ""
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Long.parseLong(Long.java:453)

结果要么抛异常,要么结果为错误的负值。

二、简单分析

代码语言:javascript
复制
public abstract class DateFormat extends Format {

    /**
     * The {@link Calendar} instance used for calculating the date-time fields
     * and the instant of time. This field is used for both formatting and
     * parsing.
     *
     * <p>Subclasses should initialize this field to a {@link Calendar}
     * appropriate for the {@link Locale} associated with this
     * <code>DateFormat</code>.
     * @serial
     */
    protected Calendar calendar;

    ...
}

DateFormat类使用了Calendar对象来维护parse和format过程中的日期时间值,当多线程同时使用同一个DateFormat对象,也就是多线程同时使用同一个Calendar对象来维护parse或format过程的日期时间值,必定会发生错乱。

引用Java api文档:

Synchronization 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 externally.

三、解决方案

使用中要么为每个线程创建一个DateFormat实例,要么对其外部加锁。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、测试
  • 二、简单分析
  • 三、解决方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档