美文网首页
Android 源码-循环处理机制的实现

Android 源码-循环处理机制的实现

作者: 考特林 | 来源:发表于2018-11-23 12:24 被阅读0次

Volley

线程中使用while(true)实现循环,处理队列中的request

NetworkDispatcher.class

@Override
    public void run() {
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
        while (true) {
            try {
                processRequest();
            } catch (InterruptedException e) {
                // We may have been interrupted because it was time to quit.
                if (mQuit) {
                    Thread.currentThread().interrupt();
                    return;
                }
                VolleyLog.e(
                        "Ignoring spurious interrupt of NetworkDispatcher thread; "
                                + "use quit() to terminate it");
            }
        }
    }

Looper

线程中使用for (;;)实现循环,处理队列中的Message

Looper.class

/**
     * Run the message queue in this thread. Be sure to call
     * {@link #quit()} to end the loop.
     */
    public static void loop() {
        ...
        final MessageQueue queue = me.mQueue;
        ...  
        for (;;) {
            Message msg = queue.next(); // might block
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }
            ...
            try {
                msg.target.dispatchMessage(msg);
            } finally {
                if (traceTag != 0) {
                    Trace.traceEnd(traceTag);
                }
            }
            ...
            msg.recycleUnchecked();
        }
    }

相关文章

网友评论

      本文标题:Android 源码-循环处理机制的实现

      本文链接:https://www.haomeiwen.com/subject/zyxjqqtx.html