Android的消息机制

《Android开发艺术探索》第10章的笔记~

1、简介

  • Android规定访问UI只能在主线程,否则程序会抛出异常。ViewRootImpl的checkThread对UI的操作的线程进行了验证,同时又规定了耗时操作必须在子线程完成,否则会导致程序无法响应即ANR。因此系统提供了Handler进行对UI的操作。
  • Handler创建时会采用当前线程的Looper构建消息循环系统,在无Looper的线程,则需要为当前线程创建Looper。
  • Handler创建后,通过post或send方法将Runnable放到Handler内部的Looper中去处理。当send方法被调用时,他会调用MessageQueue的enqueueMessage将消息放入消息队列,然后循环并处理新消息。

2、消息机制分析

2.1、ThreadLocal工作原理

ThreadLocal是一个线程内部的数据存储类,在指定线程中存储数据,同时存储的数据只能从指定线程中访问,从其他线程中无法访问。对于Handler来说,需要获取当前线程的Looper,这个时候在不同的线程则需要不同的Looper,使用ThreadLocal则可以实现Looper在线程中的存取。

实例

  • 定义一个Boolean类型的ThreadLocal
private ThreadLocal<Boolean> mBooleanThreadLocal = new ThreadLocal<Boolean>();

mBooleanThreadLocal.set(true);

new Thread("Thread1"){
    @Override
    public void run(){
        mBooleanThreadLocal.set(false);
        mBooleanThreadLocal.get();
    };
}.start();

new Thread("Thread2"){
    @Override
    public void run(){
        mBooleanThreadLocal.get();
    };
}.start();

在主线程设置mBooleanThreadLocal的值为true,在子线程1中设置为false,在子线程2中不设置,后通过get方法获取,在主线程中为true,子线程1为false,子线程2为null。
即在不同线程访问同一个ThreradLocal对象,会得到不同的结果。在不同的线程访问同一个ThreadLocal,ThreadLocsl会从各自的线程取出一个数组,然后根据当前的ThreadLocal的索引去数组中查找对应的value值。不同的线程数组是不同的。

  • ThreadLocal的set方法
public void set(T value){
    Thread currentThread=Thread.currentThread();
    Values values=values(currentThread);
    if(values==null){
        values=initializeValues(cuttentThread);
    }
    values.put(this,value);
}

在Thread内部有一个成员(ThreadLocal.Values localValues)专门用于储存线程的ThreadLocal数据,因此在set方法中,通过values方法获取当前线程的ThreadLocal数据时,如果localValue的值为null,则需要初始化,初始化完成后在进行储存。

  • localValues的存储
void put(ThreadLocal<?> key, Object value){
    cleanUp();
    int firstTombstone=-1;
    for(int index=key.hash & mark;;index=next(index)){
        Object k=table[index];
        if(k==key.reference){
            table[index+1]=value;
            return;
        }
        if(k==null){
            if(firstTombstone==-1){
                table[index]=key.reference;
                table[index+1]=value;
                size++;
                return;
            }

            table[firstTombstone]=key.reference;
            table[firstTombstone+1]=value;
            tombstones--;
            size++;
            return;
        }
        if(firstTombstone==-1 && k==TOMBSTONE){
            firstTombstone=index;                             
        }
    }
}\

以上实现了数据的存储过程,ThreadLocal的值在table数组中的位置在ThreadLocal的reference所表示的对象的下一个位置。

  • ThreadLocal的get方法
public T get(){
    Thread currentThread = Thread.currentThread();
    values values=values(currentThread);
    if(values!=null){
        Object[] table=values.table;
        int index=hash&values.mask;
        if(this.reference==table[index]){
            return (T)table[index+1];
        }
    }
    else{
        values=initializeValues(currentThread);
    }
    return (T)values.getAfterMiss(this);
}

get方法为取出当前线程的localValues对象,如果对象为null则返回初始值(初始值morenqingkuangxiaweinull)。如果对象不为null,取出table数组并且找出ThreadLocal的reference对象在数组中的位置,然后获取下一个值,即为ThreadLocal的值。

2.2、消息队列

消息队列即指MessageQueue,MessageQueue包含插入和读取两个操作。同时在读取的时候回进行删除。插入为enqueueMessage,作用是往消息队列中插入一条消息,读取为next,读取队列中的消息并且删除。MessageQueue内部使用的是单链表,便于插入和删除。

  • enqueueMessage
boolean enqueueMessage(Message msg,long when){
    ...
    synchroized(this){
        ...
        msg.markInUse();
        msg.when=when;
        Message p=mMessage;
        boolean needWake;
        if(p==null || when==0 || when<p.when){
            msg.next=p;
            mMessage=msg;
            needWake=mBlocked;
        }
        else{
            needWake=mBlocked && p.target==nulol && 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;
            prev.next=msg;
        }
        if(needWake){
            nati9veWake(mPtr);
        }
    }
    return true;
}

enqueueMessage主要为单链表的插入操作。

  • next方法
Message next(){
    ...
    int pendingIdleHandlerCount=-1;
    int nextPollTimeoutMillis!=0;
    for(;;){
        if(nextPollTimeoutMillis!=0){
            Binder.flushPendingCommands();
        }
        nativePollOnce(ptr,nextPollTimeoutMillis);
        synchronized(this){
            final long now=SystemClock.uptimeMillis();
            Message prevMsg=null;
            Message msg=mMessage;
            if(msg!=null && msg.target==null){
                do{
                    prevMsg=msg;
                    msg=msg.next;
                }while(msg != null && !msg.isAsynchronous());
            }
            if(msg!=null){
                if(now < msg.when){
                    nextPollTimeoutMillis=(int)Math.min(msg.when-now,Integer.MAX_VALUE);
                }
                else{
                    mBlocked=false;
                    if(mPrevMsg!=null){
                        prevMsg.next==msg.next;
                    }
                    else{
                        mMessage=msg.next;
                    }
                    msg.next=null;
                    if(false) log.v("MessageQueue", "Returning message:"+msg);
                    return msg;
                }
            }
            else{
                nextPollTimeoutMillis=-1;
            }
            ...
        }
        ...
    }
}

