主線程Looper創(chuàng)建的位置
ActivityThread的main方法中屑宠,也就是消息機制開始建立的地方
Looper.prepareMainLooper();
// Find the value for {@link #PROC_START_SEQ_IDENT} if provided on the command line.
// It will be in the format "seq=114"
long startSeq = 0;
if (args != null) {
for (int i = args.length - 1; i >= 0; --i) {
if (args[i] != null && args[i].startsWith(PROC_START_SEQ_IDENT)) {
startSeq = Long.parseLong(
args[i].substring(PROC_START_SEQ_IDENT.length()));
}
}
}
ActivityThread thread = new ActivityThread();
thread.attach(false, startSeq);
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}
if (false) {
Looper.myLooper().setMessageLogging(new
LogPrinter(Log.DEBUG, "ActivityThread"));
}
// End of event ActivityThreadMain.
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Looper.loop();
1. 調(diào)用了Looper.prepareMainLooper()方法军援,內(nèi)部又調(diào)用了prepare()方法
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
可以看到,new了一個looper對象,存到了sThreadLocal中涛菠。
2. 調(diào)用new Looper(quitAllowed)構造方法,創(chuàng)建了MessageQueue消息隊列铺然。
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
3. thread.getHandler()方法,拿到H(),其實也就是拿到Handler對象且轨。
new了ActivityThread對象浮声,此時H()對象也隨著創(chuàng)建出來。
ActivityThread thread = new ActivityThread();
thread.attach(false, startSeq);
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}
H()對象其實也就是Handler對象旋奢,調(diào)用了handler的無參構造
public Handler() {
this(null, false);
}
最終調(diào)用如下代碼泳挥,完成了H()對消息隊列和Looper的引用的持有。
public Handler(@Nullable Callback callback, boolean async) {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread " + Thread.currentThread()
+ " that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
4. 調(diào)用Looper.loop()開啟死循環(huán)至朗,至此Android的消息機制開啟
myLooper()會取出ThreadLocal里存儲的Looper對象屉符,并取出Looper中的消息隊列,然后死循環(huán)锹引,開始從消息隊列中取消息矗钟,通過msg.target(也就是發(fā)送消息的handler對象)的dispatchMessage()方法,把消息交給handler去處理嫌变。
/**
* Run the message queue in this thread. Be sure to call
* {@link #quit()} to end the loop.
*/
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;
...
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
final Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
try {
msg.target.dispatchMessage(msg);
} catch (Exception exception) {
if (observer != null) {
observer.dispatchingThrewException(token, msg, exception);
}
throw exception;
} finally {
ThreadLocalWorkSource.restore(origWorkSource);
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
...
msg.recycleUnchecked();
}
}
總結
- 一個線程中只允許有一個Looper和消息隊列
原因:1.代碼限制真仲;2.多個會導致混亂
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
Looper構造方法內(nèi)創(chuàng)建了一個消息隊列,多了不利于維護初澎。
一個線程中可以有多個Handler對象
線程間通過可以Handler在A,B之間通信是因為Handler在A線程創(chuàng)建時候秸应,持有了A的Looper和消息隊列,B線程持有了A的引用碑宴,所以可以通過Handler發(fā)送消息給A软啼。