一、ThreadLocal的作用
ThreadLocal以線程作為作用域,針對每個線程維護他們自己的數(shù)據(jù)。每個線程使用同一個ThreadLocal存儲數(shù)據(jù),但是拿到的數(shù)據(jù)都只會是當(dāng)前這個線程所存儲的數(shù)據(jù),不會拿到其他線程存儲的數(shù)據(jù)。
public class MainActivity extends AppCompatActivity {
private static final String TAG = "ThreadLoacalTest";
private ThreadLocal<Boolean> mBooleanThreadLocal = new ThreadLocal<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
mBooleanThreadLocal.set(true);
printThreadLocal();
new Thread("Thread#1"){
@Override
public void run() {
mBooleanThreadLocal.set("name");
printThreadLocal();
}
}.start();
new Thread("Thread#2"){
@Override
public void run() {
printThreadLocal();
}
}.start();
}
private void printThreadLocal() {
Log.d(TAG, "[Thread#" + Thread.currentThread().getName() + "#" + Thread.currentThread().getId() + "]mBooleanThreadLocal=" + mBooleanThreadLocal.get() );
}
}
07-17 11:23:19.773 3162-3162/com.chuckchan.threadlocalsample D/ThreadLoacalTest: [Thread#main#2]mBooleanThreadLocal=true
07-17 11:23:19.777 3162-3181/com.chuckchan.threadlocalsample D/ThreadLoacalTest: [Thread#Thread#2#151]mBooleanThreadLocal=null
07-17 11:23:19.798 3162-3180/com.chuckchan.threadlocalsample D/ThreadLoacalTest: [Thread#Thread#1#150]mBooleanThreadLocal=false
二、 ThreadLocal原理
1.ThreadLocal的set方法
1.獲取當(dāng)前線程對象
2.獲取當(dāng)前線程維護的Values對象
3.如果values是空,為當(dāng)前線程創(chuàng)建一個Values對象
4.將ThreadLocal引用和值傳遞給Values
public void set(T value) {
Thread currentThread = Thread.currentThread();
Values values = values(currentThread);
if (values == null) {
values = initializeValues(currentThread);
}
values.put(this, value);
}
Values values(Thread current) {
return current.localValues;
}
2.Thread的Values對象
每個Thread對象都有一個Values對象,這個Values對象的類型是ThreadLocal.Values。它的put方法
1.遍歷數(shù)組,如果當(dāng)前ThreadLocal已經(jīng)維護了一個對象,那么替換這個對象為新值
2.如果沒有存過數(shù)據(jù),那么找數(shù)組中的一個空白位置,在index和index+1這兩個相鄰的位置分別存儲鍵和值
void put(ThreadLocal<?> key, Object value) {
cleanUp();
// Keep track of first tombstone. That's where we want to go back
// and add an entry if necessary.
int firstTombstone = -1;
for (int index = key.hash & mask;; index = next(index)) {
Object k = table[index];
if (k == key.reference) {
// Replace existing entry.
table[index + 1] = value;
return;
}
if (k == null) {
if (firstTombstone == -1) {
// Fill in null slot.
table[index] = key.reference;
table[index + 1] = value;
size++;
return;
}
// Go back and replace first tombstone.
table[firstTombstone] = key.reference;
table[firstTombstone + 1] = value;
tombstones--;
size++;
return;
}
// Remember first tombstone.
if (firstTombstone == -1 && k == TOMBSTONE) {
firstTombstone = index;
}
}
}
ThreadLocal的get方法
1.獲取當(dāng)前線程的Values對象
2.獲取Values內(nèi)部維護的數(shù)組
3 .計算出ThreadLocal對象的index
4.如果index在數(shù)組中存在,那么從index+1的位置取出值
public T get() {
// Optimized for the fast path.
Thread currentThread = Thread.currentThread();
Values values = values(currentThread);
if (values != null) {
Object[] table = values.table;
int index = hash & values.mask;
if (this.reference == table[index]) {
return (T) table[index + 1];
}
} else {
values = initializeValues(currentThread);
}
return (T) values.getAfterMiss(this);
}
三、總結(jié)
1.每一個Thread內(nèi)部都持有一個ThreadLocal.Values的靜態(tài)對象
2.ThreadLocal.Values內(nèi)部維護了一個數(shù)組存儲鍵和值
3.ThreadLocal拿到當(dāng)前Thread所維護的Values對象,并將自身的引用和要存的值傳遞個Values
4.Values根據(jù)threadlocal引用計算出它的index,如果在index位置有值存在就替換原來的值,如果不存在就替換原來的值