本文借由并發(fā)環(huán)境下使用線程不安全的SimpleDateFormat優(yōu)化案例,幫助大家理解ThreadLocal.
最近整理公司項(xiàng)目,發(fā)現(xiàn)不少寫的比較糟糕的地方,比如下面這個(gè):
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(); } }}
首先分析下:
該處的函數(shù)parseymdhms()使用了synchronized修飾,意味著該操作是線程不安全的,所以需要同步,線程不安全也只能是SimpleDateFormat的parse()方法,查看下源碼,在SimpleDateFormat里面有一個(gè)全局變量
protected Calendar calendar;Date parse() { calendar.clear(); ... // 執(zhí)行一些操作, 設(shè)置 calendar 的日期什么的 calendar.getTime(); // 獲取calendar的時(shí)間}
該clear()操作會(huì)造成線程不安全.
此外使用synchronized 關(guān)鍵字對(duì)性能有很大影響,尤其是多線程的時(shí)候,每一次調(diào)用parseymdhms方法都會(huì)進(jìn)行同步判斷,并且同步本身開銷就很大,因此這是不合理的解決方案.
改進(jìn)方法
線程不安全是源于多線程使用了共享變量造成,所以這里使用ThreadLocal<SimpleDateFormat>來(lái)給每個(gè)線程單獨(dú)創(chuàng)建副本變量,先給出代碼,再分析這樣的解決問題的原因.
/** * 日期工具類(使用了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"; /** * 根據(jù)map中的key得到對(duì)應(yīng)線程的sdf實(shí)例 * @param pattern map中的key * @return 該實(shí)例 */ private static SimpleDateFormat getSdf(final String pattern){ ThreadLocal<SimpleDateFormat> sdfThread = sdfMap.get(pattern); if (sdfThread == null){ //雙重檢驗(yàn),防止sdfMap被多次put進(jìn)去值,和雙重鎖單例原因是一樣的 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實(shí)例 */ 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); } }}
測(cè)試
在主線程中執(zhí)行一個(gè),另外兩個(gè)在子線程執(zhí)行,使用的都是同一個(gè)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 mapthread: Thread[main,5,main] init pattern: MMddHHmmssSSSthread: Thread[Thread-0,5,main] init pattern: MMddHHmmssSSSthread: Thread[Thread-1,5,main] init pattern: MMddHHmmssSSS
分析
可以看出來(lái)sdfMap put進(jìn)去了一次,而SimpleDateFormat被new了三次,因?yàn)榇a中有三個(gè)線程.那么這是為什么呢?
對(duì)于每一個(gè)線程Thread,其內(nèi)部有一個(gè)ThreadLocal.ThreadLocalMap threadLocals的全局變量引用,ThreadLocal.ThreadLocalMap里面有一個(gè)保存該ThreadLocal和對(duì)應(yīng)value,一圖勝千言,結(jié)構(gòu)圖如下:
那么對(duì)于sdfMap的話,結(jié)構(gòu)圖就變更了下
1.首先第一次執(zhí)行DateUtil.formatDate(new Date(),MDHMSS);
//第一次執(zhí)行DateUtil.formatDate(new Date(),MDHMSS)分析 private static SimpleDateFormat getSdf(final String pattern){ ThreadLocal<SimpleDateFormat> sdfThread = sdfMap.get(pattern); //得到的sdfThread為null,進(jìn)入if語(yǔ)句 if (sdfThread == null){ synchronized (DateUtil.class){ sdfThread = sdfMap.get(pattern); //sdfThread仍然為null,進(jìn)入if語(yǔ)句 if (sdfThread == null){ //打印日志 logger.debug("put new sdf of pattern " + pattern + " to map"); //創(chuàng)建ThreadLocal實(shí)例,并覆蓋initialValue方法 sdfThread = new ThreadLocal<SimpleDateFormat>(){ @Override protected SimpleDateFormat initialValue() { logger.debug("thread: " + Thread.currentThread() + " init pattern: " + pattern); return new SimpleDateFormat(pattern); } }; //設(shè)置進(jìn)如sdfMap sdfMap.put(pattern,sdfThread); } } } return sdfThread.get(); }
這個(gè)時(shí)候可能有人會(huì)問,這里并沒有調(diào)用ThreadLocal的set方法,那么值是怎么設(shè)置進(jìn)入的呢?
這就需要看sdfThread.get()的實(shí)現(xiàn):
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(); }
也就是說當(dāng)值不存在的時(shí)候會(huì)調(diào)用setInitialValue()方法,該方法會(huì)調(diào)用initialValue()方法,也就是我們覆蓋的方法.
對(duì)應(yīng)日志打印.
put new sdf of pattern MMddHHmmssSSS to mapthread: Thread[main,5,main] init pattern: MMddHHmmssSSS
2.第二次在子線程執(zhí)行DateUtil.formatDate(new Date(),MDHMSS);
//第二次在子線程執(zhí)行`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); } } } //直接調(diào)用sdfThread.get()返回 return sdfThread.get(); }
分析sdfThread.get()
//第二次在子線程執(zhí)行`DateUtil.formatDate(new Date(),MDHMSS);` public T get() { Thread t = Thread.currentThread();//得到當(dāng)前子線程 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; } } //直接執(zhí)行初始化,也就是調(diào)用我們覆蓋的initialValue()方法 return setInitialValue(); }
對(duì)應(yīng)日志:
Thread[Thread-1,5,main] init pattern: MMddHHmmssSSS
總結(jié)
在什么場(chǎng)景下比較適合使用ThreadLocal?stackoverflow上有人給出了還不錯(cuò)的回答。
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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選