next方法是无限循环的,当消息队列中没有相应的消息,next方法会阻塞在这里,当有新消息时,next方法会返回新消息并删除这条消息。

2.3、Looper

Looper会不断从MessageQueue中查看有无新消息,有则立刻处理,没有就会堵塞在那里。在构造方法中会创建MessageQueue,然后保存当前线程的对象。

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

Looper.prepare()会为当前线程创建一个Looper,然后通过Looper开启消息循环。

new Thread(){
    @Override
    public void run(){
        Looper.prepare();
        Handler handler=new Handler();
        Looper.loop();
    };
}.start();

同时还有prepareMainLooper方法,这个方法用于给主线程创建Looper,本质也是通过prepare方法实现。
Looper同时提供了quit和quitSafely方法来推出一个Looper,其中quit会直接退出Looper,而quitSafely会把消息队列中的消息处理完后退出。在Looper退出后,Handler发送消息就会失败。

  • 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();
        if(msg==null){
            return;
        }
        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,"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);
        }
        msg.recycleUnchecked();
    }
}

loop是一个死循环,只有当MessageQueue的next方法返回了null,才会跳出循环。当Looper的quit或quitSafely方法被调用,就会将消息队列标记退出,此时next就会返回null。loop方法调用MessageQueue的next方法获得新消息。

2.3、Handler

Handler的工作包含了消息的发送和接收,使用post或send发送消息。post的一系列方法是通过send来实现的。

public final boolean sendMessage(Message msg){
    return sendMessageDelayed(msg,0);
}

public final boolean sendMessage(Message msg,long delayMillis){
    if(delayMillis<0){
        delayMillis=0;
    }
    return sendMessageAtTime(msg,SystemClock.uptimeMillis()+delayMillis);
}

public final boolean sendMessageAtTime(Message msg,long uptimeMillis){
    MessageQueue queue=mQueue;
    if(queue==null){
        RuntimeExecption e=new RuntimeExecption(
            this+"sendMessageAtTime() called with no mQueue");
        log.w("Looper", e.getMessage(),e);
        return false;
    }
    return enqueueMessage(queue,msg,uptimeMills);
}

public final boolean enqueueMessage(MessageQueue queue,Message msg,long uptimeMillis){
    msg.target=this;
    if(mAsynchronous){
        msg.setAsynchronous(true);
    }
    return queue.enqueueMessage(msg,uptimeMills);
}

Handler发送消息是向消息队列中插入了一条消息,MessageQueue的next方法就会返回消息给Looper,Looper收到消息后就开始处理,最终消息交给handler处理,即调用Handler的dispatchMessage方法。

  • dispatchMessage
public void dispatchMessage(Message msg){
    if(msg.callback!=null){
        handleCallback(msg);
    }
    else{
        if(mCallback!=null){
            if(mCallback.handleMessage(msg)){
                return ;
            }
        }
        handleMessage(msg);
    }
}

dispachMessage首先会检查callback是否为null,不为null就直接通过handleMessage处理消息,否则判断mCallback是否为null,不为null则调用mCallback的handleMessage方法,最后调用Handler的handleMessage方法。

  • handleCallback
private static void handleCallback(Message message){
    message.callback.run();
}
  • Callback
public interface Callback{
    public boolean handleMessage(Message msg);
}

3、主线程的消息循环

Android的主线程就是ActivityThread,主线程的入口方法为main,在main方法中系统会通过Looper.prepareMainLooper()来创建主线程的Looper以及MessageQueue,并通过Looper.loop()来开启主线程消息循环。

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();
    }

    AsyncTask.init();

    if(false){
        Looper.myLooper().setMessageLogging(new LogPrinter(Log.DEBUG,"ActivityThread"));
    }
    Looper.loop();
    throw new RuntimeExeception("Main thread loop unexpectedly exited");
}

主线程消息循环开始后,ActivityThread还需要Handler和消息队列进行交互,此时的Handler就是ActivityThread.H。其内部定义了一组消息类型,包含了四大组件的启动和停止等过程。
ActivityThread通过ApplicationThread和AMS进行进程间通信,AMS以进程间通信的方式完成ActivityThread请求后回调ApplicationThread中的Binder方法,然后ApplicationThread会向H发送消息,H收到消息后会将ApplicationThread中的逻辑切换到ActivitytThread中去执行。

参考来源:

《Android开发艺术探索》

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

推荐阅读更多精彩内容

  • 为了更好的理解 Looper 的工作原理,我们需要对 ThreadLocal 进行了解,如果对 ThreadLoc...
    墨染书阅读 1,477评论 0 3
  • 前言: 提到Android消息机制大家应该都不陌生,在日常开发中都不可避免地要涉及到这方面的内容。从开发的角度来说...
    JiaYang627阅读 357评论 0 1
  • 学习内容 Android 的消息机制 Handler 即其底层支撑 原文开篇部分: 从开发角度来说,Handler...
    whd_Alive阅读 305评论 0 0
  • 提到消息机制,想必大家都不陌生吧,在日常开发中不可避免要涉及到这方面的内容。从开发的角度来说,Handler是An...
    斜杠时光阅读 658评论 1 9
  • 就几句话。 总以为我本科毕业很了不起。 毕业后发现,有什么大不了,挣不来钱,放不开胆,猥猥琐琐,犹犹豫豫,...
    丫丫妮妮阅读 333评论 0 1