Handler/Looper/MessageQueue

  • ThreadLocal对象是一种特殊的全局变量,因为他的“全局”性只局限于自己所在的线程
  • Looper创建时,消息队列也同时被创建出来
final MessageQueue mQueue;
private Looper(boolean quitAllowed){
    mQueue = new MessageQueue(quitAllowed);
    mRun = true;
    mThread = Thread.currentThread();// Looper与当前线程建立对应关系
}

HandlerLooperMessageQueue建立联系

public Handler(){
/*省略部分代码*/
    mLooper = Looper.myLooper();// 通过sThreadLocal.get()来获取当前线程中的Looper实例
    mQueue = mLooper.mQueue;// mQueue是Looper与Handler之间沟通的桥梁
    mCallback = callback;
}
/**
     * Return the Looper object associated with the current thread.  Returns
     * null if the calling thread is not associated with a Looper.
     */
    public static @Nullable Looper myLooper() {
        return sThreadLocal.get();
    }

你可能感兴趣的:(Handler/Looper/MessageQueue)