ThreadLocal類的作用:爲每個線程創建獨立的副本,從而保證了線程安全。
ThreadLocal使用代碼示例:
public class MyThreadLocalTest {
private ThreadLocal<Integer> threadLocal=new ThreadLocal<Integer>(){
@Override
protected Integer initialValue() {
return 1;
}
};
public void StartThreadArray(){
Thread[] runs = new Thread[5];
for(int i=0;i<runs.length;i++){
runs[i]=new Thread(new MyThreadLocalTest.TestThread(1));
}
for(int i=0;i<runs.length;i++){
runs[i].start();
}
}
public class TestThread implements Runnable{
int id;
public TestThread(int id){
this.id = id;
}
public void run() {
System.out.println("this is :"+Thread.currentThread().getName()+":start");
Integer s = threadLocal.get();
s = s+id;
threadLocal.set(s);
System.out.println("this is :"+Thread.currentThread().getName()
+":"+ threadLocal.get());
}
}
public static void main(String[] args){
MyThreadLocalTest test = new MyThreadLocalTest();
test.StartThreadArray();
}
}
源碼解析:
當使用ThreadLocal時,線程第一次調用ThreadLocal的get()方時,ThreadLocal對象會先獲取當前線程對象,然後根據調用getMap(Thread t) 方法,從線程中獲取到ThreadLocalMap對象(這個對象其實就是線程中的ThreadLocal對象和TheadLocal中的元素的綁定關係,類似於一個Map集合),第一次調用get()方法時該對象爲空null,因爲還未建立映射關係,此時對調用到setInitialValue()。
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();
}
該方法會首先調用到initialValue(),你定義初始化的值,然後從線程中獲取ThreadLocalMap對象,判斷對象是否爲空,如果爲空則新建一個ThreadLocalMap對象並且調用createMap(Thread t,T value)方法。
該方法主要的作用是創建一個ThreadLocalMap對象,並且建立當前ThreadLocal對象以及初始化節點的對應關係,並且將這個ThreadLocalMap對象放入線程中的。
如果線程中的ThreadLocalMap對象不爲空,則將當前ThreadLocal對象和初始化節點的映射關係放入ThreadLocalMap中。
private T setInitialValue() {
T value = initialValue();
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
return value;
}
這就是線程第一次使用get方法。當線程第二次調用get()方法時,就直接從線程中的ThreadLocalMap中獲取值。
具體調用流程爲: