消息机制解析

先上uml图

handler.png
  1. 消息推送机制的主要角色: Handler 发送和处理消息的类.Message发送的消息对象.MessageQuene 消息队列,虽然叫消息队列,其实是单链表的数据结构,用来存储消息的类.Lopper,消息循环,不断的把消息从消息队列中拿出,交给Handler去处理.
  2. Handler运行在主线程中(UI线程中),它与子线程可以通过Message对象来传递数据,可以分发Message对象和Runnable对象到主线程中.
  3. 先看ThreadLocal类.ThreadLocal是一个线程内部的数据存储类.他可以在指定的线程中存数数据,然后在指定的线程得到改数据.当某些数据是以线程为作用域并且不同线程有不同的数据副本时,可以考虑使用ThreadLocal.

1. ThreadLocal 的set.get

public T get() {
    Thread t = Thread.currentThread(); //得到当前线程
    ThreadLocalMap map = getMap(t);
   //ThreadLocalMap 是ThreadLocal的内部类.而每个Thread内部包含一个 ThreadLocal.ThreadLocalMap的变量,名字
为threadLocals,getMap(t)返回线程的threadLocals.
    if (map != null) {
        ThreadLocalMap.Entry e = map.getEntry(this);
    //map中有一个Entry[] 数组, Entry 就是当前线程保存的数据bean,
    table 通过ThreadLocal的hash值经过运算得到 当前线程的Entry 在table中的索引,
   然后通过索引找到Entry 返回给外边.
        if (e != null)
            return (T)e.value;
    }
    return setInitialValue();
}

public void set(T value) {
    Thread t = Thread.currentThread(); //同样是找到该线程,
    ThreadLocalMap map = getMap(t); //得到线程的 ThreadLocalMap
    if (map != null)
        map.set(this, value);  //把要保存在线程中的值保存在map中的 table中.若table对这个ThreadLocal原来
有数据就更新数据,没有数据就添加数据
    else
        createMap(t, value); //新建一个ThreadLocalMap,并赋值给Tread的threadLocals,同时新建table数组,把value保存起来.
}
可以看到,最终Threal保存数据,靠的是操作ThradLocalMap中的table数组.

2. MessageQuene 的enqueueMessage和 next

首先看一下Message 的主要参数
 Handler target.//发送Message的那个handler.
 Runnable callback . //handler.post(Runnable ) 方式发送消息时传递的参数.
 Message next; //当前msg执行完成后接下来要执行的msg的引用
 long when;  //消息需设置的延时时间,在入栈时会加入当前时间转化为消息需要执行的绝对时间.

 MessageQuene 的消息入队操作
 boolean enqueueMessage(Message msg, long when) {
    if (msg.target == null) {
        throw new IllegalArgumentException("Message must have a target.");
    }
    if (msg.isInUse()) {  //msg是否正在被使用.
        throw new IllegalStateException(msg + " This message is already in use.");
    }

    synchronized (this) {
        if (mQuitting) { //表示线程需要退出了.消息加入失败
            IllegalStateException e = new IllegalStateException(
                    msg.target + " sending message to a Handler on a dead thread");
            Log.w("MessageQueue", e.getMessage(), e);
            msg.recycle();
            return false;
        }

        msg.markInUse(); //把msg标记为使用状态,同上文照应.
        msg.when = when; //when表示消息需要执行的绝对时间
        Message p = mMessages; // mMessages表示队列中最先要执行的msg
        boolean needWake;
        //新增的msg比mMessage还要先执行,所以放在链表的头部
        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 { //否则的话.就从mMessge 向后遍历,比较Message的when,将新增的msg插入到合适的位置
            // 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  //把msg插入到p前边
            prev.next = msg;
        }

        // We can assume mPtr != 0 because mQuitting is false.
        if (needWake) { 
            nativeWake(mPtr);
        }
    }
    return true;
}  
接下来看next 
   //返回一个msg出去.有可能是阻塞式的,因为他有一个循环一直返回msg
  Message next() {
      // Return here if the message loop has already quit and been disposed.
      // This can happen if the application tries to restart a looper after quit
      // which is not supported.
      final long ptr = mPtr;
      if (ptr == 0) {  //如果looper退出,就返回null
          return null;
      }

    int pendingIdleHandlerCount = -1; // -1 only during first iteration
    int nextPollTimeoutMillis = 0;
    for (;;) {  //开始执行消息循环
        if (nextPollTimeoutMillis != 0) {
            Binder.flushPendingCommands();
        }

        nativePollOnce(ptr, nextPollTimeoutMillis); 

        synchronized (this) {
            // Try to retrieve the next message.  Return if found.
            final long now = SystemClock.uptimeMillis();//得到当前时间
            Message prevMsg = null;  //相当于头结点
            Message msg = mMessages; //即将要执行的msg //相当于尾节点
            if (msg != null && msg.target == null) { //先排除掉同步的msg
                // Stalled by a barrier.  Find the next asynchronous message in the queue.
                do {  
                    prevMsg = msg;
                    msg = msg.next;
                } while (msg != null && !msg.isAsynchronous());
            }
            if (msg != null) { //到这里时.剩下了异步的msg
                if (now < msg.when) { //标识msg的执行时间还没到. 就生成一个睡眠时间 nextPollTimeoutMillis 
                    // 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; //msg 就是即将要处理的消息把他从链表中移除
                    if (false) Log.v("MessageQueue", "Returning message: " + msg);
                    return msg;
                }
            } else {
                // No more messages.
                nextPollTimeoutMillis = -1;
            }

            // Process the quit message now that all pending messages have been handled.
            if (mQuitting) { //如果要退出的话.就结束msg队列的遍历 在 quite() 方法中赋值true
                dispose();
                return null;
            }
              
              //这里是对闲置handler的处理. 忽略------------------
   
    }
}
最后就清楚了,MessageQuene的next方法会阻塞,等到有msg到了且msg的执行时间到达,就把msg从链表中移除.返回出去.只有在返回了msg消息或者MessageQuene.quit被调用的时候才会退出循环.

3.Looper 循环器.不断从MessageQuene中取出消息,交给Handler处理.

 1.构造函数和常量.
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); //在不同线程保存Looper的ThreadLocal.
 private static Looper sMainLooper;
 private Looper(boolean quitAllowed) {
    mQueue = new MessageQueue(quitAllowed); //直接创建一个MessageQueue 形参表示是否运行退出
    mThread = Thread.currentThread();  //拿到当前线程
}

