Android消息机制

为什么要有Android消息机制

我们经常需要在子线程进行耗时的I/O操作,例如读取文件或者是访问网络等,当耗时操作完成以后需要在UI上做些改变,由于android开发规范的限制,我们并不能在子线程中访问UI控件,否则就会触发程序异常,这个时候通过Handler就可以将更新UI的操作切换到主线程执行。

android规定访问UI只能在主线程中进行,如果在子线程中访问UI,那么程序就会抛出异常。ViewRootImpImpI对UI操作做了验证,这个验证工作是由ViewRootImpI的checkThread方法来完成的,如下所示。

void checkThread(){
        if(mThread != Thread.currentThread()){  
            throw new CalledFromWrongThreadException(  
                "Only the original thread that created a view hierarchy can touch its views.");  
        }  
    }

再延伸一下,为什么系统不允许在子线程中访问UI呢?这是因为Android的UI控件不是线程安全的,如果在多线程中访问UI控件则会导致不可预期的状态。那为什么不对UI控件访问加锁呢?缺点有两个:首先加锁会让UI控件的访问的逻辑变的复杂;其次,锁机制会降低UI的访问效率。那我们不用线程来操作不就行了吗?但这是不可能的,因为Android的主线程不能执行耗时操作,否则会出现ANR(不知道ANR的自己去百度吧)。所以,从各方面来说,Android消息机制是为了解决在子线程中无法访问UI的矛盾。


Android消息机制概述

Android消息机制相关概念

  • 主线程(UI线程) 
    1. 定义:当程序第一次启动时,Android会同时启动一条主线程(Main Thread)
    2. 作用:主线程主要负责处理与UI相关的事件
  • Message(消息) 
    1. 定义:Handler接收和处理的消息对象(Bean对象)
    2. 作用:通信时相关信息的存放和传递
  • ThreadLocal 
    1. 定义:线程内部的数据存储类
    2. 作用:负责存储和获取本线程的Looper
  • Message Queue(消息队列) 
    1. 定义:采用单链表的数据结构来存储消息列表
    2. 作用:用来存放通过Handler发过来的Message,按照先进先出执行
  • Handler(处理者) 
    1. 定义:Message的主要处理者
    2. 作用:负责发送Message到消息队列&处理Looper分派过来的Message
  • Looper(循环器) 
    1. 定义:扮演Message Queue和Handler之间桥梁的角色
    2. 作用: 
      消息循环:循环取出Message Queue的Message 
      消息派发:将取出的Message交付给相应的Handler

他们之间的联系

Looper中存放有MessageQueuen,MessageQueuen中又有很多Message,当我们的Handler发送消息的时候,会获取当前的Looper,并在当前的Looper的MessageQueuen当中存放我们发送的消息,而我们的MessageQueuen也会在Looper的带动下,一直循环的读取Message信息,并将Message信息发送给Handler,并执行HandlerMessage()方法。


Looper源码分析

Looper在消息机制里扮演着消息循环的角色,它不停的从消息队列里查看是否有新消息,如果有,则立即处理新消息,没有则一直阻塞在那里。当需要为一个线程创建一个Looper的时候,需要调用Looper的两个静态方法就可以给这个线程创建一个Looper对象了,这两个方法是Looper.prepare();和Looper.loop()。

Looper在子线程的创建过程

new Thread(){
    @override
    public void run(){
        //创建消息循环Looper
        Looper.prepare();
        Handler handler = new Handler();
        //执行消息循环Looper
        Looper.loop();
    }
}.start();

看下Looper.prepare()方法

private static void prepare(boolean quitAllowed) {
    //判断sThreadLocal是否为null,否则抛出异常
    //即Looper.prepare()方法不能被调用两次
    //也就是说,一个线程中只能对应一个Looper实例
    if (sThreadLocal.get() != null) {
        throw new RuntimeException("Only one Looper may be created per thread");
    }
    //初始化Looper对象设置到ThreadLocal中
    sThreadLocal.set(new Looper(quitAllowed));
}

看下Looper的构造方法

private Looper(boolean quitAllowed) {
    //创建了一个MessageQueue(消息队列)
    //这说明,当创建一个Looper实例时,会自动创建一个与之配对的MessageQueue(消息队列)
    mQueue = new MessageQueue(quitAllowed);
    mThread = Thread.currentThread();
}

整个Looper的初始化准备工作就完了,这里做了哪几件事:

  • Looper的创建会关联一个MessageQueen的创建
  • Looper对象只能被创建一次
  • Looper对象创建后被存放在sThreadLocal中

再延伸一下Looper在主线程中的创建

public static void main(String[] args) {
    ...
    Process.setArgV0("<pre-initialized>");
    //1. 创建消息循环Looper
    Looper.prepareMainLooper();

    ActivityThread thread = new ActivityThread();
    thread.attach(false);

    if (sMainThreadHandler == null) {
        sMainThreadHandler = thread.getHandler();
    }
    ...
    //2. 执行消息循环
    Looper.loop();
    throw new RuntimeException("Main thread loop unexpectedly exited");
}

