Android Looper Hander和MessageQueue的关系

本文深入解析Android中使用Handler和Thread进行线程间通信的方法,包括Handler的构造、Looper的初始化及消息队列的运作流程。阐述了如何在不同线程中实例化Handler以及消息的发送、接收与处理过程,详细解读了Android源码中的关键实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       使用Handler和Thread是Android进行线程间通信的主要方式。具体方式是,在异步线程中,使用handler发送Message到指定队列(handler.sendMessage(Message msg))。目标队列接收消息后,将消息添加到队列中,Looper轮询队列,依次对异步线程发送过来的Message进行处理,下面结合Android源码详述。

       先看Handler的构造方法(android.os.Handler.java):

      

 public Handler() {
           //doSomething
          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 = null;
    }

    /**
     * Constructor associates this handler with the queue for the
     * current thread and takes a callback interface in which you can handle
     * messages.
     */
    public Handler(Callback callback) {
       //doSomething
        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;
    }

    /**
     * Use the provided queue instead of the default one.
     */
    public Handler(Looper looper) {
        mLooper = looper;
        mQueue = looper.mQueue;
        mCallback = null;
    }

    /**
     * Use the provided queue instead of the default one and take a callback
     * interface in which to handle messages.
     */
    public Handler(Looper looper, Callback callback) {
        mLooper = looper;
        mQueue = looper.mQueue;
        mCallback = callback;
    }

 每个handler必定有一个对应的Looper,如果没有在构造器中传入,则调用Looper.myLooper()生成一个默认的Looper,再去看Looper的代码(android.os.Looper.java):

 // sThreadLocal.get() will return null unless you've called prepare().
    static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();

    /**
     * Return the Looper object associated with the current thread.  Returns
     * null if the calling thread is not associated with a Looper.
     */
    public static Looper myLooper() {
        return sThreadLocal.get();
    }

 sThreadLocal里什么时候装进去的Looper呢?在android.app.ActivityThread.java代码里发现了对Looper的static方法的调用:

 public static void main(String[] args) {
        SamplingProfilerIntegration.start();

        // CloseGuard defaults to true and can be quite spammy.  We
        // disable it here, but selectively enable it later (via
        // StrictMode) on debug builds, but using DropBox, not logs.
        CloseGuard.setEnabled(false);

        Process.setArgV0("<pre-initialized>");

        Looper.prepareMainLooper();
        if (sMainThreadHandler == null) {
            sMainThreadHandler = new Handler();
        }

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

        if (false) {
            Looper.myLooper().setMessageLogging(new
                    LogPrinter(Log.DEBUG, "ActivityThread"));
        }

        Looper.loop();

        throw new RuntimeException("Main thread loop unexpectedly exited");
    }

 这个main方法在系统启动时已经被调用过,android.os.Looper.sThreadLocal是一个静态常量,所有Looper实例共用此常量。

   

     /** Initialize the current thread as a looper.
      * This gives you a chance to create handlers that then reference
      * this looper, before actually starting the loop. Be sure to call
      * {@link #loop()} after calling this method, and end it by calling
      * {@link #quit()}.
      */
    public static void prepare() {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper());
    }

    /**
     * Initialize the current thread as a looper, marking it as an
     * application's main looper. The main looper for your application
     * is created by the Android environment, so you should never need
     * to call this function yourself.  See also: {@link #prepare()}
     */
    public static void prepareMainLooper() {
        prepare();
        setMainLooper(myLooper());
        myLooper().mQueue.mQuitAllowed = false;
    }

    private synchronized static void setMainLooper(Looper looper) {
        mMainLooper = looper;
    }

    /** Returns the application's main looper, which lives in the main thread of the application.
     */
    public synchronized static Looper getMainLooper() {
        return mMainLooper;
    }

 prepareMainLooper()方法调用了prepare()方法,prepare()方法为android.os.Looper.sThreadLocal进行赋值,我们继续跟进Looper的构造器:

    private Looper() {
        mQueue = new MessageQueue();
        mRun = true;
        mThread = Thread.currentThread();
    }

 至此,Looper和当前线程对应起来,并且实例化MessageQueue对象。

         按以上,如果我们在主线程里不传Looper实例化了一个Handler,handler对应的Looper所对应的线程就是ActivitThread运行的线程,就是我们常说的主线程。

        如果我们在非主线程里实例化了一个Looper,方法必然是在当前线程调用Looper.prepare(); Looper.loop();。Handler对应的Looper对应当前线程。

        一个线程中可以创建多个Handler,但只能拥有一个Looper,一个Looper对应一个消息队列。

         注:对同一个ThreadLocal对象调用get方法,不同线程将得到不同结果,具体原理与介绍不在此描述。

 

 

 

      下面我们开始使用handler工作,发送消息时使用sendMessage方法或者Message的sendToTarget方法,最终都会调用到handler对应的Looper持有的MessageQueue,队列调用