2. //Looper 通过prepare 方法在线程中创建Lopper,
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));
}

3. UI线程也是有Looper的,Looper 为主线程提供了 如下两个方法,本质也是通过prepare来创建Looper
public static void prepareMainLooper() {
    prepare(false);
    synchronized (Looper.class) {
        if (sMainLooper != null) {
            throw new IllegalStateException("The main Looper has already been prepared.");
        }
        sMainLooper = myLooper();
    }
}

public static Looper getMainLooper() {
    synchronized (Looper.class) {
        return sMainLooper;
    }
}
//拿到对应线程中的Looper方法
public static Looper myLooper() {
      return sThreadLocal.get();
}

4.Looper 最主要的方法是loop(),loop会不断从MessageQuene中拿消息,然后交给handler处理
public static void loop() {
    final Looper me = myLooper(); //得到当前线程的Looper对象
    if (me == null) { //需要在线程中先调用prepare 生成Looper 才能开启循环
        throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
    }
    final MessageQueue queue = me.mQueue; //拿到对应的消息队列
      //忽略--------

    for (;;) { //阻塞的循环
        Message msg = queue.next(); //  当MessageQueue的quite被调用,nxet就会返回null,然后looper也就终止循环了,此方法会阻塞
        if (msg == null) { 
            // No message indicates that the message queue is quitting.
            return;
        }
           //忽略--------------
         //target 就是当初发送msg的handler,到这里消息就交给了handler来处理
        msg.target.dispatchMessage(msg);

        //忽略-------
    
        msg.recycleUnchecked(); //消息回收
    }
}

5. looper的quite会调用MessageQueue的quite.然后在loop中遍历的msg就是null,然后Looper的循环就结束了.
 public void quit() {  
    mQueue.quit(false);
}

 public void quitSafely() { //这两个方法的区别是,上边会直接结束loop()方法的循环,下边会等loop()将MessageQueue中的消息都处理完在结束loop()循环.
    mQueue.quit(true);
}
可见.Looper就是一个控制器,不断把MessageQueue中的msg取出交个msg对应的Handler去处理.

4. Handler 兼顾发送消息和处理消息.

1. 先看成员变量
final MessageQueue mQueue;  //handler
final Looper mLooper;
final Callback mCallback; 内部接口,只有一个 handleMessage(Message msg); 方法
2. 看构造方法,最后走向两个构造方法,
public Handler(Callback callback, boolean async) {
     //忽略
    mLooper = Looper.myLooper();
    mQueue = mLooper.mQueue;
    mCallback = callback;
    mAsynchronous = async;
}
public Handler(Looper looper, Callback callback, boolean async) {
    mLooper = looper;
    mQueue = looper.mQueue;
    mCallback = callback;
    mAsynchronous = async;
}
//所以说,Handler所在的线程一定要有Looper,不然就会出错.
3. Handler的sendMessage 和post 最终都会走入下边的方法
 private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
    msg.target = this;
    if (mAsynchronous) {
        msg.setAsynchronous(true);
    }
    return queue.enqueueMessage(msg, uptimeMillis); //把消息和消息需要执行的之间加入到MessageQueue的消息链表中
}
//同样 移除消息也是调用MessageQuene的移除方法
public final void removeMessages(int what) {
    mQueue.removeMessages(this, what, null);
}
4.所以,handler把msg发出后,就到了MessageQueue中了,然后Looper就阻塞式的取消息,当消息的执行时间到达,Looper就调用hander的dispatchMessage 来处理消息
   public void dispatchMessage(Message msg) {
    if (msg.callback != null) { //callback就是  Handler.post(Runnable) 的那个对象
        handleCallback(msg);  //调用runnable的 run方法  
    } else {
        if (mCallback != null) {
            if (mCallback.handleMessage(msg)) { 这是handler内部类的执行方法,直接实现Handler时,就需要实现这种方法. new Hander(){ handlerMessage(); }
                return;
            }
        }
        handleMessage(msg); //常规的执行方法 这是我们写内部类继承handler时的写法
          //public  class A extends Handler{  handlerMessage() }
    }
}
其实就是三种不同的 实现方式,handler会根据实现方式调用对应的方法.

5.主线程中,系统自动为我们的Handler创建了Looper, 主线程是ActivityThread,入口方法是main,
public static void main(String[] args) {
    //忽略 ---

    Process.setArgV0("<pre-initialized>");
    Looper.prepareMainLooper();

    ActivityThread thread = new ActivityThread();
    thread.attach(false);
    if (sMainThreadHandler == null) {
        sMainThreadHandler = thread.getHandler(); getHander 返回的是 mH
    }
    Looper.loop();
    throw new RuntimeException("Main thread loop unexpectedly exited");
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容