码迷,mamicode.com
首页 > 编程语言 > 详细

JDK中的SimpleDateFormat线程非安全

时间:2014-05-01 05:00:34      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:com   http   style   blog   class   div   img   code   c   java   log   

  在JDK中使用SimpleDateFormat的时候都会遇到线程安全的问题,在JDK文档中也说明了该类是线程非安全的,建议对于每个线程都创建一个SimpleDateFormat对象。如下面一个Case中,多个线程去调用SimpleDateFormat中得parse方法:

mamicode.com,码迷
@Test
    public void testUnThreadSafe() throws Exception {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,S");

        final String[] dateStrings = {
                "2014-04-30 18:51:01,61",
                "2014-04-30 18:51:01,461",
                "2014-04-30 18:51:01,361",
                "2014-04-30 18:51:01,261",
                "2014-04-30 18:51:01,161",
        };
        int threadNum = 5;
        Thread[] parseThreads = new Thread[threadNum];
        for (int i=0; i<threadNum; i++) {
           parseThreads[i] = new Thread(new Runnable() {
               public void run() {
                   for (int j=0; j<dateStrings.length; j++) {
                       try {
                           System.out.println(Thread.currentThread().getName() + " " + sdf.parse(dateStrings[j]));
                       } catch (ParseException e) {
                           e.printStackTrace();
                       }
                   }
               }
           });
           parseThreads[i].start();
        }

        for (int i=0; i<threadNum; i++) {
            parseThreads[i].join();
        }
    }
mamicode.com,码迷

将会抛出异常:java.lang.NumberFormatException: multiple points

通常的解决办法有:

1. 使用synchronized

mamicode.com,码迷
synchronized (sdf) {
  System.out.println(Thread.currentThread().getName() + " " + sdf.parse(dateStrings[j]));
}
mamicode.com,码迷

2. 每用一次实例化一次

mamicode.com,码迷
try {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,S");
    System.out.println(Thread.currentThread().getName() + " " + sdf.parse(dateStrings[j]));
} catch (ParseException e) {
    e.printStackTrace();
}
mamicode.com,码迷

3. 使用ThreadLocal

mamicode.com,码迷
    @Test
    public void testUnThreadSafe() throws Exception {
//        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,S");
        final ThreadLocal<SimpleDateFormat> localSdf = new ThreadLocal<SimpleDateFormat>();
        final String[] dateStrings = {
                "2014-04-30 18:51:01,61",
                "2014-04-30 18:51:01,461",
                "2014-04-30 18:51:01,361",
                "2014-04-30 18:51:01,261",
                "2014-04-30 18:51:01,161",
        };
        int threadNum = 5;
        Thread[] parseThreads = new Thread[threadNum];
        for (int i=0; i<threadNum; i++) {
           parseThreads[i] = new Thread(new Runnable() {
               public void run() {
                   for (int j=0; j<dateStrings.length; j++) {
                       try {
                           SimpleDateFormat sdf = localSdf.get();
                           if (sdf == null) {
                               sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,S");
                               localSdf.set(sdf);
                           }
                           System.out.println(Thread.currentThread().getName() + " " + sdf.parse(dateStrings[j]));
                       } catch (ParseException e) {
                           e.printStackTrace();
                       }
                   }
               }
           });
           parseThreads[i].start();
        }

        for (int i=0; i<threadNum; i++) {
            parseThreads[i].join();
        }
    }
mamicode.com,码迷

第一种和第二种解决方案对于一个工具类来说都会带来昂贵的资源开销,建议使用ThreadLocal创建一个对单个线程来说全局的变量,保证线程安全,当然可以使用第三方工具类如Apache commons 里的FastDateFormat或者Joda-Time类库来处理。

JDK中的SimpleDateFormat线程非安全,码迷,mamicode.com

JDK中的SimpleDateFormat线程非安全

标签:com   http   style   blog   class   div   img   code   c   java   log   

原文地址:http://www.cnblogs.com/nexiyi/p/SimpleDateFormat_ThreadSafe_Solution.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!