`Runnable`在哪里执行?

时间:2015-09-05 06:12:10

标签: android multithreading

我试图围绕Android中ThreadLooperHandler架构的概念。因此,根据我目前的理解,我们可以将一个线程与一个looper和handler相关联。 Looper有一个消息队列,它存储消息和runnable,然后由循环处理(我从这个site(Looper和MessageQueue部分)读取它也包含runnable)。所以我想通过检查中的loop函数的源代码来了解它如何处理队列中的runnable Looper类(link为此)。现在,当我看到我在这里粘贴的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 (;;) {
        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.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();
    }
}

我无法找到它在messagequeue中处理runnables的位置。看到代码似乎只在那里处理消息对象。

所以在gist中我的问题是&#34; looper何时何地处理消息队列中的可运行项?如果它自己没有处理通过它们的地方?&#34;。我对Android很新,对不起,如果我的问题看起来很愚蠢。我试图了解过去两天以来Handler Loopers的概念,但我有点卡住了。我试图搜索我的具体问题,但它将我链接到我正在阅读的原始链接?

0 个答案:

没有答案