android Looper源码分析

looper的主要作用:绑定当前线程,然后不断的在MessageQueue中去取消息,交给handler处理
下面会从源码中的各种方法着手,开始分析

静态的loop()方法:
在这个线程中运行消息队列,确保在Looper结束时调用quit()方法

  public static void loop() {
        //获得当先线程锁关联的Looper实例
        final Looper me = myLooper();

        if (me == null) {
            //looper方法必须在prepare方法之后运行
            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 (;;) {
            //取出一条消息,如果没有消息则阻塞
            Message msg = queue.next(); 

            if (msg == null) {
                //如果没有消息意味着message queue已经退出了
                return;
            }

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

            //把消息交给msg的target的dispatchMessage方法去处理。也就是交给handler对象
            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();
        }
    }

静态的prepare()方法:
初始化当前线程为一个looper。 可以使你有机会在开始looper之前,创建一个依赖这个looper的handler。确保你调用这个方法之前,调用loop(),结束它之前调用quit()。

    public static void prepare() {
        prepare(true);
    }

    private static void prepare(boolean quitAllowed) {
        if (sThreadLocal.get() != null) {
            //一个线程中只能有一个Looper实例
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        //将Looper的实例放入ThreadLocal中
        sThreadLocal.set(new Looper(quitAllowed));
    }

静态的getMainLooper()方法
获得主线程中的Looper

    public static Looper getMainLooper() {
        synchronized (Looper.class) {
            return sMainLooper;
        }
    }

静态的 prepareMainLooper()方法:
android系统会自动调用这个方法,使当前线程作为一个Looper并使它成为程序的主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();
        }
    }

公开的quit()方法:
退出looper,退出之后向queue发送messages会失败,例如Handler的sendMessage(Message)方法会返回false

    public void quit() {
        mQueue.quit(false);
    }

公开的quitSafely()方法:
在消息队列中剩余的消息已经被交付处理之后,退出looper,但是,在looper退出之前,再发送的延迟消息,不会被处,退出之后向queue发送messages会失败,例如Handler的sendMessage(Message)方法会返回false

    public void quitSafely() {
        mQueue.quit(true);
    }

私有的Looper()方法:
在prepare()方法中调用,初始化MessageQueue,获得当前线程

    private Looper(boolean quitAllowed) {
        mQueue = new MessageQueue(quitAllowed);
        mThread = Thread.currentThread();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值