Java-ThreadLocal
參考
用途
誤區
看到很多資料上都有一些誤區: 即使用ThreadLocal
是用于解決對象共享訪問問題, 線程安全問題等. 其實不然. 另外也不存在什么對對象的拷貝, 因為實際上和線程相關的參數實際上就存儲在了Thread
對象中的ThreadLocalMap threadLocals
里面.
正確理解
最先接觸到Thread Local這個概念是使用Python的Flask框架. 該框架中有一個對象g
. 文檔: flask.g.
該對象可以直接用from flask import g
導入. 然后可以在需要存一些需要在多個地方使用的數據時, 可以g.set()
, 然后需要獲取值的時候可以直接g.get()
. 而比較神奇的是在多線程環境下, 每個使用到g
的地方都是直接這樣引用的, 但是不同線程間的數據卻不會相互覆蓋. 其實g
對象的實現就是使用了Thread Local
.
所以個人理解, ThreadLocal
其實主要是為了方便提供一些可能多個線程都需要訪問的數據, 但是每個線程需要獨享一個這樣的對象. 如果用傳統的全局變量, 每個線程雖然都能訪問到, 但是會發生數據覆蓋的問題, 而使用Thread Local
, 則可以很方便地在不傳遞過多參數的情況下實現一個線程對應一個對象實例. 即這個數據需要對很多線程可見(global), 但每個線程其實都擁有一個獨享的該數據對象(local).
如果不使用ThreadLocal
想要實現類似的功能, 其實用一個全局靜態Map<Thread, Value>
就可以做到. 不過ThreadLocal
就是為了簡化這個操作, 而且效率高, 所以直接使用ThreadLocal
即可.
一個應用場景(類似flask.g對象):
- 每個請求由一個線程處理
- 在請求處理過程中, 有多個地方需要用到某個數據 (比如說
before_request
,request_handling
,post_request
這幾個地方)
一個看起來可行的方法是直接在請求處理代碼中設置一個全局變量, 但是這樣不同線程就會讀到/修改同一個全局變量. 這時候使用ThreadLocal
就可以很好地避免這個問題, 而不用我們自己去維護一個跟線程有關的Map
來根據不同的線程獲取對應的數據.
ThreadLocal
例子
TransactionManager類:
注意threadLocal
是一個靜態的ThreadLocal
變量. 意味著全部的線程訪問的都是同一個ThreadLocal
對象.
package multithreading.threadlocal;
/**
* Created by xiaofu on 17-11-15.
* https://dzone.com/articles/painless-introduction-javas-threadlocal-storage
*/
public class TransactionManager {
private static ThreadLocal<String> threadLocal = new ThreadLocal<>();
public static void newTransaction(){
// 生成一個新的transaction id
String id = "" + System.currentTimeMillis();
threadLocal.set(id);
}
public static void endTransaction(){
// 避免出現內存泄露問題
threadLocal.remove();
}
public static String getTransactionID(){
return threadLocal.get();
}
}
ThreadLocalTest類:
package multithreading.threadlocal;
/**
* Created by xiaofu on 17-11-15.
* https://dzone.com/articles/painless-introduction-javas-threadlocal-storage
*/
public class ThreadLocalTest {
public static class Task implements Runnable{
private String name;
public Task(String name){this.name = name;}
@Override
public void run() {
TransactionManager.newTransaction();
System.out.printf("Task %s transaction id: %s\n", name, TransactionManager.getTransactionID());
TransactionManager.endTransaction();
}
}
public static void main(String[] args) throws InterruptedException {
// 在main線程先操作一下TransactionManager
TransactionManager.newTransaction();
System.out.println("Main transaction id: " + TransactionManager.getTransactionID());
String taskName = "[Begin a new transaction]";
Thread thread = new Thread(new Task(taskName));
thread.start();
thread.join();
System.out.println(String.format("Task %s is done", taskName));
System.out.println("Main transaction id: " + TransactionManager.getTransactionID());
TransactionManager.endTransaction();
}
}
測試結果:
重點在于在main線程調用getTransactionID()
的返回值并沒有因為期間有一另個Thread
設置了TransactionManger
中的ThreadLocal
變量的值而改變.
Main transaction id: 1510730858223
Task [Begin a new transaction] transaction id: 1510730858224
Task [Begin a new transaction] is done
Main transaction id: 1510730858223
可以看出不同線程對于同一個ThreadLocal
變量的操作是不會有互相影響的. 因為該ThreadLocal
變量對于所有線程都是全局的, 但是其存儲的數據卻是和線程相關的.
原理
ThreadLoccal
類的set
方法:
/**
* Sets the current thread's copy of this thread-local variable
* to the specified value. Most subclasses will have no need to
* override this method, relying solely on the {@link #initialValue}
* method to set the values of thread-locals.
*
* @param value the value to be stored in the current thread's 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);
}
getMap()
方法:
/**
* 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;
}
從上面代碼可以看出Thread
類是有一個叫threadLocals
的成員的.
public
class Thread implements Runnable{
// ... 省略 ...
/* ThreadLocal values pertaining to this thread. This map is maintained
* by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;
// ... 省略 ...
}
ThreadLocalMap
是ThreadLocal
的一個靜態內部類:
以下僅摘了了用于理解ThreadLocal
原理的代碼:
/**
* 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{
/**
* The entries in this hash map extend WeakReference, using
* its main ref field as the key (which is always a
* ThreadLocal object). Note that null keys (i.e. entry.get()
* == null) mean that the key is no longer referenced, so the
* entry can be expunged from table. Such entries are referred to
* as "stale entries" in the code that follows.
*/
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
/**
* The table, resized as necessary.
* table.length MUST always be a power of two.
*/
private Entry[] table;
/**
* Set the value associated with key.
*
* @param key the thread local object
* @param value the value to be set
*/
private void set(ThreadLocal<?> key, Object value) {
// We don't use a fast path as with get() because it is at
// least as common to use set() to create new entries as
// it is to replace existing ones, in which case, a fast
// path would fail more often than not.
Entry[] tab = table;
int len = tab.length;
int i = key.threadLocalHashCode & (len-1);
for (Entry e = tab[i];
e != null;
e = tab[i = nextIndex(i, len)]) {
ThreadLocal<?> k = e.get();
if (k == key) {
e.value = value;
return;
}
if (k == null) {
replaceStaleEntry(key, value, i);
return;
}
}
tab[i] = new Entry(key, value);
int sz = ++size;
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}
}
所以實際ThreadLocal
的set
方法是將對象存儲到了調用該ThreadLocal
的線程對象的threadLocals
成員中. 而該成員的類型為ThreadLocalMap
. 注意ThreadLocalMap
的set
方法, 其key
的類型是任何類型的ThreadLocal
對象. 所以ThreadLocalMap
對象存儲了ThreadLocal
-> value
的鍵值對. 因為一個線程可能使用多個ThreadLocal
對象, 所以使用了ThreadLocalMap
來管理這些值. 這也解釋了ThreadLocal
的set
方法中map.set(this, value);
這句代碼的意思.
再來看ThreadLoccal類的get
方法:
/**
* Returns the value in the current thread's copy of this
* thread-local variable. If the variable has no value for the
* current thread, it is first initialized to the value returned
* by an invocation of the {@link #initialValue} 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) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
其實就是獲取當先的線程, 然后得到其ThreadLocalMap
類型的threadLocals
對象. 然后傳遞this
, 即用于表明當前是要取得threadLocals
中key
為當前這個ThreadLocal
的對象.
關于內存泄露
原因在上面那篇文章說得很清楚了.
接下來說一個關于ThreadLocal.remove()
方法的實踐. 雖然有些情況不會造成內存泄露, 我們可以不調用ThreadLocal.remove()
方法. 但是這可能會造成一些其他問題, 比如說當線程被線程池重用的時候. 如果線程在使用完ThreadLocal
后沒有remove
, 那么很可能下次該線程再次執行的時候(可能是不同任務了), 就可能會讀到一個之前設置過的值.