本文借由并發環境下使用線程不安全的SimpleDateFormat優化案例,幫助大家理解ThreadLocal.
最近整理公司項目,發現不少寫的比較糟糕的地方,比如下面這個:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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里面有一個全局變量
1
2
3
4
5
6
7
8
9
10
11
|
protected Calendar calendar; Date parse() { calendar.clear(); ... // 執行一些操作, 設置 calendar 的日期什么的 calendar.getTime(); // 獲取calendar的時間 } |
該clear()操作會造成線程不安全.
此外使用synchronized 關鍵字對性能有很大影響,尤其是多線程的時候,每一次調用parseymdhms方法都會進行同步判斷,并且同步本身開銷就很大,因此這是不合理的解決方案.
改進方法
線程不安全是源于多線程使用了共享變量造成,所以這里使用ThreadLocal<SimpleDateFormat>來給每個線程單獨創建副本變量,先給出代碼,再分析這樣的解決問題的原因.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/** * 日期工具類(使用了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
1
2
3
4
5
6
7
8
9
|
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(); } |
日志分析
1
2
3
4
|
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);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
//第一次執行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()的實現:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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()方法,也就是我們覆蓋的方法.
對應日志打印.
1
2
|
put new sdf of pattern MMddHHmmssSSS to map thread: Thread[main, 5 ,main] init pattern: MMddHHmmssSSS |
2.第二次在子線程執行DateUtil.formatDate(new Date(),MDHMSS);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//第二次在子線程執行`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()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//第二次在子線程執行`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
參考資料:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/shuilangyizu/p/8621733.html