This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
Looper解读
类图
prepare
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
publicstaticvoidprepare(){prepare(true);}privatestaticvoidprepare(booleanquitAllowed){// 如果线程已经有了Looper就会报错
if(sThreadLocal.get()!=null){thrownewRuntimeException("Only one Looper may be created per thread");}sThreadLocal.set(newLooper(quitAllowed));}/**
* 构造方法中创建MessageQueue,并引用当前线程。
*/privateLooper(booleanquitAllowed){mQueue=newMessageQueue(quitAllowed);mThread=Thread.currentThread();}
voidquit(booleansafe){if(!mQuitAllowed){thrownewIllegalStateException("Main thread not allowed to quit.");}synchronized(this){if(mQuitting){return;}mQuitting=true;if(safe){removeAllFutureMessagesLocked();}else{removeAllMessagesLocked();}// We can assume mPtr != 0 because mQuitting was previously false.
nativeWake(mPtr);}}
...Messagemsg=queue.next();// might block
if(msg==null){// No message indicates that the message queue is quitting.
me.mLogging.println("注意啦,真的停啦~~~");return;}...