java學習記錄--ThreadLocal使用案例


本文借由並發環境下使用線程不安全的SimpleDateFormat優化案例,幫助大家理解ThreadLocal.

最近整理公司項目,發現不少寫的比較糟糕的地方,比如下面這個:

public class DateUtil {

    private final static SimpleDateFormat sdfyhm = new SimpleDateFormat(
            "yyyyMMdd");
            
    public synchronized static Date parseymdhms(String source) {
        try {
            return sdfyhm.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }

}

首先分析下:
該處的函數parseymdhms()使用了synchronized修飾,意味着該操作是線程不安全的,所以需要同步,線程不安全也只能是SimpleDateFormat的parse()方法,查看下源碼,在SimpleDateFormat里面有一個全局變量

protected Calendar calendar;

Date parse() {

    calendar.clear();

  ... // 執行一些操作, 設置 calendar 的日期什么的

  calendar.getTime(); // 獲取calendar的時間

}

該clear()操作會造成線程不安全.

此外使用synchronized 關鍵字對性能有很大影響,尤其是多線程的時候,每一次調用parseymdhms方法都會進行同步判斷,並且同步本身開銷就很大,因此這是不合理的解決方案.


改進方法

線程不安全是源於多線程使用了共享變量造成,所以這里使用ThreadLocal<SimpleDateFormat>來給每個線程單獨創建副本變量,先給出代碼,再分析這樣的解決問題的原因.


/**
 * 日期工具類(使用了ThreadLocal獲取SimpleDateFormat,其他方法可以直接拷貝common-lang)
 * @author Niu Li
 * @date 2016/11/19
 */
public class DateUtil {

    private static Map<String,ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();

    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    public final static String MDHMSS = "MMddHHmmssSSS";
    public final static String YMDHMS = "yyyyMMddHHmmss";
    public final static String YMDHMS_ = "yyyy-MM-dd HH:mm:ss";
    public final static String YMD = "yyyyMMdd";
    public final static String YMD_ = "yyyy-MM-dd";
    public final static String HMS = "HHmmss";

    /**
     * 根據map中的key得到對應線程的sdf實例
     * @param pattern map中的key
     * @return 該實例
     */
    private static SimpleDateFormat getSdf(final String pattern){
        ThreadLocal<SimpleDateFormat> sdfThread = sdfMap.get(pattern);
        if (sdfThread == null){
            //雙重檢驗,防止sdfMap被多次put進去值,和雙重鎖單例原因是一樣的
            synchronized (DateUtil.class){
                sdfThread = sdfMap.get(pattern);
                if (sdfThread == null){
                    logger.debug("put new sdf of pattern " + pattern + " to map");
                    sdfThread = new ThreadLocal<SimpleDateFormat>(){
                        @Override
                        protected SimpleDateFormat initialValue() {
                            logger.debug("thread: " + Thread.currentThread() + " init pattern: " + pattern);
                            return new SimpleDateFormat(pattern);
                        }
                    };
                    sdfMap.put(pattern,sdfThread);
                }
            }
        }
        return sdfThread.get();
    }

