Android MessageQueue和Loop分析

我们首先来看HandlerThread的创建过程,

{() {mTid = Process.myTid();Looper.prepare();synchronized (this) {mLooper = Looper.myLooper();notifyAll();}Process.setThreadPriority(mPriority);onLooperPrepared();Looper.loop();mTid = -1;}}

调用Looper.prepare()为当前现成准备一个Looper

() {prepare(true);}(boolean quitAllowed) {if (sThreadLocal.get() != null) {throw new RuntimeException(“Only one Looper may be created per thread”);}sThreadLocal.set(new Looper(quitAllowed));}

利用sThreadLocal来为保证每一个线程都有自己独一的Looper对象

最后在HandlerThread的run方法里面,进入到Looper.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 (;;) {Message msg = queue.next(); // might blockif (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 loggerPrinter logging = me.mLogging;if (logging != null) {logging.println(“>>>>> Dispatching to ” + msg.target + ” ” +msg.callback + “: ” + msg.what);}msg.target.dispatchMessage(msg);if (logging != null) {logging.println(“<<<<< Finished to ” + msg.target + ” ” + msg.callback);}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里面取出一个Message出来进行处理,并进行dispatch。 Binder.clearCallingIdentity()的作用我猜测是用来清除远程调用者的ID,因为远程IPC的线程可能不具有某些权限,清除ID后,当前现成就可以代远程进程进行一些权限操作。

我们来看MessageQueue的next方法:

Message next() {// Return here if the message loop has already quit and been disposed.// This can happen if the application tries to restart a looper after quit// which is not supported.final long ptr = mPtr;if (ptr == 0) {return null;}int pendingIdleHandlerCount = -1; // -1 only during first iterationint nextPollTimeoutMillis = 0;for (;;) {if (nextPollTimeoutMillis != 0) {Binder.flushPendingCommands();}nativePollOnce(ptr, nextPollTimeoutMillis);synchronized (this) {// Try to retrieve the next message. Return if found.final long now = SystemClock.uptimeMillis();Message prevMsg = null;Message msg = mMessages;if (msg != null && msg.target == null) {// Stalled by a barrier. Find the next asynchronous message in the queue.do {prevMsg = msg;msg = msg.next;} while (msg != null && !msg.isAsynchronous());}if (msg != null) {if (now < msg.when) {// Next message is not ready. Set a timeout to wake up when it is ready.nextPollTimeoutMillis = (int) Math.min(msg.when – now, Integer.MAX_VALUE);} else {// Got a message.mBlocked = false;if (prevMsg != null) {prevMsg.next = msg.next;} else {mMessages = msg.next;}msg.next = null;if (false) Log.v(“MessageQueue”, “Returning message: ” + msg);return msg;}} else {// No more messages.nextPollTimeoutMillis = -1;}// Process the quit message now that all pending messages have been handled.if (mQuitting) {dispose();return null;}// If first time idle, then get the number of idlers to run.// Idle handles only run if the queue is empty or if the first message// in the queue (possibly a barrier) is due to be handled in the future.if (pendingIdleHandlerCount < 0&& (mMessages == null || now < mMessages.when)) {pendingIdleHandlerCount = mIdleHandlers.size();}if (pendingIdleHandlerCount <= 0) {// No idle handlers to run. Loop and wait some more.mBlocked = true;continue;}if (mPendingIdleHandlers == null) {mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];}mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);}// Run the idle handlers.// We only ever reach this code block during the first iteration.for (int i = 0; i < pendingIdleHandlerCount; i++) {final IdleHandler idler = mPendingIdleHandlers[i];mPendingIdleHandlers[i] = null; // release the reference to the handlerboolean keep = false;try {keep = idler.queueIdle();} catch (Throwable t) {Log.wtf(“MessageQueue”, “IdleHandler threw exception”, t);}if (!keep) {synchronized (this) {mIdleHandlers.remove(idler);}}}// Reset the idle handler count to 0 so we do not run them again.pendingIdleHandlerCount = 0;// While calling an idle handler, a new message could have been delivered// so go back and look again for a pending message without waiting.nextPollTimeoutMillis = 0;}}所有欺骗中,自欺是最为严重的

Android MessageQueue和Loop分析

相关文章:

你感兴趣的文章:

标签云: