准备☞Android 异步消息分发机制

Android的消息机制

–主要是指 Handler 的运行机制 和 MessageQueue 、 looper 的工作过程。

  1. MessageQueue
    MessageQueue 消息队列,用来存储消息,虽然称为消息队列,但是它的存储结构是采用 单链表的数据结构来存储消息队列的。
    包含两个操作插入(enqueueMessage) 和 读取(next)
    enqueueMessage :往消息链表 中插入一条信息
    next:从消息列表中读取一天消息,并将其从消息队列中移除,next方法是一个无限循环的方法,如果消息列表里没有消息,那么会一直阻塞在这里。
  2. Looper
    Looper 消息循环,用来处理消息。
    Looper 会不停的从 MessageQueue 中查看是否有新的消息,如果有新消息就会立即处理
Looper.java

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

在 Looper 的构造方法会创建一个MessageQueue,消息队列,然后将当前线程的对象保存起来。


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

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.prepare() 不能被调用两次?

答:因为每次调用 Looper.prepare() 的时候的都会判断 ThreadLocal是否已经存储当前的 Looper 对象,如果已经存在就会抛出异常。
 /**
     * Run the message queue in this thread. Be sure to call
     * {@link #quit()} to end the 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(); // might block
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }

            // This must be in a local variable, in case a UI event sets the logger
            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.isTagEnabled(traceTag)) {
                Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
            }
            try {
                msg.target.dispatchMessage(msg);
            } finally {
                if (traceTag != 0) {
                    Trace.traceEnd(traceTag);
                }
            }

            if (logging != null) {
                logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
            }

            // Make sure that during the course of dispatching the
            // identity of the thread wasn't corrupted.
            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 方法为空,即queue.next() 为空。Looper 调用 quit 方法或者 quitSafely 方法,通知消息队列退出,会使 MessageQueue 的 next 方法 返回为空。

loop 调用 MessageQueue 的 next 方法获取新的消息,如果 没有消息,next 方法会一直阻塞在那里,导致 loop 方法也会一直阻塞在那里。 如果 next 方法返回了新的消息,Looper 就会处理新的消息,调用 msg.target.dispatchMessage(msg),msg.target 就是发送这条消息的 Handler 对象(Handler 中有讲解)。这样就把代码逻辑切换到指定线程去执行了。
3. ThreadLocal

线程内部的数据存储类, 通过它可以在指定的线程存储数据,而且存储后,只能在指定线程获取存储的数据,其他线程无法获取。
应用:当某些数据是以线程为作用域,并且不同线程具有不同数据
存在Looper中,并不是线程,作用是在每个线程中存储数据

问题:Handler内部是如何获取当前线程的 Looper 的?

Handler.java
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;
    }

答:Handler 通过 ThreadLocal 获取每个线程的 Looper. ThreadLocal 可以在不同线程中互不干扰的存储并提供数据。
在调用 Looper.prepare() 的时候 “sThreadLocal.set(new Looper(quitAllowed));” - 创建了一个 Looper 实例并将一个Looper的实例放入了ThreadLocal 存储。然后在 Handler 中 调用“mLooper = Looper.myLooper();”-从 sThreadLocal.get() 获取到 Looper 对象。

问题:主线程中为什么可以默认使用Handler?

答: 线程默认是没有Looper 的 ,如果要使用 Handler ,就必须为线程创建 Looper. 但是主线程,也就是 UI 线程,它就是 ActivityThread, ActivityThread 被创建的时候默认会初始化 Looper,当前UI线程调用了Looper.prepare()和Looper.loop()方法.
4. Handler
主要工作:发送和接收消息;

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

由上面可以看出 Handler 发送消息的过程就是向消息链表中插入了一条消息,MessageQueue 的 next 方法会返回这条消息给 Looper,Looper 收到消息,通过msg.target.dispatchMessage(msg) 交给 Handler 处理。
上面 enqueueMessage 方法首先 “msg.target = this;” 会把 this 赋值给 msg.target ,也就是说 把Handler 赋值给 msg的 target 属性。

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

首先会检查 Message的 callback 是否为 null,不为空就处理通过 handleCallback 来处理消息。
callback 是一个 Runnable 接口,就是 Handler 的 post 方法传递的 Runnable 参数

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

举个栗子 第一种handler 的使用方式,post方法

Handler mHandler = new handler();
mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                Log.e("TAG", Thread.currentThread().getName());
                mTxt.setText("yoxi");
            }
        });

其次 查看 mCallback 是否为空,mCallback 是个接口

    public interface Callback {
        public boolean handleMessage(Message msg);
    }

Callback 提供另一种使用 Handler 的方式,不想派生Handler的子类,可以通过 Callback 来实现。
举个栗子 第二种使用 Handler 的方式

private Handler mHandler = new Handler()
    {
        public void handleMessage(android.os.Message msg)
        {
            switch (msg.what)
            {
            case value:
                break;
            default:
                break;
            }
        };
    };

最后,调用 Handler的 handleMessage 方法来处理消息。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值