Looper
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
Looper
中创建了一个ThreadLocal<T> T
为Looper
的变量
public static void prepare() {
prepare(true);
}
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));
}
在prepare
方法中,调用了sThreadLocal.set()
和sThreadLocal.get()
两个方法,会在下面展开
其中new Looper(quitAllowed)
是调用了构造函数
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
很显然,在任意线程调用,创建的Looper
就会持有该线程的引用。
同时我们看到,还有另一个方法能创建调用了prepare
public static void prepareMainLooper() {
prepare(false);
synchronized (Looper.class) {
if (sMainLooper != null) {
throw new IllegalStateException("The main Looper has already been prepared.");
}
sMainLooper = myLooper();
}
}
在这个方法中,先是通过prepare
方法,调用sMainLooper的sThreadLocal.set()
方法。
- 接着通过下面这个
myLooper()
方法获取,这时候这个threadLocal
又出现了,我们再次放一放。
/**
* Return the Looper object associated with the current thread. Returns
* null if the calling thread is not associated with a Looper.
*/
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}
看到这儿可能会有疑问,注释中说的是返回与当前线程有关联的Looper
,并不是返回主线程的Looper
,那为何prepareMainLooper
方法是直接调用这个方法呢?
我们回到prepareMainLooper()
方法,它的注释是这样说的
/**
* Initialize the current thread as a looper, marking it as an
* application's main looper. The main looper for your application
* is created by the Android environment, so you should never need
* to call this function yourself. See also: {@link #prepare()}
*/
也就是说,这个方法只会在应用创建时被程序自己调用,不应该被我们调用,这也确保了当前是处在主线程中。
public static Looper getMainLooper() {
synchronized (Looper.class) {
return sMainLooper;
}
}
这个静态方法,方便了我们在程序中,可以快速的拿到主线程Looper
。
ThreadLocal
说到现在,我们已经把Looper
中和ThreadLocal
相关的部分都讲完了,接下来,就直接跳转到ThreadLocal
类中,看看set
和get
分别是什么。
set
/**
* Sets the current thread's copy of this thread-local variable
* to the specified value. Most subclasses will have no need to
* override this method, relying solely on the {@link #initialValue}
* method to set the values of thread-locals.
*
* @param value the value to be stored in the current thread's copy of
* this thread-local.
*/
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
get
/**
* Returns the value in the current thread's copy of this
* thread-local variable. If the variable has no value for the
* current thread, it is first initialized to the value returned
* by an invocation of the {@link #initialValue} method.
*
* @return the current thread's value of this thread-local
*/
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();
}
前两行代码都是相同的,获取当前线程的ThreadLocalMap
变量。
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
查看Thread
代码,发现这个threadLocals
是ThreadLocal.ThreadLocalMap
类型的变量。
ThreadLocalMap
是定义在ThreadLocal
中的内部类
/**
* Create the map associated with a ThreadLocal. Overridden in
* InheritableThreadLocal.
*
* @param t the current thread
* @param firstValue value for the initial entry of the map
*/
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
//构造函数
/**
* Construct a new map initially containing (firstKey, firstValue).
* ThreadLocalMaps are constructed lazily, so we only create
* one when we have at least one entry to put in it.
*/
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);
}
createMap
中调用了构造函数,参数为this
和firstValue
我们发现,对于Looper
来说,第一个参数实际上是ThreadLocal<Looper>
,而第二个参数是这个范型对应的具体对象。
每个线程都有ThreadLocalMap
对象,其中的对应一个hash表Entry[]
,存储了不同范型对象的键值对。
原理总结
线程共享变量缓存如下:
Thread.ThreadLocalMap<ThreadLocal, Object>
;
Thread: 当前线程,可以通过
Thread.currentThread()
获取。ThreadLocal:我们的static ThreadLocal变量。
Object: 当前线程共享变量。
我们调用ThreadLocal.get
方法时,实际上是从当前线程中获取ThreadLocalMap<ThreadLocal, Object>,然后根据当前ThreadLocal
获取当前线程共享变量Object。
ThreadLocal.set
,ThreadLocal.remove
实际上是同样的道理。
这种存储结构的好处:
线程死去的时候,线程共享变量
ThreadLocalMap
则销毁。ThreadLocalMap<ThreadLocal,Object>
键值对数量为ThreadLocal
的数量,一般来说ThreadLocal
数量很少,相比在ThreadLocal
中用Map<Thread, Object>
键值对存储线程共享变量(Thread数量一般来说比ThreadLocal数量多),性能提高很多。
内存泄漏问题
其中Entry类的定义如下
/**
* The entries in this hash map extend WeakReference, using
* its main ref field as the key (which is always a
* ThreadLocal object). Note that null keys (i.e. entry.get()
* == null) mean that the key is no longer referenced, so the
* entry can be expunged from table. Such entries are referred to
* as "stale entries" in the code that follows.
*/
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
ThreadLocalMap<ThreadLocal, Object>弱引用问题:
当线程没有结束,但是ThreadLocal
已经被回收,则可能导致线程中存ThreadLocalMap<null, Object>的键值对,造成内存泄露。
(ThreadLocal
被回收,ThreadLocal
关联的线程共享变量还存在)。
虽然ThreadLocal的get,set方法可以清除ThreadLocalMap
中key为null的value,但是get,set方法在内存泄露后并不会必然调用,所以为了防止此类情况的出现,我们有两种手段。
1、使用完线程共享变量后,显式调用ThreadLocalMap.remove
方法清除线程共享变量;
2、JDK建议ThreadLocal
定义为private static
,这样ThreadLocal
的弱引用问题则不存在了。