Looper在主线程中调用Looper.prepareMainLooper()方法,其本质也是通过prepare方法来实现的。

public static void prepareMainLooper() {
    //在这里会调用prepare(boolean quitAllowed)方法
    prepare(false);
    synchronized (Looper.class) {
        if (sMainLooper != null) {
            throw new IllegalStateException("The main Looper has already been prepared.");
        }
        sMainLooper = myLooper();
    }
}

下面看下Looper.loop()方法

public static void loop() {
    //myLooper()方法作用是返回sThreadLocal存储的Looper实例,如果me为null,loop()则抛出异常
    //也就是说loop方法的执行必须在prepare方法之后运行
    //也就是说,消息循环必须要先在线程当中创建Looper实例
    final Looper me = myLooper();
    if (me == null) {
        throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
    }
    //获取looper实例中的mQueue(消息队列)
    final MessageQueue queue = me.mQueue;

    Binder.clearCallingIdentity();
    final long ident = Binder.clearCallingIdentity();
    //进入消息循环
    for (;;) {
        //next()方法用于取出消息队列里的消息
        Message msg = queue.next(); 
        if (msg == null) {

            return;
        }

        final Printer logging = me.mLogging;
        if (logging != null) {
            logging.println(">>>>> Dispatching to " + msg.target + " " +
                    msg.callback + ": " + msg.what);
        }

        final long traceTag = me.mTraceTag;
        if (traceTag != 0) {
            Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
        }
        try {
            //消息派发:把消息派发给msg的target属性,然后用dispatchMessage方法去处理
            //Msg的target其实就是handler对象,下面会继续分析
            msg.target.dispatchMessage(msg);
        } finally {
            if (traceTag != 0) {
                Trace.traceEnd(traceTag);
            }
        }

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

说明:首先是获取到当前线程的Looper对象和绑定到Looper对象的消息队列,接着就是一个死循环,唯一跳出循环的方式就是MessageQueue的next方法返回了null(即调用quit或者quitSafely方法标记为退出状态,当消息队列表标记为退出状态时,next方法就会返回null);当有消息时,则通过Handler(msg.target是一个Handler对象)的dispatchMessage方法来处理这个消息,也就到了Handler里面去执行了,这样就成功地将代码逻辑切换到指定的线程中去了。

整个Looper的循环过程就完了,这里做了哪几件事:

  • 取出Looper和MessageQueen
  • 进入消息循环,有消息则分发出去
  • 消息资源的回收

Looper的退出

Looper提供了两个方法可以退出一个Looper:

  • quit():quit会直接退出Looper
  • quitSafety():quitSafety只是设定一个退出标记使MessageQueue的next方法返回null,从而退出looper方法,此方法会把消息队列中的已有消息处理完毕后退出Looper

注:

1.当Looper退出后,通过Handler发送的消息会失败,这个时候Handler的send方法会返回false; 

2. 在子线程中,如果手动为其创建了Looper,那么在所有消息处理完成之后应该调用quit方法来终止消息循环,否则这个子线程就会一直处于等待状态(MessageQueue的next方法是个阻塞操作,当没有消息时,next方法会一直阻塞在那,这也导致looper方法一直阻塞在那),而如果退出Looper以后,这个线程就会立刻终止,因此建议不需要的时候终止Looper。


MessageQueue源码分析

消息队列在android中指的是MessageQueue,MessageQueue主要包含两个操作:插入(插入一条消息到消息队列中)和读取(从消息队列中取出一条消息,并把它从消息队列中移除),两个操作对应的方法为enqueue和next。MessageQueue通过一个单链表的数据结构来维护消息队列,单链表在插入和删除上比较有优势。

enqueueMessage方法

由于Handler使用Post()方法将Message传递到MessageQueen中,在MessageQueen中会使用enqueueMessage()方法存储Message,其实现的方式是通过单链表的数据结构来存储消息列表。

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) {    
            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; 
            prev.next = msg;
        }

        if (needWake) {
            nativeWake(mPtr);
        }
    }
    return true;
}

这里做了哪几件事:

  • 首先判断消息队列里有没有消息,没有的话则将当前插入的消息作为队头,并且这时消息队列如果处于等待状态的话则将其唤醒
  • 若是在中间插入,则根据Message创建的时间进行插入

next方法

存消息是Handler存进来的,那么取消息就应该是Looper取了,从Looper的源码可以看出,消息就是在Looper中取出的,其实现是用MessageQueen里面的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 && 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 (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 {
                // 如果消息队列中没有消息,将nextPollTimeoutMillis设为-1,下次循环消息队列则处于等待状态
                nextPollTimeoutMillis = -1;
            }

            //退出消息队列,返回null,这时候Looper中的消息循环也会终止。 
            if (mQuitting) {
                dispose();
                return null;
            }
            ......
        }
        .....
    }
}

Message

其实这个Message就是用来存储Message中各种信息的Bean对象,从源码中可以其属性,这里例举我们常用的几个。

public int what;
public int arg1;
public int arg2;
public Object obj;
public Messenger replyTo;
int flags;
long when;   
Bundle data;    
Handler target;
Runnable callback;

Handler源码分析

