Handler是Android的消息传递机制上层(应用层)的实现,通过它可以把一个任务切换到Handler所在的线程中执行,只不过我们经常使用Hnadler来更新UI。
首先介绍一下消息机制中包括的四个要素
-
Message(消息):需要被传递的消息对象,其中包含了消息ID,消息相应方以及回调方法等,由MessageQueue统一列队,最终由Handler处理。
-
MessageQueue(消息队列):用来存放Handler发送过来的消息(Message),虽然叫队列,但是内部是通过单链表的数据结构来维护消息列表,等待Looper的抽取。
-
Handler(处理者):负责Message的发送及处理。通过 Handler.sendMessage() 向消息池发送各种消息事件;通过 Handler.handleMessage() 处理相应的消息事件。
-
Looper(消息泵):通过Looper.loop()不断地从MessageQueue中抽取Message,按分发机制将消息分发给目标处理者(Handler)。
下面是一张Handler的流程图:
他们的数量关系是:一个Thread只能有一个Looper,可以有多个Handler,而一个Looper中又维护了一个MessageQueue队列。
这里就要提一下ThreadLocal,是一个线程内部的数据存储类,通过它可以在指定的线程中存储数据。对于Handler来说就是获取当前线程的Looper。
下面就从整个流程上来分析一下:
1、发送消息
// 直接创建可能会导致内存泄露
public Handler() {
this(null, false);
}
// 一般创建的时候会选择这个构造方法
public Handler(Callback callback) {
this(callback, false);
}
// 上面两个构造方法的最终实现
public Handler(Callback callback, boolean async) {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && (klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " + klass.getCanonicalName());
}
}
mLooper = Looper.myLooper(); // 从当前线程的ThreadLocal中获取Looper对象
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread " + Thread.currentThread()
+ " that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
这里说一下为什么直接new Handler()会存在内存泄露,我们一般会使用下面的方式创建。这就相当于在Activity中声明了一个内部类,内部类默认会持有外部类的引用,也就是Handler持有Activity的引用而Message又持有对Handler的引用,如果在退出Activity的时候,有一个消息还没有处理的话,那么这时候Activity是没法回收的,就会造成内存泄露
// 每次创建Android Studio都会提出会出现内存泄露的警告
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
发消息包括sendMessage和postMessage两种,但最终都是调用到了sendMessageAtTime方法
public final boolean sendMessage(Message msg) {
return sendMessageDelayed(msg, 0);
}
public final boolean sendEmptyMessage(int what) {
return sendEmptyMessageDelayed(what, 0);
}
public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
Message msg = Message.obtain(); // 从消息池中获取一个消息
msg.what = what;
return sendMessageDelayed(msg, delayMillis);
}
public final boolean sendMessageDelayed(Message msg, long delayMillis) {
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis); // 系统时间加上delay的时间就是执行的时间
}
public final boolean sendEmptyMessageAtTime(int what, long uptimeMillis) {
Message msg = Message.obtain();
msg.what = what;
return sendMessageAtTime(msg, uptimeMillis);
}
// sendMessage系列方法最终调用的地方
public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
MessageQueue queue = mQueue;
if (queue == null) {
RuntimeException e = new RuntimeException(
this + " sendMessageAtTime() called with no mQueue");
Log.w("Looper", e.getMessage(), e);
return false;
}
return enqueueMessage(queue, msg, uptimeMillis);
}
// 最后由MessageQueue.enqueueMessage
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
// 其余的postDelay等等方法都和上面的send差不多,最终也是调用了sendMessage,只不过差别在于传入了一个Runnable对象
public final boolean post(Runnable r) {
return sendMessageDelayed(getPostMessage(r), 0);
}
private static Message getPostMessage(Runnable r) {
Message m = Message.obtain();
m.callback = r; // 回调对象是Runnable,直接在Runnbale中的run方法中写上要执行的方法
return m;
}
post()直接在runnable里面完成更新操作(大家可能会有疑问,在Runnable的run方法里执行那不就相当于新建了一个线程执行么,其实不然,因为在下面接收消息的时候调用的是线程的run方法而不是start方法),这个任务会被添加到handler所在线程的消息队列中,即主线程的消息队列中
boolean enqueueMessage(Message msg, long when) {
if (msg.target == null) {
throw new IllegalArgumentException("Message must have a target.");
}
if (msg.isInUse()) {
throw new IllegalStateException(msg + " This message is already in use.");
}
synchronized (this) {
if (mQuitting) {
IllegalStateException e = new IllegalStateException(
msg.target + " sending message to a Handler on a dead thread");
Log.w(TAG, e.getMessage(), e);
msg.recycle();
return false;
}
msg.markInUse();
msg.when = when;
Message p = mMessages;
boolean needWake;
if (p == null || when == 0 || when < p.when) {
// New head, wake up the event queue if blocked.
msg.next = p;
mMessages = msg;
needWake = mBlocked;
} else {
// Inserted within the middle of the queue. Usually we don't have to wake
// up the event queue unless there is a barrier at the head of the queue
// and the message is the earliest asynchronous message in the queue.
needWake = mBlocked && p.target == null && msg.isAsynchronous();
Message prev;
for (;;) { // 单链表的插入操作
prev = p;
p = p.next;
if (p == null || when < p.when) {
break;
}
if (needWake && p.isAsynchronous()) {
needWake = false;
}
}
msg.next = p; // invariant: p == prev.next
prev.next = msg;
}
// We can assume mPtr != 0 because mQuitting is false.
if (needWake) {
nativeWake(mPtr);
}
}
return true;
}
2、接收消息
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg); // post调用这个方法
} else {
if (mCallback != null) { // 这个mCallback就是我们new Handler构造函数中传入的callback
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
// 直接调用的Runnable的run方法,并不是strat(),所以仅仅是当做一个有run()的普通类使用而已,并不是开启了一个新的线程
private static void handleCallback(Message message) {
message.callback.run();
}
3、轮询消息
Looper.prepare()在每个线程只允许执行一次,该方法会创建Looper对象,Looper的构造方法中会创建一个MessageQueue对象,再将Looper对象保存到ThreadLocal。
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));
}
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
消息的轮询主要是靠Looper.loop()来实现的,它会不停地从MessageQueue中查看是否有新的消息,如果有的话就立即处理,否则就一直阻塞在那里。
public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
// Allow overriding a threshold with a system prop. e.g.
// adb shell 'setprop log.looper.1000.main.slow 1 && stop && start'
final int thresholdOverride =
SystemProperties.getInt("log.looper."
+ Process.myUid() + "."
+ Thread.currentThread().getName()
+ ".slow", 0);
boolean slowDeliveryDetected = false;
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
// This must be in a local variable, in case a UI event sets the logger
final Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
// Make sure the observer won't change while processing a transaction.
final Observer observer = sObserver;
final long traceTag = me.mTraceTag;
long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;
long slowDeliveryThresholdMs = me.mSlowDeliveryThresholdMs;
if (thresholdOverride > 0) {
slowDispatchThresholdMs = thresholdOverride;
slowDeliveryThresholdMs = thresholdOverride;
}
final boolean logSlowDelivery = (slowDeliveryThresholdMs > 0) && (msg.when > 0);
final boolean logSlowDispatch = (slowDispatchThresholdMs > 0);
final boolean needStartTime = logSlowDelivery || logSlowDispatch;
final boolean needEndTime = logSlowDispatch;
if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
}
final long dispatchStart = needStartTime ? SystemClock.uptimeMillis() : 0;
final long dispatchEnd;
Object token = null;
if (observer != null) {
token = observer.messageDispatchStarting();
}
long origWorkSource = ThreadLocalWorkSource.setUid(msg.workSourceUid);
try {
msg.target.dispatchMessage(msg);
if (observer != null) {
observer.messageDispatched(token, msg);
}
dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
} catch (Exception exception) {
if (observer != null) {
observer.dispatchingThrewException(token, msg, exception);
}
throw exception;
} finally {
ThreadLocalWorkSource.restore(origWorkSource);
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
if (logSlowDelivery) {
if (slowDeliveryDetected) {
if ((dispatchStart - msg.when) <= 10) {
Slog.w(TAG, "Drained");
slowDeliveryDetected = false;
}
} else {
if (showSlowLog(slowDeliveryThresholdMs, msg.when, dispatchStart, "delivery",
msg)) {
// Once we write a slow delivery log, suppress until the queue drains.
slowDeliveryDetected = true;
}
}
}
if (logSlowDispatch) {
showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", msg);
}
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
}
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
}
msg.recycleUnchecked();
}
}
上面代码最主要的就是一个死循环,当MessageQueue的next方法返回null时跳出循环。那么一直在主线程循环的话会不会造成系统卡死呢,当然不会,因为loop就是把消息发送到该去的地方,具体执行的地方不在这里,而且也不会消耗cpu的资源(具体可以了解一下Linux的pipe/epoll机制)
最后是quit和quitSafely,顾名思义,两个方法的区别在与是否是安全地退出Looper,quit是直接退出,而quitSafely会把消息队列中的消息处理完毕之后才退出
public void quit() {
mQueue.quit(false);
}
public void quitSafely() {
mQueue.quit(true);
}
void quit(boolean safe) {
if (!mQuitAllowed) {
throw new IllegalStateException("Main thread not allowed to quit.");
}
synchronized (this) {
if (mQuitting) {
return;
}
mQuitting = true;
if (safe) {
removeAllFutureMessagesLocked(); // 移除尚未触发的所有消息
} else {
removeAllMessagesLocked(); // 移除所有消息
}
// We can assume mPtr != 0 because mQuitting was previously false.
nativeWake(mPtr);
}
}
另外拓展一下,感觉IPC调用中的Messager和这个实现原理差不多
使用方法
private MyHandler myHandler = new MyHandler(this);
// ......
/**
*1. 声明静态内部类(静态的内部类不会持有外部类的引用)
*2. 声明弱引用(GC在回收时会忽略掉弱引用,直接回收)
*/
private static class MyHandler extends Handler {
WeakReference<Activity> mActivityReference;
MyHandler(Activity activity) {
//弱引用
mActivityReference= new WeakReference<Activity>(activity);
}
@Override
public void handleMessage(Message msg) {
Activity activity = mActivityReference.get();
}
}