一、ThreadLocal的作用
ThreadLocal以线程作为作用域,针对每个线程维护他们自己的数据。每个线程使用同一个ThreadLocal存储数据,但是拿到的数据都只会是当前这个线程所存储的数据,不会拿到其他线程存储的数据。
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.获取当前线程对象
2.获取当前线程维护的Values对象
3.如果values是空,为当前线程创建一个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.遍历数组,如果当前ThreadLocal已经维护了一个对象,那么替换这个对象为新值
2.如果没有存过数据,那么找数组中的一个空白位置,在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.获取当前线程的Values对象
2.获取Values内部维护的数组
3 .计算出ThreadLocal对象的index
4.如果index在数组中存在,那么从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);
}
三、总结
1.每一个Thread内部都持有一个ThreadLocal.Values的静态对象
2.ThreadLocal.Values内部维护了一个数组存储键和值
3.ThreadLocal拿到当前Thread所维护的Values对象,并将自身的引用和要存的值传递个Values
4.Values根据threadlocal引用计算出它的index,如果在index位置有值存在就替换原来的值,如果不存在就替换原来的值
网友评论