SimpleDateFormat多线程问题

http://blog.csdn.net/peterwanghao/article/details/1693682

SimpleDateFormat可以将时间格式在java.util.Date string之间进行相互转换。使用parseformat方法。在程序运行中却抛出java.lang.NumberFormatException异常。

java.lang.NumberFormatException: For input string: ""

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Long.parseLong(Long.java:415)

at java.lang.Long.parseLong(Long.java:452)

at java.text.DigitList.getLong(DigitList.java:149)

at java.text.DecimalFormat.parse(DecimalFormat.java:1068)

at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1388)

at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1156)

at java.text.DateFormat.parse(DateFormat.java:333)

检查了一下输入的字串内容并没有错误。后来发现问题的根本原因是将SimpleDateFormat定义为静态属性了,多线程调用。查看了资料知道时间格式不是同步的,应当为每个线程单独创建一个实例,如果需要多线程并发地访问一个SimpleDateFormat,必须要线程同步。

----

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

Here is a program in which multiple threads use a shared SimpleDateFormat.

 

 1 public static void main(String[] args) throws Exception {
 2 
 3     final DateFormat format =
 4         new SimpleDateFormat("yyyyMMdd");
 5 
 6     Callable<Date> task = new Callable<Date>(){
 7         public Date call() throws Exception {
 8             return format.parse("20101022");
 9         }
10     };
11 
12     //pool with 5 threads
13     ExecutorService exec = Executors.newFixedThreadPool(5);
14     List<Future<Date>> results =
15                  new ArrayList<Future<Date>>();
16 
17     //perform 10 date conversions
18     for(int i = 0 ; i < 10 ; i++){
19         results.add(exec.submit(task));
20     }
21     exec.shutdown();
22 
23     //look at the results
24     for(Future<Date> result : results){
25         System.out.println(result.get());
26     }
27 }

Run this a few times and you will see:

Exceptions:

Here are a few examples:

1 Caused by: java.lang.NumberFormatException: For input string: ""
2     at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
3     at java.lang.Long.parseLong(Long.java:431)
4     at java.lang.Long.parseLong(Long.java:468)
5     at java.text.DigitList.getLong(DigitList.java:177)
6     at java.text.DecimalFormat.parse(DecimalFormat.java:1298)
7     at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1589)
1 Caused by: java.lang.NumberFormatException: For input string: ".10201E.102014E4"
2     at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
3     at java.lang.Double.parseDouble(Double.java:510)
4     at java.text.DigitList.getDouble(DigitList.java:151)
5     at java.text.DecimalFormat.parse(DecimalFormat.java:1303)
6     at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1589)
1 Caused by: java.lang.NumberFormatException: multiple points
2     at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1084)
3     at java.lang.Double.parseDouble(Double.java:510)
4     at java.text.DigitList.getDouble(DigitList.java:151)
5     at java.text.DecimalFormat.parse(DecimalFormat.java:1303)
6     at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1936)
7     at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1312)

Incorrect Results:

Sat Oct 22 00:00:00 BST 2011
Thu Jan 22 00:00:00 GMT 1970
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Thu Oct 22 00:00:00 GMT 1970
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010

Correct Results:

Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010

Another approach to safely use DateFormats in a multi-threaded environment is to use a ThreadLocal variable to hold the DateFormat object, which means that each thread will have its own copy and doesn't need to wait for other threads to release it. This is how:

 1 public class DateFormatTest {
 2 
 3   private static final ThreadLocal<DateFormat> df
 4                  = new ThreadLocal<DateFormat>(){
 5     @Override
 6     protected DateFormat initialValue() {
 7         return new SimpleDateFormat("yyyyMMdd");
 8     }
 9   };
10 
11   public Date convert(String source)
12                      throws ParseException{
13     Date d = df.get().parse(source);
14     return d;
15   }
16 }

 

 

 

 

posted @ 2012-08-16 23:28  疯长的胡茬  阅读(1325)  评论(0编辑  收藏  举报