Handler的工作主要包含消息的发送和接收过程,消息的发送可以通过post一系列方法以及send的一系列方法来实现的(其实post的一系列方法最终是通过send一系列方法实现),消息的处理通过调用dispatchMessage方法实现。

Handler的创建

Handler的创建会关联一个Looper对象,而Looper对象是关联着MessageQueen对象,所以在Handler创建时候,取出Looper和MessageQueen。

public Handler(Callback callback, boolean async) {
    ...
    //取出Looper
    mLooper = Looper.myLooper();
    if (mLooper == null) {
        throw new RuntimeException(
            "Can't create handler inside thread that has not called Looper.prepare()");
    }
    //取出Looper中的MessageQueen
    mQueue = mLooper.mQueue;
    mCallback = callback;
    mAsynchronous = async;
}

前面我们也说过了Looper是存放在ThreadLocal里面的,可以看到下面的源码就知道了

public static @Nullable Looper myLooper() {
    return sThreadLocal.get();
}

整个创建的过程就完了,这里做了哪几件事:

  • 取出Looper
  • 取出Looper中的MessageQueen

Handler发送消息

方式一、sendMessage(Message msg)

//从这里开始
public final boolean sendEmptyMessage(int what)
{
    return sendEmptyMessageDelayed(what, 0);
}

//往下追踪
public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
    Message msg = Message.obtain();
    msg.what = what;
    return sendMessageDelayed(msg, delayMillis);
}

//往下追踪
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
    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);
}

//调用sendMessage方法其实最后是调用了enqueueMessage方法
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
    //为msg.target赋值为this,也就是把当前的handler作为msg的target属性
    //如果大家还记得Looper的loop()方法会取出每个msg然后执行msg.target.dispatchMessage(msg)去处理消息,其实就是派发给相应的Handler
    msg.target = this;
    if (mAsynchronous) {
        msg.setAsynchronous(true);
    }
    //最终调用queue的enqueueMessage的方法,也就是说handler发出的消息,最终会保存到消息队列中去
    return queue.enqueueMessage(msg, uptimeMillis);
}

方式二、psot(Runnable r)

public final boolean post(Runnable r)
{
   return  sendMessageDelayed(getPostMessage(r), 0);
}

其实post()方法最终也会保存到消息队列中去,和上面不同的是它传进来的是一个Runnable对象,执行了getPostMessage()方法,我们往下追踪

private static Message getPostMessage(Runnable r) {
    Message m = Message.obtain();
    m.callback = r;
    return m;
}

Handler消息处理

dispatchMessage()方法

public void dispatchMessage(Message msg) {
    if (msg.callback != null) {
        //1. post()方法的处理方法
        handleCallback(msg);
    } else {
        if (mCallback != null) {
            if (mCallback.handleMessage(msg)) {
                return;
            }
        }
        //2. sendMessage()方法的处理方法
        handleMessage(msg);
    }
}

private static void handleCallback(Message message) {
    message.callback.run();
}

public void handleMessage(Message msg) {
}

1.首先就是判断Message的callback是否为null,如果不为null,则通过handleCallback方法调用callback的run方法,callback其实是一个Runnable的对象,实际上是通过Handler的post方法所传递的。

2.如果Message的callback为null,则再判断mCallback是否为null,如果不为null,则调用mCallback的handleMessage方法来处理,mCallback是一个接口可以通过Handler handler = new Handler(new CallBack)实现。

3.如果都为null,最后通过handler的handleMessage方法来处理。


总结:Android通信机制中Message、Handler、MessageQueen、Looper的之间的关系

首先,是这个MessageQueen,MessageQueen是一个消息队列,它可以存储Handler发送过来的消息,其内部提供了进队和出队的方法来管理这个消息队列,其出队和进队的原理是采用单链表的数据结构进行插入和删除的,即enqueueMessage()方法和next()方法。这里提到的Message,其实就是一个Bean对象,里面的属性用来记录Message的各种信息。

然后,是这个Looper,Looper是一个循环器,它可以循环的取出MessageQueen中的Message,其内部提供了Looper的初始化和循环出去Message的方法,即prepare()方法和loop()方法。在prepare()方法中,Looper会关联一个MessageQueen,而且将Looper存进一个ThreadLocal中,在loop()方法中,通过ThreadLocal取出Looper,使用MessageQueen的next()方法取出Message后,判断Message是否为空,如果是则Looper阻塞,如果不是,则通过dispatchMessage()方法分发该Message到Handler中,而Handler执行handlerMessage()方法,由于handlerMessage()方法是个空方法,这也是为什么需要在Handler中重写handlerMessage()方法的原因。这里要注意的是Looper只能在一个线程中只能存在一个。这里提到的ThreadLocal,其实就是一个对象,用来在不同线程中存放对应线程的Looper。

最后,是这个Handler,Handler是Looper和MessageQueen的桥梁,Handler内部提供了发送Message的一系列方法,最终会通过MessageQueen的enqueueMessage()方法将Message存进MessageQueen中。我们平时可以直接在主线程中使用Handler,那是因为在应用程序启动时,在入口的main方法中已经默认为我们创建好了Looper。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值