    /**
     * 按照指定pattern解析日期
     * @param date 要解析的date
     * @param pattern 指定格式
     * @return 解析后date實例
     */
    public static Date parseDate(String date,String pattern){
        if(date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        try {
            return  getSdf(pattern).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            logger.error("解析的格式不支持:"+pattern);
        }
        return null;
    }
    /**
     * 按照指定pattern格式化日期
     * @param date 要格式化的date
     * @param pattern 指定格式
     * @return 解析后格式
     */
    public static String formatDate(Date date,String pattern){
        if (date == null){
            throw new IllegalArgumentException("The date must not be null");
        }else {
            return getSdf(pattern).format(date);
        }
    }
}

測試

在主線程中執行一個,另外兩個在子線程執行,使用的都是同一個pattern

public static void main(String[] args) {
        DateUtil.formatDate(new Date(),MDHMSS);
        new Thread(()->{
            DateUtil.formatDate(new Date(),MDHMSS);
        }).start();
        new Thread(()->{
            DateUtil.formatDate(new Date(),MDHMSS);
        }).start();
    }

日志分析

put new sdf of pattern MMddHHmmssSSS to map
thread: Thread[main,5,main] init pattern: MMddHHmmssSSS
thread: Thread[Thread-0,5,main] init pattern: MMddHHmmssSSS
thread: Thread[Thread-1,5,main] init pattern: MMddHHmmssSSS

分析

可以看出來sdfMap put進去了一次,而SimpleDateFormat被new了三次,因為代碼中有三個線程.那么這是為什么呢?

對於每一個線程Thread,其內部有一個ThreadLocal.ThreadLocalMap threadLocals的全局變量引用,ThreadLocal.ThreadLocalMap里面有一個保存該ThreadLocal和對應value,一圖勝千言,結構圖如下:

那么對於sdfMap的話,結構圖就變更了下

那么日志為什么是這樣的?分析下:

1.首先第一次執行DateUtil.formatDate(new Date(),MDHMSS);

//第一次執行DateUtil.formatDate(new Date(),MDHMSS)分析
    private static SimpleDateFormat getSdf(final String pattern){
        ThreadLocal<SimpleDateFormat> sdfThread = sdfMap.get(pattern);
        //得到的sdfThread為null,進入if語句
        if (sdfThread == null){
            synchronized (DateUtil.class){
                sdfThread = sdfMap.get(pattern);
                //sdfThread仍然為null,進入if語句
                if (sdfThread == null){
                    //打印日志
                    logger.debug("put new sdf of pattern " + pattern + " to map");
                    //創建ThreadLocal實例,並覆蓋initialValue方法
                    sdfThread = new ThreadLocal<SimpleDateFormat>(){
                        @Override
                        protected SimpleDateFormat initialValue() {
                            logger.debug("thread: " + Thread.currentThread() + " init pattern: " + pattern);
                            return new SimpleDateFormat(pattern);
                        }
                    };
                    //設置進如sdfMap
                    sdfMap.put(pattern,sdfThread);
                }
            }
        }
        return sdfThread.get();
    }

這個時候可能有人會問,這里並沒有調用ThreadLocal的set方法,那么值是怎么設置進入的呢?
這就需要看sdfThread.get()的實現:

public T get() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null) {
                @SuppressWarnings("unchecked")
                T result = (T)e.value;
                return result;
            }
        }
        return setInitialValue();
    }

也就是說當值不存在的時候會調用setInitialValue()方法,該方法會調用initialValue()方法,也就是我們覆蓋的方法.

對應日志打印.

put new sdf of pattern MMddHHmmssSSS to map
thread: Thread[main,5,main] init pattern: MMddHHmmssSSS

2.第二次在子線程執行DateUtil.formatDate(new Date(),MDHMSS);

//第二次在子線程執行`DateUtil.formatDate(new Date(),MDHMSS);`
    private static SimpleDateFormat getSdf(final String pattern){
        ThreadLocal<SimpleDateFormat> sdfThread = sdfMap.get(pattern);
        //這里得到的sdfThread不為null,跳過if塊
        if (sdfThread == null){
            synchronized (DateUtil.class){
                sdfThread = sdfMap.get(pattern);
                if (sdfThread == null){
                    logger.debug("put new sdf of pattern " + pattern + " to map");
                    sdfThread = new ThreadLocal<SimpleDateFormat>(){
                        @Override
                        protected SimpleDateFormat initialValue() {
                            logger.debug("thread: " + Thread.currentThread() + " init pattern: " + pattern);
                            return new SimpleDateFormat(pattern);
                        }
                    };
                    sdfMap.put(pattern,sdfThread);
                }
            }
        }
        //直接調用sdfThread.get()返回
        return sdfThread.get();
    }

分析sdfThread.get()

//第二次在子線程執行`DateUtil.formatDate(new Date(),MDHMSS);`
    public T get() {
        Thread t = Thread.currentThread();//得到當前子線程
        ThreadLocalMap map = getMap(t);
        //子線程中得到的map為null,跳過if塊
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null) {
                @SuppressWarnings("unchecked")
                T result = (T)e.value;
                return result;
            }
        }
        //直接執行初始化,也就是調用我們覆蓋的initialValue()方法
        return setInitialValue();
    }

對應日志:

Thread[Thread-1,5,main] init pattern: MMddHHmmssSSS

總結

在什么場景下比較適合使用ThreadLocal?stackoverflow上有人給出了還不錯的回答。
When and how should I use a ThreadLocal variable?
One possible (and common) use is when you have some object that is not thread-safe, but you want to avoid synchronizing access to that object (I’m looking at you, SimpleDateFormat). Instead, give each thread its own instance of the object.

參考代碼:

https://github.com/nl101531/JavaWEB 下Util-Demo

 

參考資料:

http://www.importnew.com/21479.html
http://www.cnblogs.com/zemliu/archive/2013/08/29/3290585.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM