您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Android線程什么情況下會進(jìn)入等待狀態(tài)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Android線程什么情況下會進(jìn)入等待狀態(tài)”吧!
兩種情況,一是當(dāng)消息隊列中沒有消息時,它會使線程進(jìn)入等待狀態(tài);;二是消息隊列中有消息,但是消息指定了執(zhí)行的時間,而現(xiàn)在還沒有到這個時間,線程也會進(jìn)入等待狀態(tài)。
消息隊列中的消息是按時間先后來排序的,后面我們在分 析消息的發(fā)送時會看到。
這個函數(shù)最關(guān)鍵的地方便是從消息隊列中獲取下一個要處理的消息了,即MessageQueue.next函數(shù),它實(shí)現(xiàn)frameworks/base/core/java/android/os/MessageQueue.java文件中:
[java] view plaincopypublic class MessageQueue {
......
final Message next() {
int pendingIdleHandlerCount = -1; // -1 only during first iteration
int nextPollTimeoutMillis = 0;
for (;;) {
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
nativePollOnce(mPtr, nextPollTimeoutMillis);
synchronized (this) {
// Try to retrieve the next message. Return if found.
final long now = SystemClock.uptimeMillis();
final Message msg = mMessages;
if (msg != null) {
final long when = msg.when;
if (now >= when) {
mBlocked = false;
mMessages = msg.next;
msg.next = null;
if (Config.LOGV) Log.v("MessageQueue", "Returning message: " + msg);
return msg;
} else {
nextPollTimeoutMillis = (int) Math.min(when - now, Integer.MAX_VALUE);
}
} else {
nextPollTimeoutMillis = -1;
}
// If first time, then get the number of idlers to run.
if (pendingIdleHandlerCount < 0) {
pendingIdleHandlerCount = mIdleHandlers.size();
}
if (pendingIdleHandlerCount == 0) {
// No idle handlers to run. Loop and wait some more.
mBlocked = true;
continue;
}
if (mPendingIdleHandlers == null) {
mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount,
];
}
mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
}
// Run the idle handlers.
// We only ever reach this code block during the first iteration.
for (int i = 0; i < pendingIdleHandlerCount; i++) {
final IdleHandler idler = mPendingIdleHandlers[i];
mPendingIdleHandlers[i] = null; // release the reference to the handler
boolean keep = false;
try {
keep = idler.queueIdle();
} catch (Throwable t) {
Log.wtf("MessageQueue", "IdleHandler threw exception", t);
}
if (!keep) {
synchronized (this) {
mIdleHandlers.remove(idler);
}
}
}
// Reset the idle handler count to 0 so we do not run them again.
pendingIdleHandlerCount = 0;
// While calling an idle handler, a new message could have been
livered
// so go back and look again for a pending message without waiting.
nextPollTimeoutMillis = 0;
}
}
......
}
執(zhí)行下面語句是看看當(dāng)前消息隊列中有沒有消息:
[java] view plaincopynativePollOnce(mPtr, nextPollTimeoutMillis);
這是一個JNI方法,我們等一下再分析,這里傳入的參數(shù)mPtr就是指向前面我們在JNI層創(chuàng)建的NativeMessageQueue對象了,而參數(shù) nextPollTimeoutMillis則表示如果當(dāng)前消息隊列中沒有消息,它要等待的時候,for循環(huán)開始時,傳入的值為0,表示不等待。
到此,相信大家對“Android線程什么情況下會進(jìn)入等待狀態(tài)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。