`

SimpleDateFormat线程不安全及解决办法

    博客分类:
  • java
 
阅读更多

SimpleDateFormatt线程不安全及解决办法

 

一. 为什么SimpleDateFormat不是线程安全的?

Java源码如下:

 

  1.  
    /**
  2.  
    * Date formats are not synchronized.
  3.  
    * It is recommended to create separate format instances for each thread.
  4.  
    * If multiple threads access a format concurrently, it must be synchronized
  5.  
    * externally.
  6.  
    */
  7.  
    public class SimpleDateFormat extends DateFormat {
  8.  
     
  9.  
    public Date parse(String text, ParsePosition pos){
  10.  
    calendar.clear(); // Clears all the time fields
  11.  
    // other logic ...
  12.  
    Date parsedDate = calendar.getTime();
  13.  
    }
  14.  
    }
  15.  
     
  16.  
     
  17.  
    abstract class DateFormat{
  18.  
    // other logic ...
  19.  
    protected Calendar calendar;
  20.  
    public Date parse(String source) throws ParseException{
  21.  
    ParsePosition pos = new ParsePosition(0);
  22.  
    Date result = parse(source, pos);
  23.  
    if (pos.index == 0)
  24.  
    throw new ParseException("Unparseable date: \"" + source + "\"" ,
  25.  
    pos.errorIndex);
  26.  
    return result;
  27.  
    }
  28.  
    }

如果我们把SimpleDateFormat定义成static成员变量,那么多个thread之间会共享这个sdf对象, 所以Calendar对象也会共享。

 

 

假定线程A和线程B都进入了parse(text, pos) 方法, 线程B执行到calendar.clear()后,线程A执行到calendar.getTime(), 那么就会有问题。

 

二. 解决方案

1.方法内部创建SimpleDateFormat,不管是什么时候,将有线程安全的对象由共享变为私有局部变量都可以避免多线程问题,不过也加重了创建对象的负担,虽然随时创建SimpleDateFormat会造成一定的性能影响,而且会对GC产生一定的压力,但这并不是核心问题,只要能产生正确的结果:
 
  1.  
    public static String format(Date date) {
  2.  
    if (date == null) {
  3.  
    return "";
  4.  
    }
  5.  
    return new SimpleDateFormat(YMD_HYPHEN_PATTERN).format(date);
  6.  
    }
 
 
 
2.将SimpleDateFormat进行同步使用,在每次执行时都对其加锁,这样也会影响性能,想要调用此方法的线程就需要block,当多线程并发量比较大时会对性能产生一定影响;
 
  1.  
    public static String formatDate(Date date)throws ParseException{
  2.  
    synchronized(sdf){
  3.  
    return sdf.format(date);
  4.  
    }
  5.  
    }
  
在任何公共的地方使用该类时,都需要对SimpleDateFormat进行加锁。
 
3.使用ThreadLocal变量,用空间换时间,这样每个线程就会独立享有一个本地的SimpleDateFormat变量;
 
 
  1.  
    private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() {
  2.  
    @Override
  3.  
    protected DateFormat initialValue() {
  4.  
    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  5.  
    }
  6.  
    };
  7.  
     
  8.  
    public static Date parse(String dateStr) throws ParseException {
  9.  
    return threadLocal.get().parse(dateStr);
  10.  
    }
或者
 
 写一个工具类:

 

  1.  
    public class DateUtil {
  2.  
    private static ThreadLocal<SimpleDateFormat> local = new ThreadLocal<SimpleDateFormat>();
  3.  
     
  4.  
    public static Date parse(String str) throws Exception {
  5.  
    SimpleDateFormat sdf = local.get();
  6.  
    if (sdf == null) {
  7.  
    sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
  8.  
    local.set(sdf);
  9.  
    }
  10.  
    return sdf.parse(str);
  11.  
    }
  12.  
     
  13.  
    public static String format(Date date) throws Exception {
  14.  
    SimpleDateFormat sdf = local.get();
  15.  
    if (sdf == null) {
  16.  
    sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
  17.  
    local.set(sdf);
  18.  
    }
  19.  
    return sdf.format(date);
  20.  
    }
  21.  
    }
  1.  
这样就可以保证每个线程的本地变量都是安全的,不同线程之间并不共享相同的SimpleDateFormat,从而避免了线程安全问题。
 

 

如果需要对性能比较敏感,可以采用这种方式,至少比前两种的速度要快,但是占用内存也会大一点(但也不会多么夸张)。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics