Android Looper简介

Looper主要是prepare()和loop()两个方法:

    1.首先看prepare()方法java

public static final void prepare() {  
    if (sThreadLocal.get() != null) {  
        throw new RuntimeException("Only one Looper may be created per thread");  
    }  
    sThreadLocal.set(new Looper(true));  
}

    sThreadLocal是一个ThreadLocal对象,能够在一个线程中存储变量。能够看到,在第5行,将一个Looper的实例放入了ThreadLocal,而且2-4行判断了sThreadLocal是否为null,不然抛出异常。这也就说明了Looper.prepare()方法不能被调用两次,同时也保证了一个线程中只有一个Looper实例~相信有些哥们必定遇到这个错误。ide

    2.下面看Looper的构造方法:oop

private Looper(boolean quitAllowed) {  
    mQueue = new MessageQueue(quitAllowed);  //建立了一个MessageQueue(消息队列)。
    mRun = true;  
    mThread = Thread.currentThread();  
}

    在构造方法中,建立了一个MessageQueue(消息队列)。ui

    3.而后咱们看loop()方法:this

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  
        Printer logging = me.mLogging;  
        if (logging != null) {  
            logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what);  
        }  
        msg.target.dispatchMessage(msg);  
        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.recycle();  
    }  
}

    第2行:spa

public static Looper myLooper() {
 //直接返回sThreadLocal存储的Looper实例,若是me为null则抛出异常,也就是说looper方法必须在prepare方法以后运行
    return sThreadLocal.get(); 
}

    第6行:拿到该looper实例中的mQueue(消息队列)线程

    13到45行:就进入了咱们所说的无限循环。code

    14行:取出一条消息,若是没有消息则阻塞。对象

    27行:使用调用 msg.target.dispatchMessage(msg);把消息交给msg的target的dispatchMessage方法去处理。Msg的target是什么呢?其实就是handler对象,下面会进行分析。token

    44行:释放消息占据的资源。

Looper主要做用:

    一、与当前线程绑定,保证一个线程只会有一个Looper实例,同时一个Looper实例也只有一个MessageQueue。

    二、loop()方法,不断从MessageQueue中去取消息,交给消息的target属性的dispatchMessage去处理。

相关文章
相关标签/搜索