SimpleDateFormat多线程问题

       SimpleDateFormat多线程问题

        四月份在优化一个功能时候,尝试把Date和SimpleDateFormat封装在class中,然后统一format和parse解析,以此避免重复new对象节省重复申请内存。于是就遇到了一个坑——经常出现莫名其妙的日期,打印出来时候甚至有0650年的日期,唐朝啊,难道计算机给我玩穿越?!,大家都知道一般日期是1970-01-01开始的,要么再早一些事1900-01-01开始的,不会再早了。

       一开始怀疑事输入问题造成的,但是经过一番调试发现传入数据没错,传入数据在经过统一对象Format和parse后就出现日期不对,怀疑是SimpleDateFormat的问题。于是查阅官方文档

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

文档最后特定对多线程做如下描述

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.

 

官方文档说的明明白白,SimpleDateFormat不是线程安全的,如果需要多线程使用,那么需要显示synchronized同步。

首先定义日志打印类

private void log(String message) {
    StackTraceElement[] elements = Thread.currentThread().getStackTrace();
    android.util.Log.d(TAG, elements[5].getMethodName() + "() " + message);
}

在没有加锁时的例子

@Test
public void multiThreadTest() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    final long timeMillSec = Calendar.getInstance().getTimeInMillis();
    List<Thread> threadList = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        Thread t = new Thread(new DateFormatTask(dateFormat, timeMillSec));
        t.setName("thread-" + i);
        t.start();
        threadList.add(t);
    }

    try {
        Thread.sleep(3000L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

/**
 * SimpleDateFormat测试,没有加锁版本
 */
private class DateFormatTask implements Runnable {
    private DateFormat dateFormat;
    private long timeMillSec = 0;

    private DateFormatTask(DateFormat dateFormat, long timeMillSec) {
        this.dateFormat = dateFormat;
        this.timeMillSec = timeMillSec;
    }

    @Override
    public void run() {
        String dateText = dateFormat.format(new Date(timeMillSec));
        long parseTimeMillSec = 0L;
        try {
            parseTimeMillSec = dateFormat.parse(dateText).getTime();
            String newDateText = dateFormat.format(new Date(parseTimeMillSec));
            log(Thread.currentThread().getName() + " timeMillSec: " + timeMillSec
                    + ", dateText: " + dateText
                    + ", parseTimeMillSec: " + parseTimeMillSec
                    + ", newDateText: " + newDateText);
        } catch (ParseException e) {
            log(Thread.currentThread().getName() + ", parse failed. "
                    + e.getClass().getSimpleName() + " " + e.getMessage()
                    + " timeMillSec: " + timeMillSec);
        } catch (Exception e) {
            log(Thread.currentThread().getName() + ", parse failed. "
                    + e.getClass().getSimpleName() + " " + e.getMessage()
                    + " timeMillSec: " + timeMillSec);
        }
    }
}

  输出结果

05-04 22:28:21.871  3337  3456 D SimpleDateFormatTest: run() sync-thread-1 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.872  3337  3455 D SimpleDateFormatTest: run() sync-thread-0 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.872  3337  3458 D SimpleDateFormatTest: run() sync-thread-3 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.872  3337  3457 D SimpleDateFormatTest: run() sync-thread-2 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.873  3337  3459 D SimpleDateFormatTest: run() sync-thread-4 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.873  3337  3460 D SimpleDateFormatTest: run() sync-thread-5 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.874  3337  3461 D SimpleDateFormatTest: run() sync-thread-6 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.874  3337  3462 D SimpleDateFormatTest: run() sync-thread-7 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.874  3337  3463 D SimpleDateFormatTest: run() sync-thread-8 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.875  3337  3464 D SimpleDateFormatTest: run() sync-thread-9 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.875  3337  3465 D SimpleDateFormatTest: run() sync-thread-10 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.876  3337  3466 D SimpleDateFormatTest: run() sync-thread-11 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.876  3337  3467 D SimpleDateFormatTest: run() sync-thread-12 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.876  3337  3469 D SimpleDateFormatTest: run() sync-thread-14 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.877  3337  3470 D SimpleDateFormatTest: run() sync-thread-15 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.877  3337  3468 D SimpleDateFormatTest: run() sync-thread-13 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.878  3337  3471 D SimpleDateFormatTest: run() sync-thread-16 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.878  3337  3473 D SimpleDateFormatTest: run() sync-thread-17 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.878  3337  3474 D SimpleDateFormatTest: run() sync-thread-18 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:28:21.879  3337  3475 D SimpleDateFormatTest: run() sync-thread-19 timeMillSec: 1588602501868, dateText: 2020-05-04 22:28:21, parseTimeMillSec: 1588602501000, newDateText: 2020-05-04 22:28:21
05-04 22:30:47.752  4040  4163 D SimpleDateFormatTest: run() thread-6 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: -62125090153000, newDateText: 0001-05-04 22:30:47
05-04 22:30:47.752  4040  4166 D SimpleDateFormatTest: run() thread-9 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: -62125090153000, newDateText: 0001-05-04 22:30:47
05-04 22:30:47.753  4040  4168 D SimpleDateFormatTest: run() thread-11 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: 1588602600000, newDateText: 2020-05-04 22:30:00
05-04 22:30:47.753  4040  4170 D SimpleDateFormatTest: run() thread-13 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: 1588602647000, newDateText: 2020-05-04 22:30:47
05-04 22:30:47.753  4040  4157 D SimpleDateFormatTest: run() thread-0 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: 1575122474000, newDateText: 2019-11-30 22:01:14
05-04 22:30:47.753  4040  4164 D SimpleDateFormatTest: run() thread-7 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: 1575122474000, newDateText: 2019-11-30 22:01:14
05-04 22:30:47.753  4040  4179 D SimpleDateFormatTest: run() thread-16 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: 95615325030000, newDateText: 4999-00-09 00:30:47
05-04 22:30:47.753  4040  4160 D SimpleDateFormatTest: run() thread-3 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: -62108839757000, newDateText: 0001-02-05 06:43:47
05-04 22:30:47.754  4040  4158 D SimpleDateFormatTest: run() thread-1 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: -61943447773000, newDateText: 0007-05-06 16:43:47
05-04 22:30:47.754  4040  4165 D SimpleDateFormatTest: run() thread-8 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: -61462403713000, newDateText: 0000-05-04 22:04:47
05-04 22:30:47.754  4040  4162 D SimpleDateFormatTest: run() thread-5 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: -61462403713000, newDateText: 0020-05-04 22:04:47
05-04 22:30:47.754  4040  4174 D SimpleDateFormatTest: run() thread-14 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: 1588602167000, newDateText: 2020-05-04 22:22:47
05-04 22:30:47.754  4040  4169 D SimpleDateFormatTest: run() thread-12 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: -62156474173000, newDateText: 0030-05-04 22:22:47
05-04 22:30:47.754  4040  4175 D SimpleDateFormatTest: run() thread-15 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: 95628263447000, newDateText: 5000-05-04 22:30:47
05-04 22:30:47.754  4040  4167 D SimpleDateFormatTest: run() thread-10 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: 102002769047000, newDateText: 5202-05-04 22:30:47
05-04 22:30:47.754  4040  4161 D SimpleDateFormatTest: run() thread-4 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: 1575124247000, newDateText: 2019-11-30 22:30:47
05-04 22:30:47.755  4040  4159 D SimpleDateFormatTest: run() thread-2 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: -61462402153000, newDateText: 0022-05-04 22:30:47
05-04 22:30:47.755  4040  4187 D SimpleDateFormatTest: run() thread-19 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: 1588602647000, newDateText: 2020-05-04 22:30:47
05-04 22:30:47.757  4040  4182 D SimpleDateFormatTest: run() thread-17 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: -62169758953000, newDateText: 0002-12-04 22:30:47
05-04 22:30:47.757  4040  4183 D SimpleDateFormatTest: run() thread-18 timeMillSec: 1588602647748, dateText: 2020-05-04 22:30:47, parseTimeMillSec: -62169758953000, newDateText: 0002-12-04 22:30:47

 

加锁版本例子

@Test
public void multiThreadSyncTest() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    final long timeMillSec = Calendar.getInstance().getTimeInMillis();
    List<Thread> threadList = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        Thread t = new Thread(new SyncDateFormatTask(dateFormat, timeMillSec));
        t.setName("sync-thread-" + i);
        t.start();
        threadList.add(t);
    }

    try {
        Thread.sleep(3000L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

/**
 * SimpleDateFormat测试,加锁版本
 */
private class SyncDateFormatTask implements Runnable {
    private DateFormat dateFormat;
    private long timeMillSec = 0;

    private SyncDateFormatTask(DateFormat dateFormat, long timeMillSec) {
        this.dateFormat = dateFormat;
        this.timeMillSec = timeMillSec;
    }

    @Override
    public void run() {
        synchronized (dateFormat) {
            String dateText = dateFormat.format(new Date(timeMillSec));
            long parseTimeMillSec = 0L;
            try {
                parseTimeMillSec = dateFormat.parse(dateText).getTime();
                String newDateText = dateFormat.format(new Date(parseTimeMillSec));
                log(Thread.currentThread().getName() + " timeMillSec: " + timeMillSec
                        + ", dateText: " + dateText
                        + ", parseTimeMillSec: " + parseTimeMillSec
                        + ", newDateText: " + newDateText);
            } catch (ParseException e) {
                log(Thread.currentThread().getName() + ", parse failed. "
                        + e.getClass().getSimpleName() + " " + e.getMessage()
                        + " timeMillSec: " + timeMillSec);
            } catch (Exception e) {
                log(Thread.currentThread().getName() + ", parse failed. "
                        + e.getClass().getSimpleName() + " " + e.getMessage()
                        + " timeMillSec: " + timeMillSec);
            }
        }
    }
}

 

输出结果

05-04 22:32:52.261  4421  4483 D SimpleDateFormatTest: run() sync-thread-1 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.262  4421  4482 D SimpleDateFormatTest: run() sync-thread-0 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.263  4421  4484 D SimpleDateFormatTest: run() sync-thread-2 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.264  4421  4485 D SimpleDateFormatTest: run() sync-thread-3 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.265  4421  4486 D SimpleDateFormatTest: run() sync-thread-4 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.266  4421  4490 D SimpleDateFormatTest: run() sync-thread-7 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.267  4421  4492 D SimpleDateFormatTest: run() sync-thread-9 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.267  4421  4493 D SimpleDateFormatTest: run() sync-thread-10 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.268  4421  4488 D SimpleDateFormatTest: run() sync-thread-5 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.269  4421  4491 D SimpleDateFormatTest: run() sync-thread-8 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.270  4421  4489 D SimpleDateFormatTest: run() sync-thread-6 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.271  4421  4494 D SimpleDateFormatTest: run() sync-thread-11 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.271  4421  4496 D SimpleDateFormatTest: run() sync-thread-13 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.272  4421  4495 D SimpleDateFormatTest: run() sync-thread-12 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.273  4421  4498 D SimpleDateFormatTest: run() sync-thread-14 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.273  4421  4499 D SimpleDateFormatTest: run() sync-thread-15 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.274  4421  4501 D SimpleDateFormatTest: run() sync-thread-17 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.275  4421  4503 D SimpleDateFormatTest: run() sync-thread-19 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.275  4421  4500 D SimpleDateFormatTest: run() sync-thread-16 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52
05-04 22:32:52.276  4421  4502 D SimpleDateFormatTest: run() sync-thread-18 timeMillSec: 1588602772243, dateText: 2020-05-04 22:32:52, parseTimeMillSec: 1588602772000, newDateText: 2020-05-04 22:32:52

 

 

 

对比两个例子可以看出来,没加锁时会打印不同的日期,而加锁后就不会造成日期混乱了。

 

分析原因:主要因为SimepleDateFormat共用成员变量protected Calendar calendar;导致的。

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值