Java ThreadLocal深度解析
首先,ThreadLocal 不是用來解決共享物件的多執行緒訪問問題的,一般情況下,通過ThreadLocal.set() 到執行緒中的物件是該執行緒自己使用的物件,其他執行緒是不需要訪問的,也訪問不到的。各個執行緒中訪問的是不同的物件。
另外,說ThreadLocal使得各執行緒能夠保持各自獨立的一個物件,並不是通過ThreadLocal.set()來實現的,而是通過每個執行緒中的new 物件 的操作來建立的物件,每個執行緒建立一個,不是什麼物件的拷貝或副本。通過ThreadLocal.set()將這個新建立的物件的引用儲存到各執行緒的自己的一個map中,每個執行緒都有這樣一個map,執行ThreadLocal.get()時,各執行緒從自己的map中取出放進去的物件,因此取出來的是各自自己執行緒中的物件,ThreadLocal例項是作為map的key來使用的。
如果ThreadLocal.set()進去的東西本來就是多個執行緒共享的同一個物件,那麼多個執行緒的ThreadLocal.get()取得的還是這個共享物件本身,還是有併發訪問問題。
下面來看一個hibernate中典型的ThreadLocal的應用:
private static final ThreadLocal threadSession = new ThreadLocal(); public static Session getSession() throws InfrastructureException { Session s = (Session) threadSession.get(); try { if (s == null) { s = getSessionFactory().openSession(); threadSession.set(s); } } catch (HibernateException ex) { throw new InfrastructureException(ex); } return s; }
可以看到在getSession()方法中,首先判斷當前執行緒中有沒有放進去session,如果還沒有,那麼通過sessionFactory().openSession()來建立一個session,再將session set到執行緒中,實際是放到當前執行緒的ThreadLocalMap這個map中,這時,對於這個session的唯一引用就是當前執行緒中的那個ThreadLocalMap(下面會講到),而threadSession作為這個值的key,要取得這個session可以通過threadSession.get()來得到,裡面執行的操作實際是先取得當前執行緒中的ThreadLocalMap,然後將threadSession作為key將對應的值取出。這個session相當於執行緒的私有變數,而不是public的。 顯然,其他執行緒中是取不到這個session的,他們也只能取到自己的ThreadLocalMap中的東西。要是session是多個執行緒共享使用的,那還不亂套了。 試想如果不用ThreadLocal怎麼來實現呢?可能就要在action中建立session,然後把session一個個傳到service和dao中,這可夠麻煩的。或者可以自己定義一個靜態的map,將當前thread作為key,建立的session作為值,put到map中,應該也行,這也是一般人的想法,但事實上,ThreadLocal的實現剛好相反,它是在每個執行緒中有一個map,而將ThreadLocal例項作為key,這樣每個map中的項數很少,而且當執行緒銷燬時相應的東西也一起銷燬了,不知道除了這些還有什麼其他的好處。
總之,ThreadLocal不是用來解決物件共享訪問問題的,而主要是提供了保持物件的方法和避免引數傳遞的方便的物件訪問方式。歸納了兩點: 1。每個執行緒中都有一個自己的ThreadLocalMap類物件,可以將執行緒自己的物件保持到其中,各管各的,執行緒可以正確的訪問到自己的物件。 2。將一個共用的ThreadLocal靜態例項作為key,將不同物件的引用儲存到不同執行緒的ThreadLocalMap中,然後線上程執行的各處通過這個靜態ThreadLocal例項的get()方法取得自己執行緒儲存的那個物件,避免了將這個物件作為引數傳遞的麻煩。
當然如果要把本來執行緒共享的物件通過ThreadLocal.set()放到執行緒中也可以,可以實現避免引數傳遞的訪問方式,但是要注意get()到的是那同一個共享物件,併發訪問問題要靠其他手段來解決。但一般來說執行緒共享的物件通過設定為某類的靜態變數就可以實現方便的訪問了,似乎沒必要放到執行緒中。
ThreadLocal的應用場合,我覺得最適合的是按執行緒多例項(每個執行緒對應一個例項)的物件的訪問,並且這個物件很多地方都要用到。
下面來看看ThreadLocal的實現原理(jdk1.5原始碼)
public class ThreadLocal<T> { /** * ThreadLocals rely on per-thread hash maps attached to each thread * (Thread.threadLocals and inheritableThreadLocals). The ThreadLocal * objects act as keys, searched via threadLocalHashCode. This is a * custom hash code (useful only within ThreadLocalMaps) that eliminates * collisions in the common case where consecutively constructed * ThreadLocals are used by the same threads, while remaining well-behaved * in less common cases. */ private final int threadLocalHashCode = nextHashCode(); /** * The next hash code to be given out. Accessed only by like-named method. */ private static int nextHashCode = 0; /** * The difference between successively generated hash codes - turns * implicit sequential thread-local IDs into near-optimally spread * multiplicative hash values for power-of-two-sized tables. */ private static final int HASH_INCREMENT = 0x61c88647; /** * Compute the next hash code. The static synchronization used here * should not be a performance bottleneck. When ThreadLocals are * generated in different threads at a fast enough rate to regularly * contend on this lock, memory contention is by far a more serious * problem than lock contention. */ private static synchronized int nextHashCode() { int h = nextHashCode; nextHashCode = h + HASH_INCREMENT; return h; } /** * Creates a thread local variable. */ public ThreadLocal() { } /** * Returns the value in the current thread's copy of this thread-local * variable. Creates and initializes the copy if this is the first time * the thread has called this method. * * @return the current thread's value of this thread-local */ public T get() { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) return (T)map.get(this); // Maps are constructed lazily. if the map for this thread // doesn't exist, create it, with this ThreadLocal and its // initial value as its only entry. T value = initialValue(); createMap(t, value); return value; } /** * Sets the current thread's copy of this thread-local variable * to the specified value. Many applications will have no need for * this functionality, relying solely on the {@link #initialValue} * method to set the values of thread-locals. * * @param value the value to be stored in the current threads' copy of * this thread-local. */ public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); } /** * Get the map associated with a ThreadLocal. Overridden in * InheritableThreadLocal. * * @param t the current thread * @return the map */ ThreadLocalMap getMap(Thread t) { return t.threadLocals; } /** * Create the map associated with a ThreadLocal. Overridden in * InheritableThreadLocal. * * @param t the current thread * @param firstValue value for the initial entry of the map * @param map the map to store. */ void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue); } ....... /** * ThreadLocalMap is a customized hash map suitable only for * maintaining thread local values. No operations are exported * outside of the ThreadLocal class. The class is package private to * allow declaration of fields in class Thread. To help deal with * very large and long-lived usages, the hash table entries use * WeakReferences for keys. However, since reference queues are not * used, stale entries are guaranteed to be removed only when * the table starts running out of space. */ static class ThreadLocalMap { ........ } }
可以看到ThreadLocal類中的變數只有這3個int型:
private final int threadLocalHashCode = nextHashCode(); private static int nextHashCode = 0; private static final int HASH_INCREMENT = 0x61c88647;
而作為ThreadLocal例項的變數只有 threadLocalHashCode 這一個,nextHashCode 和HASH_INCREMENT 是ThreadLocal類的靜態變數,實際上HASH_INCREMENT是一個常量,表示了連續分配的兩個ThreadLocal例項的threadLocalHashCode值的增量,而nextHashCode 的表示了即將分配的下一個ThreadLocal例項的threadLocalHashCode 的值。
可以來看一下建立一個ThreadLocal例項即new ThreadLocal()時做了哪些操作,從上面看到建構函式ThreadLocal()裡什麼操作都沒有,唯一的操作是這句:
private final int threadLocalHashCode = nextHashCode();
那麼nextHashCode()做了什麼呢:
private static synchronized int nextHashCode() { int h = nextHashCode; nextHashCode = h + HASH_INCREMENT; return h; }
就是將ThreadLocal類的下一個hashCode值即nextHashCode的值賦給例項的threadLocalHashCode,然後nextHashCode的值增加HASH_INCREMENT這個值。
因此ThreadLocal例項的變數只有這個threadLocalHashCode,而且是final的,用來區分不同的ThreadLocal例項,ThreadLocal類主要是作為工具類來使用,那麼ThreadLocal.set()進去的物件是放在哪兒的呢?
看一下上面的set()方法,兩句合併一下成為
ThreadLocalMap map = Thread.currentThread().threadLocals;
這個ThreadLocalMap 類是ThreadLocal中定義的內部類,但是它的例項卻用在Thread類中:
public class Thread implements Runnable { ...... /* ThreadLocal values pertaining to this thread. This map is maintained * by the ThreadLocal class. */ ThreadLocal.ThreadLocalMap threadLocals = null; ...... }
再看這句:
if (map != null) map.set(this, value);
也就是將該ThreadLocal例項作為key,要保持的物件作為值,設定到當前執行緒的ThreadLocalMap 中,get()方法同樣大家看了程式碼也就明白了。
相關文章
- Java ThreadLocal解析Javathread
- ThreadLocal原始碼解析-Java8thread原始碼Java
- ThreadLocal解析thread
- 解析ThreadLocalthread
- ThreadLocal 解析thread
- ThreadLocal之深度解讀thread
- ThreadLocal原始碼深度剖析thread原始碼
- 深度解析JAVA序列化Java
- java單例模式深度解析Java單例模式
- ThreadLocal原理深入解析thread
- ThreadLocal原始碼解析thread原始碼
- java8Stream原理深度解析Java
- 深度解析Java記憶體原型Java記憶體原型
- Java - ThreadLocal類Javathread
- JAVA集合:TreeMap紅黑樹深度解析Java
- JAVA集合:HashMap深度解析(版本對比)JavaHashMap
- Java 記憶體模型 JMM 深度解析Java記憶體模型
- Java記憶體模型深度解析:鎖Java記憶體模型
- Thread、ThreadLocal原始碼解析thread原始碼
- ThreadLocal底層原始碼解析thread原始碼
- JAVA集合:ConcurrentHashMap深度解析(版本對比)JavaHashMap
- Java記憶體模型深度解析:重排序Java記憶體模型排序
- Java記憶體模型深度解析:volatileJava記憶體模型
- Java記憶體模型深度解析:finalJava記憶體模型
- Java記憶體模型深度解析:總結Java記憶體模型
- ThreadLocal與ThreadLocalMap原始碼解析thread原始碼
- Java併發——ThreadLocal分析Javathread
- Java基礎(1)——ThreadLocalJavathread
- Java 之 ThreadLocal 詳解Javathread
- java之ThreadLocal筆記Javathread筆記
- Java ThreadLocal 使用詳解Javathread
- 語言小知識-Java ArrayList類 深度解析Java
- 語言小知識-Java HashMap類 深度解析JavaHashMap
- Java記憶體模型深度解析:基礎部分Java記憶體模型
- 一次ThreadLocal原始碼解析之旅thread原始碼
- Java併發程式設計:ThreadLocal的使用以及實現原理解析Java程式設計thread
- 深度解析HashMapHashMap
- 深度解析CNNCNN