前提:
在维护老项目时发现Log时不时出现以下错误
java.lang.NumberFormatException: For input string: ".22E.22E11"
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)
java.lang.NumberFormatException: For input string: ""
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)
解决思路:
一开始奔着前后台没有做日期字段校验的思路去想的(因为这个Log是时不时出现),但验证之后发现并不是这个问题,而是发现后台代码居然将simpleDataFormat放到了全局变量里!!!!众所周知,simpleDataFormat是线程不安全的。
模拟当时代码并查看运行的情况,确实发现了该问题。
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SimpleDateFormatTest {
//SimpleDateFormat对象
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 30; i++) {
executorService.execute(() -> {
try {
try {
simpleDateFormat.parse("2022-02-02");
simpleDateFormat.parse("2022-02-02");
simpleDateFormat.parse("2022-02-02");
System.out.println("线程:" + Thread.currentThread().getName() + " 格式化日期成功!!!");
} catch (NumberFormatException e) {
System.out.println("线程:" + Thread.currentThread().getName() + " 格式化日期失败;NumberFormatException");
e.printStackTrace();
System.exit(1);
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
executorService.shutdown();
}
}
解决方案:
目前有多种方案解决
1.使用threadLocal解决
ThreadLocal原理是在每个线程中都创建了一个副本,每个线程可以访问自己内部的副本变量.确保每个线程都有自己独有一份simpleDateFormat
2.不在使用共享对象,每个调用点都创建对象
实际项目中很多地方也是直接在局部方法中选择 new SimpleDateFormat(),这个也是可以的。不过会导致代码的冗余
3.使用线程安全类DateTimeFormatter替代
api的调用方式以及入参无法直接适配,例如(sdf.format(new Date) => dtf.format(new TemporalAccessor())),涉及代码的修改远比前两个方案要多很多
4.使用synchronize
线程阻塞会影响效率故不推荐
这里拿ThreadLocal举例
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SimpleDateFormatTest {
private static final ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 30; i++) {
executorService.execute(() -> {
try {
try {
threadLocal.get().parse("2022-02-02");
threadLocal.get().parse("2022-02-02");
threadLocal.get().parse("2022-02-02");
System.out.println("线程:" + Thread.currentThread().getName() + " 格式化日期成功!!!");
} catch (NumberFormatException e) {
System.out.println("线程:" + Thread.currentThread().getName() + " 格式化日期失败;NumberFormatException");
e.printStackTrace();
System.exit(1);
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
executorService.shutdown();
}
}
多次运行正常,问题成功解决!