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實例也只有一個MessageQueue。
二、loop()方法,不斷從MessageQueue中去取消息,交給消息的target屬性的dispatchMessage去處理。