解决SimpleDateFormat的线程不安全问题的方法:ThreadLocal

问题 

我们写了个DateUtil,内部有个SimpleDateFormat,是个static,我们想全局公用此常量

public class DateUtil {
 
    private static final String MESSAGE_FORMAT = "MM-dd HH:mm:ss.ms";
    private static final SimpleDateFormat format=new SimpleDateFormat(MESSAGE_FORMAT, Locale.getDefault());
 
    public static final DateFormat getDateFormat() {
        return format;
    }
}

如果要在多线程的环境下使用DateUtil,是会出问题的,SimpleDateFormat的parse方法是存在线程安全问题的,如果直接这么使用,很可能会在SimpleDateFormat的parse方法内部崩溃

方法0:同步

把parse也封到工具类内,且对parse加同步锁

public class DateUtil0 {
 
    private static final String MESSAGE_FORMAT = "MM-dd HH:mm:ss.ms";
    private static final SimpleDateFormat format=new SimpleDateFormat(MESSAGE_FORMAT, Locale.getDefault());
 
    public static final DateFormat getDateFormat() {
        return format;
    }
 
    public  Date parse(String str) {
        try {
            synchronized(format){
                return  format.parse(str);
            }
        } catch (ParseException e) {
        }
        return  null;
    }
}

这样没问题,但是加了锁,效率严重下降。

方法1:使用ThreadLocal的泛型方法,推荐此方法

public class DateUtil1 {
 
    private static final ThreadLocal<DateFormat> messageFormat = new ThreadLocal<DateFormat>();
    private static final String MESSAGE_FORMAT = "MM-dd HH:mm:ss.ms";
 
    private static final DateFormat getDateFormat() {
        DateFormat format = messageFormat.get();
        if (format == null) {
            format = new SimpleDateFormat(MESSAGE_FORMAT, Locale.getDefault());
            messageFormat.set(format);
        }
 
        return format;
    }
}

hreadLocal是专门为解决此类问题而存在的,当我们不想每次使用都new一个东西,就会使用static,一次创建,到处都能用,但是使用的时候往往又有线程安全问题。其实只要不同线程里有一个不同的实例,就解决了这个问题。对于上例,每个线程创建一个SimpleDateFormat,互相独立。线程内公用同一个,不同线程之间相互隔离.SimpleDateFormat。ThreadLocal就是根据上述思想被创建出来的。
核心思想在getDateFormat内部,首先去get,如果当前线程已有对应实例,就直接返回,如果没有,就创建一个并返回。

方法2:ThreadLocal的匿名内部类写法

public class DateUtil2 {
 
 
    private static final String MESSAGE_FORMAT = "MM-dd HH:mm:ss.ms";
 
    private static final ThreadLocal messageFormat = new ThreadLocal(){
        protected synchronized Object initialValue() {
            return  new SimpleDateFormat(MESSAGE_FORMAT, Locale.getDefault());
        }
    };
 
 
    private static final DateFormat getDateFormat() {
      return (DateFormat) messageFormat.get();
    }
}

getDateFormat会去调get方法,get方法内部检查当前线程是否有实例存在,有就直接返回,无就调用initialValue来new一个,注意这里initialValue要加同步锁

方法3 匿名内部类返回null的写法

1,2的杂交,将无实例就new写在getDateFormat里

public class DateUtil3 {
 
 
    private static final String MESSAGE_FORMAT = "MM-dd HH:mm:ss.ms";
 
    private static final ThreadLocal messageFormat = new ThreadLocal(){
        protected synchronized Object initialValue() {
            return  null;
        }
    };
 
 
    private static final DateFormat getDateFormat() {
        
        DateFormat df = (DateFormat) messageFormat.get();
        if (df == null) {
            df = new SimpleDateFormat(MESSAGE_FORMAT, Locale.getDefault());
            messageFormat.set(df);
        }
        return df;
    }
}

方法4,3的简化

public class DateUtil4 {
 
 
    private static final String MESSAGE_FORMAT = "MM-dd HH:mm:ss.ms";
 
    private static final ThreadLocal messageFormat = new ThreadLocal();
 
 
    private static final DateFormat getDateFormat() {
 
        DateFormat df = (DateFormat) messageFormat.get();
        if (df == null) {
            df = new SimpleDateFormat(MESSAGE_FORMAT, Locale.getDefault());
            messageFormat.set(df);
        }
        return df;
    }
}

4和1比较,其实差别就在于一个使用泛型,一个使用强转,最优方案还是1

猜你喜欢

转载自blog.csdn.net/u010506876/article/details/90145061