浏览 2316 次
锁定老帖子 主题:偶遇java时间解析异常
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-01-20
最后修改:2011-01-20
先贴两个异常,不知大家见过没
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又没有事情,于是自己写了个demo
class TestThread extends Thread{ String str = "1251-12-12 03:22:22"; public void run(){ while(true){ Date date = Test.validateDateTime(str); System.out.println(date); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); break; } } } } class Test{ public final static SimpleDateFormat dateTimeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); /** * * @param dateStr * String 格式为"yyyy-MM-dd HH:mm:ss" * @return 日期 */ public static Date validateDateTime(String dateStr) { try { return dateStr == null || dateStr.equals("") ? null : dateTimeFormat.parse(dateStr); } catch (ParseException e) { // e.printStackTrace(); return null; } } public static void main(String[] args) throws InterruptedException { ExecutorService exe = Executors.newCachedThreadPool(); for (int i = 0; i < 100;i ++){ exe.execute(new TestThread()); } Thread.sleep(5000); exe.shutdownNow(); } }
测试上面的demo 就发现在多线程下九抛出了上面连个异常,查了下API,上面写着:
类 SimpleDateFormat <!-- Generated by javadoc (build 1.6.0-beta2) on Fri Mar 09 12:51:19 CST 2007 --> <noscript></noscript> 同步日期格式是不同步的。建议为每个线程创建独立的格式实例。如果多个线程同时访问一个格式,则它必须是外部同步的。
谨记,谨记。。。
有兴趣的 ,来解释下那个异常是怎么发生的吧 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-01-20
http://www.iteye.com/topic/882541
刚讨论过的话题 |
|
返回顶楼 | |
发表时间:2011-01-20
线程安全问题啊,SimpleDateFormat要使用原型对象。
|
|
返回顶楼 | |