美文网首页
技术:Handler消息机制浅谈

技术:Handler消息机制浅谈

作者: 玉圣 | 来源:发表于2017-06-02 10:50 被阅读30次

Message:
消息;其中包含了消息ID,消息对象以及处理的数据等,由MessageQueue统一列队,终由Handler处理

Handler:
处理者;负责Message发送消息及处理。Handler通过与Looper进行沟通,从而使用Handler时,需要实现handlerMessage(Message msg)方法来对特定的Message进行处理,例如更新UI等(主线程中才行)

MessageQueue:
消息队列;用来存放Handler发送过来的消息,并按照FIFO(先入先出队列)规则执行。当然,存放Message并非实际意义的保存,而是将Message以链表的方式串联起来的,等Looper的抽取。

Looper:
消息泵,不断从MessageQueue中抽取Message执行。因此,一个线程中的MessageQueue需要一个Looper进行管理。Looper是当前线程创建的时候产生的(UI Thread即主线程是系统帮忙创建的Looper,而如果在子线程中,需要手动在创建线程后立即创建Looper[调用Looper.prepare()方法])。也就是说,会在当前线程上绑定一个Looper对象。

Thread:
线程;负责调度消息循环,即消息循环的执行场所。

知识要点

一、说明

1、handler应该由处理消息的线程创建。
2、handler与创建它的线程相关联,而且也只与创建它的线程相关联。handler运行在创建它的线程中,所以,如果在handler中进行耗时的操作,会阻塞创建它的线程。

二、一些知识点

1、Android的线程分为有消息循环的线程和没有消息循环的线程,有消息循环的线程一般都会有一个Looper。主线程(UI线程)就是一个消息循环的线程。

2、获取looper:
Looper.myLooper(); //获得当前的Looper
Looper.getMainLooper() //获得UI线程的Lopper

3、Handle的初始化函数(构造函数),如果没有参数,那么他就默认使用的是当前的Looper,如果有Looper参数,就是用对应的线程的Looper。

4、如果一个线程中调用Looper.prepare(),那么系统就会自动的为该线程建立一个消息队列,然后调用 Looper.loop();之后就进入了消息循环,这个之后就可以发消息、取消息、和处理消息。

消息处理机制原理:

在创建Activity之前,当系统启动的时候,先加载ActivityThread这个类,在这个类中的main函数,调用了Looper.prepareMainLooper(); 方法进行初始化Looper对象;

然后创建了主线程的handler对象(Tips:加载ActivityThread的时候,其内部的Handler对象[静态的]还未创建);随后才创建了ActivityThread对象;最后调用了Looper.loop(); 方法,不断的进行轮询消息队列的消息。

也就是说,在ActivityThread和Activity创建之前(同样也是Handler创建之前,当然handler由于这两者初始化),就已经开启了Looper的loop()方法,不断的进行轮询消息。

需要注意的是,这个轮询的方法是阻塞式的,没有消息就一直等待(实际是等着MessageQueue的next()方法返回消息)。在应用一执行的时候,就已经开启了Looper,并初始化了Handler对象。此时,系统的某些组件或者其他的一些活动等发送了系统级别的消息,这个时候主线程中的Looper就可以进行轮询消息,并调用msg.target.dispatchMessage(msg) (msg.target即为handler)进行分发消息,并通过handler的handleMessage方法进行处理;所以会优于我们自己创建的handler中的消息而处理系统消息。

handler流程示意图

一、简单概述:

0、准备数据和对象:

①、如果在主线程中处理message(即创建handler对象),那么如上所述,系统的Looper已经准备好了(当然,MessageQueue也初始化了),且其轮询方法loop已经开启。【系统的Handler准备好了,是用于处理系统的消息】。【Tips:如果是子线程中创建handler,就需要显式的调用Looper的方法prepare()和loop(),初始化Looper和开启轮询器】

②、通过Message.obtain()准备消息数据(实际是从消息池中取出的消息)

③、创建Handler对象,在其构造函数中,获取到Looper对象、MessageQueue对象(从Looper中获取的),并将handler作为message的标签设置到msg.target上

1、发送消息:sendMessage() :通过Handler将消息发送给消息队列

2、给Message贴上handler的标签:在发送消息的时候,为handler发送的message贴上当前handler的标签

3、开启HandlerThread线程,执行run方法。

4、在HandlerThread类的run方法中开启轮询器进行轮询:调用Looper.loop()方法进行轮询消息队列的消息

【Tips:这两步需要再斟酌,个人认为这个类是自己手动创建的一个线程类,Looper的开启在上面已经详细说明了,这里是说自己手动创建线程(HandlerThread)的时候,才会在这个线程中进行Looper的轮询的】

5、在消息队列MessageQueue中enqueueMessage(Message msg, long when) 方法里,对消息进行入列,即依据传入的时间进行消息入列(排队)

6、轮询消息:与此同时,Looper在不断的轮询消息队列

7、在Looper.loop()方法中,获取到MessageQueue对象后,从中取出消息(Message msg = queue.next())

8、分发消息:从消息队列中取出消息后,调用msg.target.dispatchMessage(msg); 进行分发消息

