ThreadLocal用于在当前线程中存储数据,由于存储的数据只能在当前线程内使用,所以自然是线程安全的。
Handler体系中,Looper只会存在一个实例,且只在当前线程使用,所以使用ThreadLocal进行存储。
frameworks/base/core/java/android/os/Looper.java
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}
Looper类中使用ThreadLocal来存储Looper对象,在调用prepare方法的时候,判断ThreadLocal对象中是否包含Looper对象,如果包含,说明重复调用了prepare,会抛异常。调用myLooper的时候就从ThreadLocal对象中获取Looper对象。
从上面可以看出ThreadLocal类似于一个容器,可以存储一个对象,通过set方法将对象存储到ThreadLocal容器中,通过get从ThreadLocal容器中获取对象。
首先看一下ThreadLocal的构造方法
public ThreadLocal() {
}
是空参构造,没有任何处理
接下来看ThreadLocal的set方法
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
首先获取当前Thread对象,然后通过Thread对象获取ThreadLocalMap如果map为空,则创建Map,如果map不为空,调用set方法,则将ThreadLocal作为key,元素作为value设置到map中
java/lang/ThreadLocal.java
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
java/lang/Thread.java
ThreadLocal.ThreadLocalMap threadLocals = null;
每一个Thread对象都持有一个ThreadLocalMap对象,如果该对象没有创建的话,就会调用set里面的createMap进行创建。
java/lang/ThreadLocal.java
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
初始化ThreadLocalMap,传入参数为ThreadLocal对象和Object对象
java/lang/ThreadLocal.java
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
table = new Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY);
}
ThreadLocalMap存储元素采用的是Entry数组,初始容量为16,跟HashMap方式类似,采用2的指数次为数组长度进行hash。
ThreadLocalMap的set方法
private void set(ThreadLocal<?> key, Object value) {
// We don't use a fast path as with get() because it is at
// least as common to use set() to create new entries as
// it is to replace existing ones, in which case, a fast
// path would fail more often than not.
Entry[] tab = table;
int len = tab.length;
int i = key.threadLocalHashCode & (len-1);
for (Entry e = tab[i];
e != null;
e = tab[i = nextIndex(i, len)]) {
// Android-changed: Use refersTo() (twice).
// ThreadLocal> k = e.get();
// if (k == key) { ... } if (k == null) { ... }
if (e.refersTo(key)) {
e.value = value;
return;
}
if (e.refersTo(null)) {
replaceStaleEntry(key, value, i);
return;
}
}
tab[i] = new Entry(key, value);
int sz = ++size;
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}
采用hash算法,使用ThreadLocal对象作为Key计算索引,并存入value。
ThreadLocal的get方法
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();
}
首先获取Thread对象,然后获取Thread对象中的ThreadLocalMap对象,然后调用getEntry获取Entry对象,并返回其value。
private Entry getEntry(ThreadLocal<?> key) {
int i = key.threadLocalHashCode & (table.length - 1);
Entry e = table[i];
// Android-changed: Use refersTo().
if (e != null && e.refersTo(key))
return e;
else
return getEntryAfterMiss(key, i, e);
}
也是通过hash算法算出索引后返回Entry对象