1> 内部调用 sendMessageDelayed 函数
public final boolean postDelayed(Runnable r, long delayMillis){
return sendMessageDelayed(getPostMessage(r), delayMillis);
}
2> sendMessageDelayed 函数内部调用 sendMessageDelayed
public final boolean sendMessageDelayed(Message msg, long delayMillis){
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}
3> sendMessageDelayed 内部调用 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 内部调用 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 函数内部调用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,通过分析可以得到如下的结论
- 消息队列是在一个同步的代码块中 synchronized (this) {}
- 如果发送的消息为空、延迟时间为0等,标识了这条信息就是一条普通消息,不进行特殊处理,体现在:if (p == null || when == 0 || when < p.when) {}
- 如果发送的消息含有延迟信息,则使用 nativeWake (native 函数) 函数进行唤醒
实践结论总结:调用 Handler.postDelayed 函数不会造成主线程阻塞,请看下面例子,应用会先展示onCreate—1、onCreate—3、onCreate—2,且延迟消息的也是一个有序的消息队列(先入先出)
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();
喜欢的话、双击、评论、转发,动一动你的小手让更多的人知道,关注帅比~杨!