9、将处理好的消息分发给指定的handler处理,即调用了handler的dispatchMessage(msg) 方法进行分发消息。

10、在创建handler时,复写的handleMessage方法中进行消息的处理

11、回收消息:在消息使用完毕后,在Looper.loop() 方法中调用msg.recycle() ,将消息进行回收,即将消息的所有字段恢复为初始状态。

二、详细解释:

1、准备Looper对象

两种情况初始化Looper对象:

1)在主线程中不需要显式的创建Looper对象,直接创建Handler对象即可;因为在主线程ActivityThread的main函数中已经自动调用了创建Looper的方法:Looper.prepareMainLooper(); ,并在最后调用了Looper.loop() 方法进行轮询。

2)如果在子线程中创建Handler对象,需要创建Looper对象,即调用显式的调用Looper.prepare()

初始化Looper的工作:

1)初始化Looper对象:通过调用Looper.prepare() 初始化Looper对象,在这个方法中,新创建了Looper对象;在创建Looper的同时,(在其构造函数中)也初始化了MessageQueue。

2)将Looper绑定到当前线程:在初始化中,调用sThreadLocal.set(new Looper(quitAllowed)) 方法,将其和ThreadLocal进行绑定

在ThreadLocal对象中的set方法,是将当前线程和Looper绑定到一起:首先获取到当前的线程,并获取线程内部类Values,通过Thread.Values的put方法,将当前线程和Looper对象进行绑定到一起。即将传入的Looper对象挂载到当前线程上。

Tips:在Looper对象中,可以通过getThread() 方法,获取到当前线程,即此Looper绑定的线程对象。

源代码:

Looper中:

public static void prepare() {
  prepare(true);
}  

private static void prepare(boolean quitAllowed) {
  if (sThreadLocal.get() != null) {
    throw new RuntimeException("Only one Looper may be created per thread");
  }
  sThreadLocal.set(new Looper(quitAllowed));
}  

private Looper(boolean quitAllowed) {
  mQueue = new MessageQueue(quitAllowed);
  mRun = true;
  mThread = Thread.currentThread();
}

ThreadLocal中:

public void set(T value) {
  Thread currentThread = Thread.currentThread();
  Values values = values(currentThread);
  if (values == null) {
    values = initializeValues(currentThread);
  }
  values.put(this, value);
}

2、创建消息Message:

消息的创建可以通过两种方式:

1)new Message()

2)Message.obtain()
当存在多个handler的时候,可以通过Message.obtain(Handler handler)创建消息,指定处理的handler对象

Tips:建议使用第二种方式更好一些。
原因:
因为通过第一种方式,每有一个新消息,都要进行new一个Message对象,这会创建出多个Message,很占内存。

而如果通过obtain的方法,是从消息池sPool中取出消息。每次调用obtain()方法的时候,先判断消息池是否有消息(if (sPool != null) ),没有则创建新消息对象,有则从消息池中取出消息,并将取出的消息从池中移除【具体看obtain()方法】

    public static Message obtain() {
        synchronized (sPoolSync) {
            if (sPool != null) {
                Message m = sPool;
                sPool = m.next;
                m.next = null;
                m.flags = 0; // clear in-use flag
                sPoolSize--;
                return m;
            }
        }
        return new Message();
    }

3、创建Handler对象

两种形式创建Handler对象:

1)创建无参构造函数的Handler对象:

2)创建指定Looper对象的Handler对象

最终都会调用相应的含有Callback和boolean类型的参数的构造函数
【这里的Callback是控制是否分发消息的,其中含有一个返回值为boolean的handleMessage(Message msg) 方法进行判断的;

boolean类型的是参数是判断是否进行异步处理,这个参数默认是系统处理的,我们无需关心】

在这个构造函数中,进行了一系列的初始化工作:
①、获取到当前线程中的Looper对象
②、通过Looper对象,获取到消息队列MessageQueue对象
③、获取Callback回调对象
④、获取异步处理的标记

源代码:
①、创建无参构造函数的Handler对象:

    public Handler() {  
        this(null, false);  
    }  
    public Handler(Callback callback, boolean async) {  
        if (FIND_POTENTIAL_LEAKS) {  
            final Class<? extends Handler> klass = getClass();  
            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && (klass.getModifiers() & Modifier.STATIC) == 0) {  
                Log.w(TAG, "The following Handler class should be static or leaks might occur: " +klass.getCanonicalName());  
            }  
        }  
        mLooper = Looper.myLooper();  
        if (mLooper == null) {  
            throw new RuntimeException("Can't create handler inside thread that has not called Looper.prepare()");  
        }  
        mQueue = mLooper.mQueue;  
        mCallback = callback;  
        mAsynchronous = async;  
    } 

②、创建指定Looper对象的Handler对象

    public Handler(Looper looper) {  
        this(looper, null, false);  
    }  

    public Handler(Looper looper, Callback callback, boolean async) {  
        mLooper = looper;  
        mQueue = looper.mQueue;  
        mCallback = callback;  
        mAsynchronous = async;  
    } 

4、Handler对象发送消息:

