public Handler(@Nullable Callback callback, boolean async) {
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;
}
- 創(chuàng)建handler會獲取looper對象
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
- looper從ThreadlocalMap中獲取拘哨,ThreadLocalMap通過Thread的一個成員變量threadLocals賦值,內(nèi)部維護(hù)了一個Entry數(shù)組,存儲了對應(yīng)的ThreadLocal和Looper對象撇寞,根據(jù)Threadlocal獲取對應(yīng)的Looper對象绍撞。
- ThreadLocal可以當(dāng)做是ThreadlocalMap的包裝類,提供get()和set()
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));
}
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
public static void main(String[] args) {
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false, startSeq);
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}
if (false) {
Looper.myLooper().setMessageLogging(new
LogPrinter(Log.DEBUG, "ActivityThread"));
}
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
- 在Looper.prepare()中創(chuàng)建Looper對象蜻韭,并從當(dāng)前Thread中將ThreadLocal和創(chuàng)建的Looper對象存儲進(jìn)ThreadLocalMap對象悼尾。在主線程中不需要執(zhí)行prepare()方法是因為在ActivityThread的main()中已經(jīng)執(zhí)行了
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
- 在創(chuàng)建Looper對象時,會創(chuàng)建MessageQueue對象肖方。所以一個線程對應(yīng)一個Looper闺魏,對應(yīng)一個MessageQueue
handler.sendMessage()
handler.sendMessageAtTime()
handler.sendMessageDelayed()
- 幾種發(fā)送消息的方法,最后都會走到sendMessageAtTime(Message msg, long delayMillis)方法中俯画,只是delayMeillis參數(shù)修改
private boolean enqueueMessage(@NonNull MessageQueue queue, @NonNull Message msg,
long uptimeMillis) {
msg.target = this;
msg.workSourceUid = ThreadLocalWorkSource.getUid();
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
- 發(fā)送消息時會將msg.targe賦值為當(dāng)前的handler析桥,后續(xù)分發(fā)消息時也是通過該標(biāo)志,找到對應(yīng)的handler
boolean enqueueMessage(Message msg, long when) {
synchronized (this) {
msg.markInUse();
msg.when = when;
Message p = mMessages;
boolean needWake;
if (p == null || when == 0 || when < p.when) {
// New head, wake up the event queue if blocked.
msg.next = p;
mMessages = msg;
needWake = mBlocked;
} else {
needWake = mBlocked && p.target == null && msg.isAsynchronous();
Message prev;
for (;;) {
prev = p;
p = p.next;
if (p == null || when < p.when) {
break;
}
if (needWake && p.isAsynchronous()) {
needWake = false;
}
}
msg.next = p; // invariant: p == prev.next
prev.next = msg;
}
}
return true;
}
- MessageQueue是一個鏈表結(jié)構(gòu)艰垂,根據(jù)delay的時間來修改next指向烹骨。以上是往消息隊列中添加消息的流程
for (;;) {
Message msg = queue.next();
}
- 通過Looper.loop()方法取出消息,該方法是一個死循環(huán)材泄,通過next()方法獲取下一個需要處理的消息
for (;;) {
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
nativePollOnce(ptr, nextPollTimeoutMillis); //nextPollTimeoutMillis 0立即返回 -1 一直阻塞 >0 阻塞時長
if (msg != null) {
if (now < msg.when) {
// Next message is not ready. Set a timeout to wake up when it is ready.
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
} else {
// Got a message.
mBlocked = false;
if (prevMsg != null) {
prevMsg.next = msg.next;
} else {
mMessages = msg.next;
}
msg.next = null;
if (DEBUG) Log.v(TAG, "Returning message: " + msg);
msg.markInUse();
return msg;
}
} else {
// No more messages.
nextPollTimeoutMillis = -1;
}
- 獲取消息沮焕,通過nativePollOnce設(shè)置阻塞狀態(tài),假如下一消息時間未到則阻塞等待的時長拉宗,假如沒有沒有消息則傳入-1峦树,一直阻塞。喚醒會通過添加消息enqueueMessage()#nativeWake()旦事,或者quit()#nativeWake()
msg.target.dispatchMessage(msg);
public void dispatchMessage(@NonNull Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
- 取出消息后通過handler#dispatchMessage()進(jìn)行分發(fā)魁巩,并根據(jù)handler創(chuàng)建的方式回調(diào)到對應(yīng)的位置
public final void runOnUiThread(Runnable action) {
if (Thread.currentThread() != mUiThread) {
mHandler.post(action);
} else {
action.run();
}
}
public final boolean post(@NonNull Runnable r) {
return sendMessageDelayed(getPostMessage(r), 0);
}
- runOnUiThread使用的也是handler的方法,在非UI線程時姐浮,取主線程的handler谷遂。在子線程中發(fā)送消息,最后會將消息發(fā)送到handler對應(yīng)的messageQueue中卖鲤,再通過Loop()方法去除消息處理肾扰,而Looper.loop()方法是在主線程中啟動的,所以線程切換到主線程
public final boolean postDelayed(
@NonNull Runnable r, @Nullable Object token, long delayMillis) {
return sendMessageDelayed(getPostMessage(r, token), delayMillis);
}
- 至于其他的postDelayed()方法原理都類似