消息循环:
总结
- 当一个Android应用启动时,框架会分配到一个Looper实例给应用的主线程。这个Looper的主要工作就是处理一个接着一个的消息对象。在Android中,所有Android框架的事件(比如Activity的生命周期方法的调用和按钮的点击等)都是放到消息中,然后加入到Looper要处理的消息队列中,由Looper依次处理。主线程的Looper的生命周期和应用的一样长。
- 当在主线程中初始化一个Handler时,它就会关联到Looper的消息队列。发送到消息队列的消息本身就持有Handler的引用,只有这样Looper在处理这个条消息的时候才能调用Handler#handleMessage(Message)处理消息。
public Handler(Callback callback, boolean async) {
...
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
在Handler发送消息的时候,会把消息插入到Handler初始化关联的Looper的消息队列中,并且msg消息本身就持有了Handler的引用。
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
在Looper初始化的时候,Looper会初始化对应Looper的MessageQueue.并把Looper放入ThreadLocal中
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));
}
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
- Android没有全局的消息队列,消息队列是和某个线程相关联在一起的。每个线程最多有一个消息队列,消息的取出和处理,也在这个线程本身中完成。每个线程在创建自己的消息队列之前都应该调用Looper.prepare()
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));
}
那么如何获取到当前线程的Looper,答案就在 ThreadLocal
问题
- Handler是属于哪个类的?
Handler属于Activity,创建任何一个Handler都属于重写了Activity中的Handler。
- Handler、Looper、MessageQueue何时建立的相互关系?
在Handler的构造中,默认完成了对当前线程Looper的绑定.
-
主线程的Looper和MessageQueue是何时创建的?
在Android中,ActivityThread的main方法是程序的入口,主线程的Looper和MessageQueue就是在此时创建的。
在同一线程中,Looper和MessageQueue是怎样的数量对应关系,与Handler又是怎样的数量对应关系?
MessageQueue 中消息为空,线程阻塞挂起等待,为什么不会造成ANR?
有关Handler的内存泄漏是怎么一回事?Handler可能造成内存泄漏(四)
在Java中,非静态的内部类或者匿名类会隐式的持有其外部类的引用,而静态的内部类则不会。
Handler定义的模板
public class SampleActivity extends Activity {
/**
* Instances of static inner classes do not hold an implicit
* reference to their outer class.
*/
private static class MyHandler extends Handler {
private final WeakReference<SampleActivity> mOutter;
public MyHandler(SampleActivity activity) {
mOutter= new WeakReference<SampleActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
SampleActivity activity = mOutter.get();
if (activity != null) {
// ...
}
}
}
private final MyHandler mHandler = new MyHandler(this);
/**
* Instances of anonymous classes do not hold an implicit
* reference to their outer class when they are "static".
*/
private static final Runnable sRunnable = new Runnable() {
@Override
public void run() { /* ... */ }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Post a message and delay its execution for 10 minutes.
mHandler.postDelayed(sRunnable, 1000 * 60 * 10);
// Go back to the previous Activity.
finish();
}
}
ThreadLocal的工作原理
示例引入
final ThreadLocal<Boolean> mBooleanThreadLocal = new ThreadLocal<>();
mBooleanThreadLocal.set(true);
Log.d(TAG,"[main]mBooleanThreadLocal="+mBooleanThreadLocal.get()); //true
new Thread("Thread#1"){
@Override
public void run(){
mBooleanThreadLocal.set(false);
Log.d(TAG,"[Thread#1]mBooleanThreadLocal="+mBooleanThreadLocal.get()); //false
}
}.start();
new Thread("Thread#2"){
@Override
public void run(){
Log.d(TAG,"[Thread#2]mBooleanThreadLocal="+mBooleanThreadLocal.get()); //null
}
}.start();
结果
也就是说,ThreadLocal保存的数据仅仅与当前的线程有关。其用于线程间无影响的修改数据。下面分析其原理
Looper 的prepare会向ThreeadLocal 中插入当前的线程的Looper对象
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));
}
看看ThreadLocal的set方法
public void set(T value) {
Thread currentThread = Thread.currentThread();
Values values = values(currentThread);
if (values == null) {
values = initializeValues(currentThread);
}
values.put(this, value);
}
首先通过values方法获取到当前线程的ThreadLocal数据。Thread类中有个专门用来存储线程的 ThreadLocal 数据:ThreadLocal.Values localValues. 如果localValues值为null,则进行初始化。在localValues内部有一个数组:private Object[] table,ThreadLocal 的值就存在这个数组中.
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的值在 table 数组中的存储位置总之那个是 ThreadLocal 的 reference 字段所标识的对象的下一个位置。
再来看 ThreadLocal 的get方法
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);
}
可以得出 ThreadLocal 的 set 和 get 方法操作的都是当前线程的 localValues 对象的 Table 数组,因此在不同线程中访问同一个 ThreadLocal 的 set 和 get方法,他们对 ThreadLocal 所做的读/写仅限于各自的线程的内部。