1)Handler发送消息给消息队列:

Handler对象通过调用sendMessage(Message msg) 方法,最终将消息发送给消息队列进行处理

这个方法(所有重载的sendMessage)最终调用的是enqueueMessage(MessageQueuequeue, Message msg, long uptimeMillis)

(1)先拿到消息队列:在调用到sendMessageAtTime(Messagemsg, long uptimeMillis) 方法的时候,获取到消息队列(在创建Handler对象时获取到的)

(2)当消息队列不为null的时候(为空直接返回false,告知调用者处理消息失败),再调用处理消息入列的方法:

enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis)

这个方法,做了三件事:

①、为消息打上标签:msg.target = this;:将当前的handler对象这个标签贴到传入的message对象上,为Message指定处理者
②、异步处理消息:msg.setAsynchronous(true); ,在asyn为true的时候设置
③、将消息传递给消息队列MessageQueue进行处理:queue.enqueueMessage(msg, uptimeMillis);

    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;  
        if (queue == null) {  
            RuntimeException e = new RuntimeException(  
                    this + " sendMessageAtTime() called with no mQueue");  
            Log.w("Looper", e.getMessage(), e);  
            return false;  
        }  
        return 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);  
    }

2)MessageQueue消息队列处理消息:

在其中的enqueueMessage(Messagemsg, long when) 方法中,工作如下:

在消息未被处理且handler对象不为null的时候,进行如下操作(同步代码块中执行)

①、将传入的处理消息的时间when(即为上面的uptimeMillis)赋值为当前消息的when属性。

②、将next()方法中处理好的消息赋值给新的消息引用:

Message p =mMessages;

在next()方法中:不断的从消息池中取出消息,赋值给mMessage,当没有消息发来的时候,Looper的loop()方法由于是阻塞式的,就一直等消息传进来

③、当传入的时间为0,且next()方法中取出的消息为null的时候,将传入的消息msg入列,排列在消息队列上,此时为消息是先进先出的
否则,进入到死循环中,不断的将消息入列,根据消息的时刻(when)来排列发送过来的消息,此时消息是按时间的先后进行排列在消息队列上的

    final boolean enqueueMessage(Message msg, long when) {  
        if (msg.isInUse()) {  
            throw new AndroidRuntimeException(msg + " This message is already in use.");  
        }  
        if (msg.target == null) {  
            throw new AndroidRuntimeException("Message must have a target.");  
        }  
        boolean needWake;  
        synchronized (this) {  
            if (mQuiting) {  
                RuntimeException e = new RuntimeException(msg.target + " sending message to a Handler on a dead thread");  
                Log.w("MessageQueue", e.getMessage(), e);  
                return false;  
            }  
  
            msg.when = when;  
            Message p = mMessages;  
            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 {  
                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;  
            }  
        }  
        if (needWake) {  
            nativeWake(mPtr);  
        }  
        return true;  
    }

5、轮询Message

1)开启loop轮询消息

当开启线程的时候,执行run方法,在HandlerThread类中,调用的run方法中将开启loop进行轮询消息队列:

在loop方法中,先拿到MessageQueue对象,然后死循环不断从队列中取出消息,当消息不为null的时候,通过handler分发消息:msg.target.dispatchMessage(msg)。消息分发完之后,调用msg.recycle()回收消息,

2)回收消息:

在Message的回收消息recycle()这个方法中:首先调用clearForRecycle()方法,将消息的所有字段都恢复到原始状态【如flags=0,what=0,obj=null,when=0等等】

然后在同步代码块中将消息放回到消息池sPool中,重新利用Message对象

源代码:
Looper.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;  
        Binder.clearCallingIdentity();  
        final long ident = Binder.clearCallingIdentity();  
        for (;;) {  
            Message msg = queue.next(); // might block  
            if (msg == null) {  
               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);  
            }  
            final long newIdent = Binder.clearCallingIdentity();  
            if (ident != newIdent) {  
                Log.wtf(TAG, “……”);  
            }  
            msg.recycle();  
        }  
    }

msg.recycle();:

    public void recycle() {  
        clearForRecycle();  
        synchronized (sPoolSync) {  
            if (sPoolSize < MAX_POOL_SIZE) {  
                next = sPool;  
                sPool = this;  
                sPoolSize++;  
            }  
        }  
    }
/*package*/ 
    void clearForRecycle() {  
        flags = 0;  
        what = 0;  
        arg1 = 0;  
        arg2 = 0;  
        obj = null;  
        replyTo = null;  
        when = 0;  
        target = null;  
        callback = null;  
        data = null;  
    }
  /** 
     * Subclasses must implement this to receive messages. 
     */  
    public void handleMessage(Message msg) {  
    }  
      
    /** 
     * Handle system messages here. 
     */  
    public void dispatchMessage(Message msg) {  
        if (msg.callback != null) {  
            handleCallback(msg);  
        } else {  
            if (mCallback != null) {  
                if (mCallback.handleMessage(msg)) {  
                    return;  
                }  
            }  
            handleMessage(msg);  
        }  
    }

相关文章

网友评论

      本文标题:技术:Handler消息机制浅谈

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