一次 SimpleDateFormat 引发的惨案(2)
- UID
- 1066743
|
一次 SimpleDateFormat 引发的惨案(2)
难道是别人也同时调用了该服务该方法?那为何在我这边的服务器日志上打印出来了?不可能。
还是找找自身的问题吧,从我开始调用一步一来分析。。。咦?调用的时候,为了性能,我写了一行很简练的代码:
ids.parallelStream().forEach(id -> invokeMethod(id));
复制代码
哦,并行处理?-> 并发?-> 线程安全?-> parse?-> SimpleDateFormat类?
是不是找到点线索?如果要进一步真正找到“嫌疑人”,那就还原一下现场嘛。。
package com.jessehuang.dateformat;
import java.text.ParseException;
import java.util.Date;
public class DateUtilTest {
public static class TestSimpleDateFormatThreadSafe extends Thread {
@Override
public void run() {
while(true) {
try {
this.join(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
try {
System.out.println(this.getName() + ":" + DateUtil.parse("2019-01-10 00:00:00"));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
for(int i = 0; i < 3; i++){
new TestSimpleDateFormatThreadSafe().start();
}
}
}
复制代码
输出结果:
Exception in thread "Thread-1" Exception in thread "Thread-0" java.lang.NumberFormatException: multiple points
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890)
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.jessehuang.SimpleDateFormatTest.parse(SimpleDateFormatTest.java:21)
at com.jessehuang.SimpleDateFormatTest$TestSimpleDateFormatThreadSafe.run(SimpleDateFormatTest.java:34)
java.lang.NumberFormatException: multiple points
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890)
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.jessehuang.SimpleDateFormatTest.parse(SimpleDateFormatTest.java:21)
at com.jessehuang.SimpleDateFormatTest$TestSimpleDateFormatThreadSafe.run(SimpleDateFormatTest.java:34)
Thread-2:Sat Jan 10 00:00:00 CST 2201
Thread-2:Thu Jan 10 00:00:00 CST 2019
Thread-2:Thu Jan 10 00:00:00 CST 2019
Thread-2:Thu Jan 10 00:00:00 CST 2019
复制代码
看到了吗?2201这种年份出现了。Thread-1和Thread-0报java.lang.NumberFormatException: multiple points错误,直接挂死,没起来;Thread-2 虽然没有挂死,但输出的时间是有错误的,比如我们输入的时间是:2019-01-10 00:00:00 ,但会输出:Sat Jan 10 00:00:00 CST 2201 这样的令人“穿越”的日期。
是的,破案了,凶手就是你 —— SimpleDateFormat |
|
|
|
|
|