美文网首页
Handler源码分析

Handler源码分析

作者: on_i_on | 来源:发表于2019-09-28 10:27 被阅读0次

    Handler使用方法

    1、Handler.sendMessage()

    2、Handler.post()

    //自定义一个handler类
    class myHander extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mEtInput.setText(msg.obj.toString());
            i++;
            if (i == 5) {
                return;
            }
            Message message = new Message();
            message.what = i;
            message.obj = "test" + i;
            myHandler.sendMessageDelayed(message,1000);
        }
    }
    
         //创建自定义handler实例
         myHandler = new myHander();
         //thread类实现多线程显示
         new Thread() {
             @Override
             public void run() {
                 super.run();
                 try {
                     Thread.sleep(1000);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 //创建消息对象
                 Message msg = Message.obtain();
                 message.obj = "test";
                 //子线程通过handle发消息到消息队列中
                 myHandler.sendMessage(msg);
             }
         }.start();
    
        //或者通过post方法使用
        final Handler handler1 = new Handler();
        new Thread() {
            @Override
            public void run() {
                super.run();
                handler1.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mEtInput.setText("handler post");
                    }
                }, 12000);
            }
        }.start();
    

    handler使用主要涉及Handler、Lopper、MessageQueue。
    整体流程可以概述为:handler通过sendMessage()以及post()方法,将消息发送到消息队列中,post方法最终也是调用的sendMessage()方法,然后由Loop.loop(),方法遍历MessageQueue,通过queue.next(),方法去除msg,若有msg就通过msg.target获得由跟msg绑定的handler调用.dispatchMessage(msg)处理,若msg.callback != null就会由重写的run()方法处理,若msg.callback为null则由重写的handleMessage(Message msg)方法处理。
    下面一步步跟着源码分析handler的每一步:

     /**
       *Handler构造方法
       */
      public Handler() {
          this(null, false);
      }
      public Handler(Callback callback, boolean async) {
        ...
        //在构造方法里通过Looper.myLooper()获取mLooper 
        mLooper = Looper.myLooper();
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called Looper.prepare()");
        }
        //通过mLooper.mQueue获得mLooper 中的mQueue
        mQueue = mLooper.mQueue;
        mCallback = callback;
        mAsynchronous = async;
        //通过这次代码我们可以得知在handler的构造函数中,handler与lopper绑定同时等于跟looper中的messagequeue绑定,
        //一个线程有lopper的前提主线程与子线程不同,在app启动时主线程ActivityThread的main方法会调用Looper.prepareMainLooper()方法,
        //子线程则通过自己调用Looper.prepare()生成Looper对象。
       }
    

    上述我们可以知道每一个Looper中都有一个MessageQueue与其绑定。下面看一下如何实现的:

    /**
      *Looper.prepareMainLooper()方法
      *在ActivityThread的main()方法里面会调用Looper.prepareMainLooper()方法
      *Looper.prepareMainLooper()最终调用prepare(false)方法
      *同时创建主线程 开启消息的自动轮询
      */
    public static void main(String[] args) {
        ...
    
        Looper.prepareMainLooper();//这个方法最终调用的还是prepare(false);方法
        ActivityThread thread = new ActivityThread();//创建主线程
        thread.attach(false);
        ...
        // End of event ActivityThreadMain.
        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
        Looper.loop();//开启消息的自动轮询
    
        throw new RuntimeException("Main thread loop unexpectedly exited");
    }
    

    所以主线程的Looper.prepareMainLooper()实现与MessageQueue绑定,直接看Looper.prepare()就好。

    public static void prepare() {
        prepare(true);
    }
    
    private static void prepare(boolean quitAllowed) {
        //sThreadLocal.get()不为null会抛异常 说明prepare()方法只能调用一次
        //也就是说一个线程只有一个Looper实例
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper(quitAllowed));
    }
    
    private Looper(boolean quitAllowed) {
        //创建一个消息队列  这里便是Looper与MessageQueue绑定的地方
        mQueue = new MessageQueue(quitAllowed);
        mThread = Thread.currentThread();
    }
    

    主线程与子线程创建Handler与Lopper以及MessageQueue向绑定的方式大致相同,但是主线程可以自动调用Looper.loop()以实现循环遍历的的功能,子线程需要自行调用,下面看一下Looper.loop()的具体实现:

    /**
      *主线程消息循环是不允许退出,即无限循环
      *子线程消息循环允许退出,调用MessageQueue的quit()方法
      */
    public static void loop() {
        // 返回sThreadLocal存储的Looper实例
        final Looper me = myLooper();
        if (me == null) {
            throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
        }
        // 获取Looper实例中的消息队列对象(MessageQueue)
        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获得handler,把Message派发给对应的handler
                msg.target.dispatchMessage(msg);
                end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
            } finally {
                if (traceTag != 0) {
                    Trace.traceEnd(traceTag);
                }
            }
    
         ...
           // 回收释放资源
           msg.recycleUnchecked();
        }
    }
    

    下面对queue.next()以及msg.target.dispatchMessage(msg)两个方法进行分析:

    /**
      * queue.next()
      */
    Message next() {
        ...
    
        int nextPollTimeoutMillis = 0;
        for (;;) {
            if (nextPollTimeoutMillis != 0) {
                Binder.flushPendingCommands();
            }
            // nativePollOnce方法在Native层,当nextPollTimeoutMillis==-1时即消息队列中没有消息,消息队列出于等待状态
            nativePollOnce(ptr, nextPollTimeoutMillis);
    
            synchronized (this) {
                final long now = SystemClock.uptimeMillis();
                Message prevMsg = null;
                Message msg = mMessages;
    
                ...
                // 按照先进先出的顺序取出消息
                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 (DEBUG) Log.v(TAG, "Returning message: " + msg);
                        msg.markInUse();
                        return msg;
                    }
                } else {
                    // No more messages.
                    nextPollTimeoutMillis = -1;
                }
    
                ...
        }
    }
    
    /**
      * dispatchMessage(msg)
      */
    // msg.callback != null 证明是使用的post()方法发送消息  会回调Runnable对象里复写的run()方法
    public void dispatchMessage(Message msg) {
        if (msg.callback != null) {
            handleCallback(msg);
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            // msg.callback为空,证明使用的sendMessage(Message msg)发送消息,会回调复写的handleMessage(msg)
            handleMessage(msg);
        }
    }
    

    循环next遍历出消息之后,通过dispatchMessage(msg)进行分发,分发给handler处理,那handler是什么时候赋值给msg从而可以通过msg.target获得handler实例的呢?handler在最初始就是sendMessage(msg),然后msg才会进入消息队列,所以应该是在sendMessage(msg)方法里面,下面我们分析一下sendMessage(msg)方法:

    //一路下来 发现最后是调用到MessageQueue的enqueueMessage(Message msg, long when)方法
    public final boolean sendMessage(Message msg){
        return sendMessageDelayed(msg, 0);
    }
    
    public final boolean sendMessageDelayed(Message msg, long delayMillis){
        if (delayMillis < 0) {
            delayMillis = 0;
        }
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
    }
    
    public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
        MessageQueue queue = mQueue;
        ...
        return enqueueMessage(queue, msg, uptimeMillis);
    }
    
    private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        // 这时候还在Handler这个类里面所以,this代表Handler
        msg.target = this;
        ...
        return queue.enqueueMessage(msg, uptimeMillis);
    }
    // 这个方法就是MessageQueue里面的方法了
    boolean enqueueMessage(Message msg, long when) {
        ...
    
        synchronized (this) {
            ...
    
            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;
    }
    

    上文中提到过post方法

     // 这里会将Runnable对象封装成msg对象并且发送到消息队列中
     handler1.postDelayed(new Runnable() {
           @Override
            public void run() {
                mEtInput.setText("handler post");
            }
      }, 12000);
    
    // postDelayed调用sendMessageDelayed  这就与handler的sendMessage方法一样
    // sendMessageDelayed(Message msg, long delayMillis)   getPostMessage(r)应该获得一个msg的返回 
    public final boolean postDelayed(Runnable r, long delayMillis){
        return sendMessageDelayed(getPostMessage(r), delayMillis);
    }
    
    private static Message getPostMessage(Runnable r) {
        Message m = Message.obtain();
        // 这里给msg的callback赋值一个人Runnable对象  把Runnable 对象封装成了一个Message对象
        m.callback = r;
        return m;
    }
    
    最终调用sendMessageDelayed方法就与sendMessage()方法一样了,唯一的区别就在于Handler.post()方法不需要传入Message对象,
    而是sendMessageDelayed(getPostMessage(r), delayMillis)的getPostMessage(Runnable r)方法里将Runnable 对象封装成了一个Message对象,
    之后回调也就变成了复写Runnable对象的run()方法。
    

    本文主要学习https://www.jianshu.com/p/b4d745c7ff7a 文末再粘贴两站作者总结的图:

    Handler.sendMessage() Handler.Post()

    相关文章

      网友评论

          本文标题:Handler源码分析

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