本人进行android开发时间不长,写此博客主要是为了巩固所学知识。
Handler、Message、Loopler、MessageQueen
首先看一下我们平常使用Handler的一个最常见用法。
Handler handler =new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//这里进行一些UI操作等处理
}
new Thread(new Runnable() {
@Override
public void run() {
Message message = Message.obtain();
handler.sendMessage(message);
}
});
};
看一下handler的构造函数的源码
public Handler() {
this(null, 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();
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;
}
看到当mLooper == null时会抛一个“Can't create handler inside thread that has not called Looper.prepare()"这个异常,所以我们首先需要调用Looper.prepare()
public static void prepare() {
prepare(true);
}
//将looper保存到ThreadLocal中,这里可以把ThreadLocal理解为一个以当前线程为键的Map,所以一个线程中只会有一个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));
}
接下来我们看handler.sendMessage(message)这个方法,从字面意思就是将信息发送出去。一般sendMessage方法最终都会调用sendMessageAtTime(Message msg, long uptimeMillis)这个方法
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);
}
我们看到最终会执行enqueueMessage(queue, msg, uptimeMillis)这个方法
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
最终又会调用MessageQueen中的queue.enqueueMessage(msg, uptimeMillis)这个方法
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;
}
MessageQueen虽然名字是一个队列,但实质上他是一个单向链表,这个结构能快速进行插入和删除操作。从上面源码可以看出来,主要是按照�发送消息的时间顺序将msg插入到消息队列中。接下来我们就需要从消息队列中取出msg了。这时候就需要调用Looper.loop()方法。
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();
for (;;) {
//从消息队列中取出msg
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
Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
//将msg交由handler处理
msg.target.dispatchMessage(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();
}
}
可以看到Looper.loop()方法通过在一个死循环中调用Message msg = queue.next()将消息不断的从消息队列中取出来。queue.next()方法的作用就是从消息队列中取msg。现在msg已经取出来,下一步就是怎样将他传递给handler了对吧。所以在死循环中还有一个方法msg.target.dispatchMessage(msg) ,而msg.target就是handler,在handler的enqueueMessage()方法中传入的msg.target = this,接下来就看看handler的dispatchMessage()方法
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
如果我们采用无参的构造函数创建handler,msg.callback与mCallback均为空,所以我们会调用handleMessage(msg),这样文章开头的那个实例整个流程就走完了,handleMessage(msg)会在handler所在的线程中执行。
//当我们通过这种方式创建handler时,dispatchMessage中的mCallback就不为null
public Handler(Callback callback) {
this(callback, false);
}
//Callback是一个接口,里面正好也有我们需要的handleMessage(Message msg)
public interface Callback {
public boolean handleMessage(Message msg);
}
//当我们调用handler.post()方法执行异步任务时
public final boolean post(Runnable r)
{
return sendMessageDelayed(getPostMessage(r), 0);
}
//这个方法中我们看到给m.callback赋值了,就是我们传入的runnable接口
private static Message getPostMessage(Runnable r) {
Message m = Message.obtain();
m.callback = r;
return m;
}
//最后在handleCallback方法中我们执行了它的run方法
private static void handleCallback(Message message) {
message.callback.run();
}
总结
- 梳理整个执行过程
1.调用Looper.prepare()方法,这是创建handler所必须的。在主线程中由于ActivityThread已经创建过looper,所以在主线程中创建handler以前无需创建looper。
2.通过调用handler.sendMessage(message)方法最终会执行enqueueMessage(queue, msg, uptimeMillis),enqueueMessage又会调用MessageQueen的queue.enqueueMessage(msg, uptimeMillis),这样消息就会被添加到消息队列中。
3.调用Looper.loop()方法在死循环中执行Message msg = queue.next(),不断的将msg从消息队列中取出来,同时执行msg.target.dispatchMessage(msg),将消息传递给handler,由handler来处理,如我们调用的handleMessage就是处理消息的方式之一。 -
异步处理机制流程图
这里写图片描述 - 从其他线程访问 UI 线程的几种方式
Android 提供了几种途径来从其他线程访问 UI 线程。以下列出了几种有用的方法:
• Activity.runOnUiThread(Runnable)
• View.post(Runnable) 这里的view就是我们需要改变的ui控件
• View.postDelayed(Runnable, long)
• Handler.post(Runnable, long)
但是,随着操作日趋复杂,这类代码也会变得复杂且难以维护。 要通过工作线程处理更复杂的交互,可以考虑在工作线程中使用 Handler 处理来自 UI 线程的消息。当然,最好的解决方案或许是扩展 AsyncTask 类,此类简化了与 UI 进行交互所需执行的工作线程任务。
网友评论