Java-ThreadLocal
參考
用途
誤區(qū)
看到很多資料上都有一些誤區(qū): 即使用ThreadLocal是用于解決對象共享訪問問題, 線程安全問題等. 其實(shí)不然. 另外也不存在什么對對象的拷貝, 因?yàn)閷?shí)際上和線程相關(guān)的參數(shù)實(shí)際上就存儲在了Thread對象中的ThreadLocalMap threadLocals里面.
正確理解
最先接觸到Thread Local這個概念是使用Python的Flask框架. 該框架中有一個對象g. 文檔: flask.g.
該對象可以直接用from flask import g導(dǎo)入. 然后可以在需要存一些需要在多個地方使用的數(shù)據(jù)時, 可以g.set(), 然后需要獲取值的時候可以直接g.get(). 而比較神奇的是在多線程環(huán)境下, 每個使用到g的地方都是直接這樣引用的, 但是不同線程間的數(shù)據(jù)卻不會相互覆蓋. 其實(shí)g對象的實(shí)現(xiàn)就是使用了Thread Local.
所以個人理解, ThreadLocal其實(shí)主要是為了方便提供一些可能多個線程都需要訪問的數(shù)據(jù), 但是每個線程需要獨(dú)享一個這樣的對象. 如果用傳統(tǒng)的全局變量, 每個線程雖然都能訪問到, 但是會發(fā)生數(shù)據(jù)覆蓋的問題, 而使用Thread Local, 則可以很方便地在不傳遞過多參數(shù)的情況下實(shí)現(xiàn)一個線程對應(yīng)一個對象實(shí)例. 即這個數(shù)據(jù)需要對很多線程可見(global), 但每個線程其實(shí)都擁有一個獨(dú)享的該數(shù)據(jù)對象(local).
如果不使用ThreadLocal想要實(shí)現(xiàn)類似的功能, 其實(shí)用一個全局靜態(tài)Map<Thread, Value>就可以做到. 不過ThreadLocal就是為了簡化這個操作, 而且效率高, 所以直接使用ThreadLocal即可.
一個應(yīng)用場景(類似flask.g對象):
- 每個請求由一個線程處理
- 在請求處理過程中, 有多個地方需要用到某個數(shù)據(jù) (比如說
before_request,request_handling,post_request這幾個地方)
一個看起來可行的方法是直接在請求處理代碼中設(shè)置一個全局變量, 但是這樣不同線程就會讀到/修改同一個全局變量. 這時候使用ThreadLocal就可以很好地避免這個問題, 而不用我們自己去維護(hù)一個跟線程有關(guān)的Map來根據(jù)不同的線程獲取對應(yīng)的數(shù)據(jù).
ThreadLocal例子
TransactionManager類:
注意threadLocal是一個靜態(tài)的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(){
// 避免出現(xiàn)內(nèi)存泄露問題
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();
}
}
測試結(jié)果:
重點(diǎn)在于在main線程調(diào)用getTransactionID()的返回值并沒有因?yàn)槠陂g有一另個Thread設(shè)置了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變量的操作是不會有互相影響的. 因?yàn)樵?code>ThreadLocal變量對于所有線程都是全局的, 但是其存儲的數(shù)據(jù)卻是和線程相關(guān)的.
原理
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的一個靜態(tài)內(nèi)部類:
以下僅摘了了用于理解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();
}
}
所以實(shí)際ThreadLocal的set方法是將對象存儲到了調(diào)用該ThreadLocal的線程對象的threadLocals成員中. 而該成員的類型為ThreadLocalMap. 注意ThreadLocalMap的set方法, 其key的類型是任何類型的ThreadLocal對象. 所以ThreadLocalMap對象存儲了ThreadLocal -> value的鍵值對. 因?yàn)橐粋€線程可能使用多個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();
}
其實(shí)就是獲取當(dāng)先的線程, 然后得到其ThreadLocalMap類型的threadLocals對象. 然后傳遞this, 即用于表明當(dāng)前是要取得threadLocals中key為當(dāng)前這個ThreadLocal的對象.
關(guān)于內(nèi)存泄露
原因在上面那篇文章說得很清楚了.
接下來說一個關(guān)于ThreadLocal.remove()方法的實(shí)踐. 雖然有些情況不會造成內(nèi)存泄露, 我們可以不調(diào)用ThreadLocal.remove()方法. 但是這可能會造成一些其他問題, 比如說當(dāng)線程被線程池重用的時候. 如果線程在使用完ThreadLocal后沒有remove, 那么很可能下次該線程再次執(zhí)行的時候(可能是不同任務(wù)了), 就可能會讀到一個之前設(shè)置過的值.