zoukankan      html  css  js  c++  java
  • 还在用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也是可以的

  • 相关阅读:
    NHibernate开源框架Cuyahoga学习之数据访问实现
    Petshop4.0 缓存设计学习
    Jquery实现ToolTip之元素定位
    客户单操作Cookie
    .Net 通过MySQLDriverCS操作MySQL
    NHibernate的基本使用
    提高ASP.NET性能的十大方法
    工作流引擎下基于表达式Tree复杂验证的实现
    C#注册表操作
    WinForm应用程序中的ComboBox实现ReadOnly功能
  • 原文地址:https://www.cnblogs.com/cvandy/p/13057525.html
Copyright © 2011-2022 走看看