ThreadLocal: understanding of local thread variables, threadlocal Variables
Generally, Web applications are divided into three layers: presentation layer, service layer, and persistence layer. The corresponding logic is written in different layers. The lower layer opens function calls through interfaces to the upper layer.In general, all program calls from receiving requests to returning responses belong to the same thread.That is to say, when the same thread runs through N layers, different threads may modify certain variables in the program due to different parameters, but also prevent the modified value from affecting other threads, because different threads can run drops at the same time, we need to solve the access conflict between variables shared by some threads. ThreadLocal local thread variable is a solution. It encapsulates insecure variables in the program into ThreadLocal, this is equivalent to providing an independent variable copy for each thread (in fact, it is a different object). Modifying the variable value in the thread does not affect other threads, because other threads have their own copy information. Code understanding:
1 // use the ThreadLocal object to create only one instance in each thread. 2 public static final String dateFormat = "yyyy-MM-dd "; 3 4 private static final ThreadLocal <DateFormat> dfThreadLocal = new ThreadLocal <DateFormat> () {5 @ Override 6 protected DateFormat initialValue () {7 return new SimpleDateFormat (dateFormat ); 8} 9}; 10 11 public static String dateToString (Date date) {12 return dfThreadLocal. get (). format (date); 13}
For each thread, there is a Map-like theme ThreadLocalMap (ThreadLocal static class). What is saved in it? It must be key-value, the key is the shared static variable dfThreadLocal in the above Code, and the value is the DateFormat instance, that is, the new SimpleDateFormat (dateFormat. Then, in the thread, how can I get this value? It is implemented by dfThreadLocal. get (). The source code of the method is as follows:
1 ThreadLocal .ThreadLocalMap inheritableThreadLocals = null ; 2 3 public T get () { 4 Thread t = Thread.currentThread (); 5 ThreadLocalMap map = getMap(t ); 6 if ( map != null) { 7 ThreadLocalMap.Entry e = map.getEntry (this); 8 if ( e != null) 9 return ( T)e .value;10 }11 return setInitialValue ();12 }13 14 ThreadLocalMap getMap (Thread t) {15 return t .inheritableThreadLocals;16 }Obviously,
First, according to Thread. currentThread () gets inheritableThreadLocals (ThreadLocalMap, which is a variable of Thread), and then uses this (that is, the dfThreadLocal object in the top Code) as the key (or index) get the true value T (that is, the SimpleDateFormat object)Ah, it should be clear now. Why different threads have their own values because
Different threads ---> different ThreadLocalMap objects (thread variables) ---> get different value values through the same key (if static). Note: It is generally modified by static, because it can avoid repeated creation of TSO (Thread Specific Object, that is, the Object associated with ThreadLocal) in a Thread and is modified by statis, the same thread key must be the same, and the value must have only one copy. A ThreadLocal instance is associated with a TSO object of the current thread. If ThreadLocal is declared as an instance variable, every creation of a class instance will lead to the birth of a TSO instance. This is certainly not necessary.
For more articles, see my personal blog:
Http://www.acanblog.com