Android多线程基础 解析Handler机制

解析异步消息处理机制

Android的异步消息处理主要由4个部分组成:Message,Handler,MessageQueue以及Looper。

1.Message:是在线程之间传递信息,它可以在内部携带少量信息,用于在不同线程之间交换数据。
2.Handler:是处理者的意思,它主要用于发送和处理消息的。
3.MessageQueue:是消息队列的意思,他主要用于存所有通过Handler发送的消息,这部分消息为一直存在于消息队列中,等待被处理。每个线程只会有一个MessageQueue对象。
4.Looper:每个线程MessageQueue的管理者。调用Looper.loop()方法后,会进入到一个无线循环中,只要一发现MessageQueue中存在一条消息,就会将他取出,并且传递到Handler的handleMessage()方法中,每个线程也只会有一个Looper对象。

异步消息处理流程:

需要在主线程创建一个Handler对象,并重写handleMessage( )方法,然后在子线程需要更新UI的时候,创建一个Message对象,并通过Handle将信息发送,之后这条消息会被添加到MessageQueue中等待被处理,而Looper会一直循环的从MessageQueue中取出消息,最后分发回调handleMessage( )方法中。而此时操作也就变换到了主线程,因此可以放心的更新UI了。


.png

问题一:在子线程想更新UI的时候,为什么Handler必须声明在主线程呢?

解决这个问题之前,要先知道,UI线程的消息循环是在ActivityThread.main方法中创建的,该函数为Android应用程序入口,代码如下:

    Looper.prepareMainLooper();//1.创建消息循环Looper
   ActivityThread thread = new ActivityThread();
    thread.attach(false);
    if (sMainThreadHandler == null) {
        sMainThreadHandler = thread.getHandler();
    }

    if (false) {
        Looper.myLooper().setMessageLogging(new
                LogPrinter(Log.DEBUG, "ActivityThread"));
    }
    // End of event ActivityThreadMain.
    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
    Looper.loop();  //2.开启消息循环
    throw new RuntimeException("Main thread loop unexpectedly exited");

由这段代码可以知道,当应用程序创建启动时, Looper.prepareMainLooper();就已经帮我们创建了Looper,并且开启了消息循环。
而当我们在子线程执行耗时操作后,就需要Handler将一个消息Post到UI线程,然后在handleMessage( )处理,然而每个Handler都会关联一个消息队列,消息队列被封装在Looper中。

问题二:既然Handler关联了线程以及消息队列,那么他们是如何关联的呢?摘取一段代码

public Handler( ){
     //省略上面代码
        mLooper = Looper.myLooper();//获取Looper
        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;
}

从以上源码可知,Handler默认函数会获取Looper对象,还有消息队列,那么他们是如何获取消息对象的呢?进去 myLooper,如下:

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

发现它是 sThreadLocal.get( )获取的,那么它又是什么时候存了 Looper对象?继续看,就会发现这么一段代码

  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));//创建了Looper对象并赋值给了sThreadLocal
    }

这样 Looper就和线程关联上了,不同的线程就不能访问对方的消息队列,再看回来Handler的默认构造函数, mQueue = mLooper.mQueue;//获取消息队列,让Looper对象和消息队列关联,而Handler又和Looper关联,Looper又与线程关联,那么他们就关联起来了。
那么问题一的答案就出来了:
默认情况下,消息队列只有一个即上面分析的主线程的消息队列它在 Looper.prepareMainLooper();方法中创建,而Handler又和Looper关联,Looper又与线程(主线程)关联,此时只有Handler声明下主线程,handleMessage才会在主线程执行,在主线程更新UI才是安全的。

问题三:创建Looper后,是如何执行消息循环的?

在ActivityThread.main方法中进去看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;

        // Make sure the identity of this thread is that of the local process,
        // and keep track of what that identity token actually is.
        Binder.clearCallingIdentity();
        final long ident = Binder.clearCallingIdentity();
       //死循环
        for (;;) {
            Message msg = queue.next(); //获取消息
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }
            //....省略
            try {
                msg.target.dispatchMessage(msg);//消息处理
                end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
            } finally {
                if (traceTag != 0) {
                    Trace.traceEnd(traceTag);
                }
            }
             //....省略
            msg.recycleUnchecked();//回收消息
        }
    }

在 msg.target.dispatchMessage(msg)中,msg是Message类型进去 Message

public final class Message implements Parcelable {
  Handler target;
  Runnable callback;
  Message next;
}

可以知道target是一个Handler,在上一篇中知道,我们通过知道了Handler将消息发送给消息队列,然后在这里消息队列又会把消息分发给Handler处理。
看下消息分发处理:

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

从上面的Message类型可以知道callback是Runnable类型的,在分发的时候回进行判断,不为空由 handleCallback(msg);处理,为空时 handleMessage(msg);处理,而 handleMessage(msg);是一个空方法,我们平时写的更新UI操作也是在这里面执行的。其实他就是Handler分发的两种方式,一种Post(Runnable callback),一种sendMessage:

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

这两种最后都是会执行sendMessageDelayed()方法:

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

继续进去看sendMessageAtTime()方法。

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

继续进去看enqueueMessage()方法。

 private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);//将消息插入消息队列
    }

msg.target = this设置当前Handler对象
因此可以知道不管是Pos方法还是sendMessage方法,最后Handler都会把消息加到消息队列MessageQueue中。然后Looper会不断的去获取消息,并且调用 Handle的dispatchMessage的消息也就是( msg.target.dispatchMessage(msg);//消息处理),这样消息就会不停的被产生,被添加,和被处理。这就是消息的处理机制

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值