还在用SimpleDateFormat格式化时间?小心经理锤你

还在用SimpleDateFormat格式化时间?小心经理锤你

SimpleDateFormat

场景

本来开开心心的周末时光,线上突然就疯狂报错,以为程序炸了,截停日志,发现是就是类似下述一段错误

java.lang.NumberFormatException: For input string: ".202006E.202006E44"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
	at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
	at java.lang.Double.parseDouble(Double.java:538)
	at java.text.DigitList.getDouble(DigitList.java:169)
	at java.text.DecimalFormat.parse(DecimalFormat.java:2089)
	at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)
	at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
	at java.text.DateFormat.parse(DateFormat.java:364)
	at com.github.springtools.SimpleDateFormatTest.lambda$null$0(Xxxxxxx.java:2020)
	at java.lang.Thread.run(Thread.java:748)

定位到错误处,发现是一个时间格式化(SimpleDateFormat)的异常,一个时间格式化怎么会导致这种错误,还使得接口不能正常调用

测试

拉出来,使用模拟接口多线程的环境,单独进行测试.....

package com.github.springtools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.stream.IntStream;

public class SimpleDateFormatTest {
    public static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) throws ParseException {
        System.out.println(FORMAT.parse("2020-06-06"));
        System.out.println("--------单个调用结束--------");
        System.out.println("--------多线程调用开始--------");
        
        IntStream.rangeClosed(0, 10)
                .forEach(i -> new Thread(() -> {
                    try {
                        System.out.println(FORMAT.parse("2020-06-06"));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }).start());
    }
}

输出

Sat Jun 06 00:00:00 CST 2020
--------单个调用结束--------
--------多线程调用开始--------
Sat Jun 06 00:00:00 CST 2020
Sat Jun 06 00:00:00 CST 2020
Exception in thread "Thread-7" Exception in thread "Thread-8" java.lang.NumberFormatException: For input string: ".202006E.202006E44"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
	at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
	at java.lang.Double.parseDouble(Double.java:538)

罪魁祸手浮出水面,就是SimpleDateFormat的锅
有趣
线程不安全,去找Java文档里的SimpleDateFormat: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

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.日期格式不同步。建议为每个线程创建单独的格式实例。如果多个线程同时访问一种格式,则必须在外部进行同步。

解决方法

使用ThreadLocal

// ThreadLocal
public static final ThreadLocal<SimpleDateFormat> THREADLOCAL_FORMAT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
IntStream.rangeClosed(0, 5)
        .forEach(i -> new Thread(() -> {
            try {
                System.out.println("ThreadLocal:" + THREADLOCAL_FORMAT.get().parse("2020-06-06"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }).start());

使用Java 8中的时间处理

public static final DateTimeFormatter JAVA8_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
IntStream.rangeClosed(0, 5)
        .forEach(i -> new Thread(() -> {
            try {
                System.out.println("JAVA8_FORMATTER:" + JAVA8_FORMATTER.parse("2020-06-06"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }).start());

总结

使用多线程的时候,一定要考虑到其调用到的实例变量,Java8中时间格式化DateTimeFormatter是用final修饰的,不可变类,所以是线程安全的,或者在线程中调用ThreadLocal也是可以的

posted @ 2020-06-06 23:00  JavaPro  阅读(403)  评论(0编辑  收藏  举报
豫ICP备19010929号