美文网首页
Android线程通信流程源码分析

Android线程通信流程源码分析

作者: Danny_yy | 来源:发表于2019-12-24 22:47 被阅读0次

    Android的线程间通信核心元素

    1. Handler
    2. Looper
    3. Message
    4. MessageQueue

    1. Looper

    先分析这个是因为能够引出四者的关系。 在Looper中,维持一个Thread对象以及MessageQueue,通过Looper的构造函数我们可以知道:

    private Looper(boolean quitAllowed) { 
          mQueue = new MessageQueue(quitAllowed);//传入的参数代表这个Queue是否能够被退出 
          mThread = Thread.currentThread();
    }
    

    Looper在构造函数里干了两件事情:

    1. 将线程对象指向了创建Looper的线程
    2. 创建了一个新的MessageQueue

    分析完构造函数之后,接下来我们主要分析两个方法:

    1. looper.loop()
    2. looper.prepare()

    looper.loop()(在当前线程启动一个Message loop机制,此段代码将直接分析出Looper、Handler、Message、MessageQueue的关系)

     public static void loop() {
            final Looper me = myLooper();//获得当前线程绑定的Looper
            if (me == null) {
                throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
            }
            final MessageQueue queue = me.mQueue;//获得与Looper绑定的MessageQueue
    
            // 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();
            
            //进入死循环,不断地去取对象,分发对象到Handler中消费
            for (;;) {
                Message msg = queue.next(); // 不断的取下一个Message对象,在这里可能会造成堵塞。
                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);
                }
                
                //在这里,开始分发Message了
                //至于这个target是神马?什么时候被赋值的? 
                //我们一会分析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);
                }
                
                //当分发完Message之后,当然要标记将该Message标记为 *正在使用* 啦
                msg.recycleUnchecked();
            }
        }
    

    分析了上面的源代码,我们可以意识到,最重要的方法是:

    1. queue.next()
    2. msg.target.dispatchMessage(msg)
    3. msg.recycleUnchecked()

    其实Looper中最重要的部分都是由Message、MessageQueue组成的有木有!这段最重要的代码中涉及到了四个对象,他们与彼此的关系如下:

    1. MessageQueue:装食物的容器
    2. Message:被装的食物
    3. Handler(msg.target实际上就是Handler):食物的消费者
    4. Looper:负责分发食物的人

    looper.prepare()(在当前线程关联一个Looper对象)

     private static void prepare(boolean quitAllowed) {
            if (sThreadLocal.get() != null) {
                throw new RuntimeException("Only one Looper may be created per thread");
            }
            //在当前线程绑定一个Looper
            sThreadLocal.set(new Looper(quitAllowed));
        }
    

    以上代码只做了两件事情:

    1. 判断当前线程有木有Looper,如果有则抛出异常(在这里我们就可以知道,Android规定一个线程只能够拥有一个与自己关联的Looper)。
    2. 如果没有的话,那么就设置一个新的Looper到当前线程。

    2.Handler

    由于我们使用Handler的通常性的第一步是:

     Handler handler = new Handler(){
            //你们有没有很好奇这个方法是在哪里被回调的?
            //我也是!所以接下来会分析到哟!
            @Override
            public void handleMessage(Message msg) {
                //Handler your Message
            }
        };
    

    所以我们先来分析Handler的构造方法

    //空参数的构造方法与之对应,这里只给出主要的代码,具体大家可以到源码中查看
    public Handler(Callback callback, boolean async) {
            //打印内存泄露提醒log
            ....
            
            //获取与创建Handler线程绑定的Looper
            mLooper = Looper.myLooper();
            if (mLooper == null) {
                throw new RuntimeException(
                    "Can't create handler inside thread that has not called Looper.prepare()");
            }
            //获取与Looper绑定的MessageQueue
            //因为一个Looper就只有一个MessageQueue,也就是与当前线程绑定的MessageQueue
            mQueue = mLooper.mQueue;
            mCallback = callback;
            mAsynchronous = async;
            
        }
    

    带上问题:

    1. Looper.loop()死循环中的msg.target是什么时候被赋值的?
    2. handler.handleMessage(msg)在什么时候被回调的?

    A1:Looper.loop()死循环中的msg.target是什么时候被赋值的? 要分析这个问题,很自然的我们想到从发送消息开始,无论是handler.sendMessage(msg)还是handler.sendEmptyMessage(what),我们最终都可以追溯到以下方法

    
    public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
            //引用Handler中的MessageQueue
            //这个MessageQueue就是创建Looper时被创建的MessageQueue
            MessageQueue queue = mQueue;
            
            if (queue == null) {
                RuntimeException e = new RuntimeException(
                        this + " sendMessageAtTime() called with no mQueue");
                Log.w("Looper", e.getMessage(), e);
                return false;
            }
            //将新来的Message加入到MessageQueue中
            return enqueueMessage(queue, msg, uptimeMillis);
        }
    

    3. 我们接下来分析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);
        }
    

    加上以上分析,我们将之前分析结果串起来,就可以知道了某些东西: Looper.loop()不断地获取MessageQueue中的Message,然后调用与Message绑定的Handler对象的dispatchMessage方法,最后,我们看到了handleMessage就在dispatchMessage方法里被调用的。

    通过以上的分析,我们可以很清晰的知道Handler、Looper、Message、MessageQueue这四者的关系以及如何合作的了。

    总结: 当我们调用handler.sendMessage(msg)方法发送一个Message时,实际上这个Message是发送到与当前线程绑定的一个MessageQueue中,然后与当前线程绑定的Looper将会不断的从MessageQueue中取出新的Message,调用msg.target.dispathMessage(msg)方法将消息分发到与Message绑定的handler.handleMessage()方法中。

    一个Thread对应多个Handler 一个Thread对应一个Looper和MessageQueue,Handler与Thread共享Looper和MessageQueue。 Message只是消息的载体,将会被发送到与线程绑定的唯一的MessageQueue中,并且被与线程绑定的唯一的Looper分发,被与其自身绑定的Handler消费。

    相关文章

      网友评论

          本文标题:Android线程通信流程源码分析

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