final boolean enqueueMessage(Message msg, long when) {
        if (msg.isInUse()) {
            throw new AndroidRuntimeException(msg
                    + " This message is already in use.");
        }
        if (msg.target == null && !mQuitAllowed) {
            throw new RuntimeException("Main thread not allowed to quit");
        }
        final boolean needWake;
        synchronized (this) {
            if (mQuiting) {
                RuntimeException e = new RuntimeException(
                    msg.target + " sending message to a Handler on a dead thread");
                Log.w("MessageQueue", e.getMessage(), e);
                return false;
            } else if (msg.target == null) {
                mQuiting = true;
            }

            msg.when = when;
            //Log.d("MessageQueue", "Enqueing: " + msg);
            Message p = mMessages;
            if (p == null || when == 0 || when < p.when) {
                msg.next = p;
                mMessages = msg;
                needWake = mBlocked; // new head, might need to wake up
            } else {
                Message prev = null;
                while (p != null && p.when <= when) {
                    prev = p;
                    p = p.next;
                }
                msg.next = prev.next;
                prev.next = msg;
                needWake = false; // still waiting on head, no need to wake up
            }
        }
        if (needWake) {
            nativeWake(mPtr);
        }
        return true;
    }

 这个方法把发送的Message添加到队列中,而已经启动的Looper则一直在轮询运行

    /**
     * Run the message queue in this thread. Be sure to call
     * {@link #quit()} to end the loop.
     */
    public static void loop() {
        Looper me = myLooper();
        if (me == null) {
            throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
        }
        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();
        
        while (true) {
            Message msg = queue.next(); // might block
            if (msg != null) {
                if (msg.target == null) {
                    // No target is a magic identifier for the quit message.
                    return;
                }

                long wallStart = 0;
                long threadStart = 0;

                // This must be in a local variable, in case a UI event sets the logger
                Printer logging = me.mLogging;
                if (logging != null) {
                    logging.println(">>>>> Dispatching to " + msg.target + " " +
                            msg.callback + ": " + msg.what);
                    wallStart = SystemClock.currentTimeMicro();
                    threadStart = SystemClock.currentThreadTimeMicro();
                }

                msg.target.dispatchMessage(msg);

                if (logging != null) {
                    long wallTime = SystemClock.currentTimeMicro() - wallStart;
                    long threadTime = SystemClock.currentThreadTimeMicro() - threadStart;

                    logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
                    if (logging instanceof Profiler) {
                        ((Profiler) logging).profile(msg, wallStart, wallTime,
                                threadStart, threadTime);
                    }
                }

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

 方法中msg.target.dispatchMessage(msg);说明handlder的dispatchMessage()方法运行在loop()调用的线程中,dispatchMessage方法将调用handleMessage方法。

     这样,Handler处理消息的代码将运行在其对应的Looper所在的线程。整体的结构也就清晰了:

     Handler实例化时,引用一个Looper,Looper唯一对应一个线程,Looper持有一个消息队列,Handler发送消息到消息队列,Looper轮询获取消息队列中的待处理Message,Message对象的target在轮询中顺次分发给Handler,Handler的handleMessage方法被调用。

内容概要:本文档为《400_IB Specification Vol 2-Release-2.0-Final-2025-07-31.pdf》,主要描述了InfiniBand架构2.0版本的物理层规范。文档详细规定了链路初始化、配置与训练流程,包括但不限于传输序列(TS1、TS2、TS3)、链路去偏斜、波特率、前向纠错(FEC)支持、链路速度协商及扩展速度选项等。此外,还介绍了链路状态机的不同状态(如禁用、轮询、配置等),以及各状态下应遵循的规则命令。针对不同数据速率(从SDR到XDR)的链路格式化规则也有详细说明,确保数据包格式控制符号在多条物理通道上的一致性正确性。文档还涵盖了链路性能监控错误检测机制。 适用人群:适用于从事网络硬件设计、开发及维护的技术人员,尤其是那些需要深入了解InfiniBand物理层细节的专业人士。 使用场景及目标:① 设计实现支持多种数据速率编码方式的InfiniBand设备;② 开发链路初始化训练算法,确保链路两端设备能够正确配置并优化通信质量;③ 实现链路性能监控错误检测,提高系统的可靠性稳定性。 其他说明:本文档属于InfiniBand贸易协会所有,为专有信息,仅供内部参考技术交流使用。文档内容详尽,对于理解实施InfiniBand接口具有重要指导意义。读者应结合相关背景资料进行学习,以确保正确理解应用规范中的各项技术要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值