Android应用程序消息处理机制(Looper、Handler)分析(6)

移动开发 Android
什么情况下,线程会进入等待状态呢?两种情况,一是当消息队列中没有消息时,它会使线程进入等待状态;;二是消息队列中有消息,但是消息指定了执行的时间,而现在还没有到这个时间,线程也会进入等待状态。消息队列中的消息是按时间先后来排序的,后面我们在分 析消息的发送时会看到。

调用以下函数的时候,有可能会让线程进入等待状态。

什么情况下,线程会进入等待状态呢?

两种情况,一是当消息队列中没有消息时,它会使线程进入等待状态;;二是消息队列中有消息,但是消息指定了执行的时间,而现在还没有到这个时间,线程也会进入等待状态。

消息队列中的消息是按时间先后来排序的,后面我们在分 析消息的发送时会看到。

这个函数最关键的地方便是从消息队列中获取下一个要处理的消息了,即MessageQueue.next函数,它实现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


...... 
} 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.

执行下面语句是看看当前消息队列中有没有消息:

[java] view plaincopynativePollOnce(mPtr, nextPollTimeoutMillis); 
  • 1.

这是一个JNI方法,我们等一下再分析,这里传入的参数mPtr就是指向前面我们在JNI层创建的NativeMessageQueue对象了,而参数 nextPollTimeoutMillis则表示如果当前消息队列中没有消息,它要等待的时候,for循环开始时,传入的值为0,表示不等待。

责任编辑:闫佳明 来源: bbs.9ria
相关推荐

2014-05-22 15:07:44

Android消息处理机制Looper

2014-05-22 15:41:59

Android消息处理机制Looper

2014-05-22 15:48:50

Android消息处理机制Looper

2014-05-22 15:04:00

Android消息处理机制Looper

2014-05-22 15:00:16

Android消息处理机制Looper

2014-05-22 15:38:27

Android消息处理机制Looper

2014-05-22 14:57:28

Android消息处理机制Looper

2014-05-22 15:15:53

Android消息处理机制Looper

2014-05-22 15:33:31

Android消息处理机制Looper

2014-05-22 15:45:58

Android消息处理机制Looper

2011-04-28 11:01:40

Android消息处理LooperHandler

2011-11-23 09:33:45

HandlerLooperMessage

2014-05-27 10:13:57

移动技术半月刊

2016-10-21 13:03:18

androidhandlerlooper

2011-09-05 17:40:40

MTK定时器

2011-03-17 09:20:05

异常处理机制

2021-08-12 16:28:10

AndroidHandleLooper

2014-05-27 14:44:26

AndroidActivitysingleTask

2023-03-08 08:54:59

SpringMVCJava

2023-06-15 14:09:00

解析器Servlet容器
点赞
收藏

51CTO技术栈公众号