一. ThreadLocal介紹
ThreadLocal可以在不同的線程中互不干擾的存儲數(shù)據(jù)镣典。
ThreadLocal是一個(gè)線程內(nèi)部的數(shù)據(jù)存儲類,通過它可以在指定的線程中存儲數(shù)據(jù)柠辞,數(shù)據(jù)存儲后,只有在指定線程中可以獲取到存儲的數(shù)據(jù)主胧,對于其他線程來說則無法取到數(shù)據(jù)叭首。
二. 例子
public class ThreadLocalActivity extends AppCompatActivity {
private ThreadLocal threadLocal;
private static final String TAG = "ThreadLocalActivity-->";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thread_local);
threadLocal = new ThreadLocal();
threadLocal.set("初始值 0");
Log.d(TAG, "當(dāng)前線程:" + Thread.currentThread().getName() + " threadLocal值為:" + threadLocal.get());
HandlerThread handlerThread1 = new HandlerThread("線程一");
HandlerThread handlerThread2 = new HandlerThread("線程二");
handlerThread1.start();
handlerThread2.start();
Handler handler1 = new Handler(handlerThread1.getLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
threadLocal.set("子線程1設(shè)置值為 1");
Log.d(TAG, "當(dāng)前線程:" + Thread.currentThread().getName() + " threadLocal值為:" + threadLocal.get());
}
};
Handler handler2 = new Handler(handlerThread2.getLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.d(TAG, "當(dāng)前線程:" + Thread.currentThread().getName() + " threadLocal值為:" + threadLocal.get());
}
};
handler1.sendEmptyMessage(1);
handler2.sendEmptyMessage(2);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d(TAG, "當(dāng)前線程:" + Thread.currentThread().getName() + " threadLocal值為:" + threadLocal.get());
}
}
運(yùn)行該ThreadLocalActivity ,得到的日志為:
04-27 15:02:01.283 14375-14375/pri.hsj.thread D/ThreadLocalActivity-->: 當(dāng)前線程:main threadLocal值為:初始值 0
04-27 15:02:01.284 14375-14440/pri.hsj.thread D/ThreadLocalActivity-->: 當(dāng)前線程:線程二 threadLocal值為:null
04-27 15:02:01.284 14375-14439/pri.hsj.thread D/ThreadLocalActivity-->: 當(dāng)前線程:線程一 threadLocal值為:子線程1設(shè)置值為 1
04-27 15:02:01.784 14375-14375/pri.hsj.thread D/ThreadLocalActivity-->: 當(dāng)前線程:main threadLocal值為:初始值 0
三. 原因分析和總結(jié)
我們看下ThreadLocal源碼:ThreadLocal#set()方法和ThreadLocal#get()方法
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
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();
}
我們會發(fā)現(xiàn)不管是set()方法還是get()方法踪栋,都是對當(dāng)前線程做操作焙格,因此在不同線程中訪問同一個(gè)ThreadLocal的set()方法和get()方法,它們對ThreadLocal所作的讀/寫操作僅限于各自線程的內(nèi)部夷都,這就是為什么ThreadLocal可以在多個(gè)線程中互不干擾的存儲和修改數(shù)據(jù)眷唉。