java.text.SimpleDateFormat多线程下的问题

1. 今天在做性能压测的时候发现java.text.SimpleDateFormat多线程下的错误

2. 先贴出两段错误大家看一下:

Exception in thread "pool-1-thread-42" java.lang.NumberFormatException: multiple points  
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1084)  
    at java.lang.Double.parseDouble(Double.java:510)  
    at java.text.DigitList.getDouble(DigitList.java:151)  
    at java.text.DecimalFormat.parse(DecimalFormat.java:1303)  
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1591)  
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1312)  
    at java.text.DateFormat.parse(DateFormat.java:335)  
    at infoair.obcs.utilities.DateTimeUtils.validateDateTime(DateTimeUtils.java:294)  
    at infoair.obcs.utilities.TestThread.run(DateTimeUtils.java:318)  
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
    at java.lang.Thread.run(Thread.java:619)  
Exception in thread "pool-1-thread-9" java.lang.NumberFormatException: For input string: ""  
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)  
    at java.lang.Long.parseLong(Long.java:431)  
    at java.lang.Long.parseLong(Long.java:468)  
    at java.text.DigitList.getLong(DigitList.java:177)  
    at java.text.DecimalFormat.parse(DecimalFormat.java:1298)  
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1591)  
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1312)  
    at java.text.DateFormat.parse(DateFormat.java:335)  
    at infoair.obcs.utilities.DateTimeUtils.validateDateTime(DateTimeUtils.java:294)  
    at infoair.obcs.utilities.TestThread.run(DateTimeUtils.java:318)  
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
    at java.lang.Thread.run(Thread.java:619)  
分析:自己在本地debug的时候没有任何问题,但是线上很奇怪。然后查看jdk文档发现这个类不是线程安全的。

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.
解决方案:

在调用的代码中每次new一个就可以了。

参考:http://stackoverflow.com/questions/4021151/java-dateformat-is-not-threadsafe-what-does-this-leads-to

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值