ThreadLocal源码分析
threadlocal是一个线程内部的数据存储类。某线程储存的数据,只有这个线程中才能获取到存储的数据。而其他线程是无法获取到这个数据的。
一、ThreadLocal的使用
private ThreadLocal<Integer> mThreadLocal = new ThreadLocal<Integer>();
/***创建一个泛型为String类型的ThreadLocal**/
private ThreadLocal<String> mStringThreadLocal = new ThreadLocal<String>();
new Thread("thread#1"){
@Override
public void run() {
Log.i(TAG, "run: [Therad#1]mThreadLocal" );
mThreadLocal.set(10);
Log.i(TAG, "run: [Therad#1]mThreadLocal=" + mThreadLocal.get() );
/**为mStringThreadLocal设置值***/
mStringThreadLocal.set("string10");
Log.i(TAG, "run: [Therad#1]mStringThreadLocal=" + mStringThreadLocal.get() );
}
}.start();
new Thread("thread#2"){
@Override
public void run() {
mThreadLocal.set(400);
Log.i(TAG, "run: [Therad#2]mThreadLocal=" + mThreadLocal.get() );
mStringThreadLocal.set("string400");
Log.i(TAG, "run: [Therad#2]mStringThreadLocal=" + mStringThreadLocal.get() );
}
}.start();
二、存储值
1、存储值
public void set(T value) {
Thread t = Thread.currentThread();/***获取当前线程**/
ThreadLocalMap map = getMap(t);/**根据线程获取线程的成员变量ThreadLocalMap,它是一种Map映射结构***/
if (map != null)
map.set(this, value);
else/**首次一般map为空,需要为thread t创建一个ThreadLocalMap**/
createMap(t, value);
}
2、获取线程的ThreadLocalMap
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
3、首次存储值,要创建一个LocalThreadMap复制给当前线程。
void createMap(Thread t, T firstValue) { /**为thread t创建一个ThreadLocalMap**/
t.threadLocals = new ThreadLocalMap(this, firstValue);/**并且首个键值对key-value:key为当前的ThreadLocal(比如mBooleanThread,mStringThread),values为firstValue**/
}
非首次存储值,直接存储就行了
if (map != null)
map.set(this, value);
4、创建构造LocalThreadMap中会创建一个Entry结构类型数组表table ,
将根据首个要存储的数据:当前ThreadLocal和Value创建生成一个Entry对象,
并放入table表中第i个位置(i的值是有当前的ThreadLocal中threadLocalHashCode值间接得到的)
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) { /**首个键值对key-value:key为当前的ThreadLocal(比如mBooleanThread,mStringThread),values为firstValue**/
table = new Entry[INITIAL_CAPACITY];/***创建一个Entry[]数组表**/
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);/**首页key-value组装成一个Entry,放在table中的第i个位置**/
size = 1;
setThreshold(INITIAL_CAPACITY);
}
看看Entry的具体数据结构
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
ThreadLocal的具体数据结构
static class ThreadLocalMap {
/**
* The initial capacity -- MUST be a power of two.
*/
private static final int INITIAL_CAPACITY = 16;
/**
* The table, resized as necessary.
* table.length MUST always be a power of two.
*/
private Entry[] table;
/**
* The number of entries in the table.
*/
private int size = 0;
/**
* The next size value at which to resize.
*/
private int threshold; // Default to 0
}
三、取值
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);/**拿到当前线程的ThreadLocalMap**/
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);/***根据当前的mThreadLocal获取TheadMap**/
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;/***从Entry中取出value即可**/
return result;
}
}
return setInitialValue();
}
1、根据当前线程获取到线程对应的ThreadLocalMap
然后根据当前的ThreadLocal从ThreadLocalMap中获取Entry()
private Entry getEntry(ThreadLocal<?> key) {
int i = key.threadLocalHashCode & (table.length - 1);//根据ThreadLocal中的hashcode做为索引去线程中ThreadLocalMap的table数组表中拿到Entry,然后从Entry
Entry e = table[i];
if (e != null && e.get() == key)
return e;
else
return getEntryAfterMiss(key, i, e);
}
2、直接返回值即可
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;/***从Entry中取出value即可**/
return result;
}