zoukankan      html  css  js  c++  java
  • 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 }

     

     

     

  • 相关阅读:
    [附件解决方案]CruiseControl.NET 超冷门”BUG” 关键字:VstsHistoryParser ParseChangeSet CheckForModifications FormatException
    SQL SERVER 2008 函数大全 字符串函数
    第五章 DOM接口 DOM Interfaces
    [MSSQL]FOR XML AUTO I
    DOS CHOICE命令
    [MSSQL]NTILE另类分页有么有?!
    原生态webglDEMO,以后可能选择Three.JS来代替了,代码网上找的,参考引用
    [MSSQL]COALESCE与ISNULL函数
    【转】“无法在Web服务器上启动调试。您不具备调试此应用程序的权限,此项目的URL位于Internet区域”错误提示的解决
    【转】一个项目涉及到的50个Sql语句(整理版)
  • 原文地址:https://www.cnblogs.com/alphaqiu/p/2643257.html
Copyright © 2011-2022 走看看