1> 內(nèi)部調(diào)用 sendMessageDelayed 函數(shù)
public final boolean postDelayed(Runnable r, long delayMillis){
return sendMessageDelayed(getPostMessage(r), delayMillis);
}
2> sendMessageDelayed 函數(shù)內(nèi)部調(diào)用 sendMessageDelayed
public final boolean sendMessageDelayed(Message msg, long delayMillis){
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}
3> sendMessageDelayed 內(nèi)部調(diào)用 sendMessageAtTime
public boolean sendMessageAtTime(Message msg, long uptimeMillis{
MessageQueue queue = mQueue;
if (queue == null) {
RuntimeException e = new RuntimeException(this + " sendMessageAtTime() called with no mQueue");
Log.w("Looper", e.getMessage(), e);
return false;
}
return enqueueMessage(queue, msg, uptimeMillis);
}
4> sendMessageAtTime 內(nèi)部調(diào)用 enqueueMessa
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
5> enqueueMessage 函數(shù)內(nèi)部調(diào)用queue.enqueueMessage
boolean enqueueMessage(Message msg, long when) {
if (msg.target == null) {
throw new IllegalArgumentException("Message must have a target.");
}
if (msg.isInUse()) {
throw new IllegalStateException(msg + " This message is already in use.");
}
synchronized (this) {
if (mQuitting) {
IllegalStateException e = new IllegalStateException(
msg.target + " sending message to a Handler on a dead thread");
Log.w(TAG, e.getMessage(), e);
msg.recycle();
return false;
}
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 {
// Inserted within the middle of the queue. Usually we don't have to wake
// up the event queue unless there is a barrier at the head of the queue
// and the message is the earliest asynchronous message in the queue.
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;
}
// We can assume mPtr != 0 because mQuitting is false.
if (needWake) {
nativeWake(mPtr);
}
}
return true;
}
核心代碼在 enqueueMessage室囊,通過分析可以得到如下的結(jié)論
- 消息隊(duì)列是在一個(gè)同步的代碼塊中 synchronized (this) {}
- 如果發(fā)送的消息為空吭狡、延遲時(shí)間為0等,標(biāo)識(shí)了這條信息就是一條普通消息劣纲,不進(jìn)行特殊處理,體現(xiàn)在:if (p == null || when == 0 || when < p.when) {}
- 如果發(fā)送的消息含有延遲信息育苟,則使用 nativeWake (native 函數(shù)) 函數(shù)進(jìn)行喚醒
實(shí)踐結(jié)論總結(jié):調(diào)用 Handler.postDelayed 函數(shù)不會(huì)造成主線程阻塞慢逾,請(qǐng)看下面例子歧强,應(yīng)用會(huì)先展示onCreate---1反番、onCreate---3沙热、onCreate---2,且延遲消息的也是一個(gè)有序的消息隊(duì)列(先入先出)
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"onCreate---3", Toast.LENGTH_SHORT).show();
}
},2000);
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "onCreate---2", Toast.LENGTH_SHORT).show();
}
}, 2000);
Toast.makeText(MainActivity.this, "onCreate---1", Toast.LENGTH_SHORT).show();
喜歡的話罢缸、雙擊篙贸、評(píng)論、轉(zhuǎn)發(fā)枫疆,動(dòng)一動(dòng)你的小手讓更多的人知道爵川,關(guān)注帥比~楊!