千家信息网

Android线程什么情况下会进入等待状态

发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,本篇内容主要讲解"Android线程什么情况下会进入等待状态",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Android线程什么情况下会进入等待状态"吧!
千家信息网最后更新 2025年02月04日Android线程什么情况下会进入等待状态

本篇内容主要讲解"Android线程什么情况下会进入等待状态",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Android线程什么情况下会进入等待状态"吧!

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

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

这个函数最关键的地方便是从消息队列中获取下一个要处理的消息了,即MessageQueue.next函数,它实现frameworks/base/core/java/android/os/MessageQueue.java文件中:

  1. [java] view plaincopypublic class MessageQueue {

  2. ......

  3. final Message next() {

  4. int pendingIdleHandlerCount = -1; // -1 only during first iteration

  5. int nextPollTimeoutMillis = 0;

  6. for (;;) {

  7. if (nextPollTimeoutMillis != 0) {

  8. Binder.flushPendingCommands();

  9. }

  10. nativePollOnce(mPtr, nextPollTimeoutMillis);

  11. synchronized (this) {

  12. // Try to retrieve the next message. Return if found.

  13. final long now = SystemClock.uptimeMillis();

  14. final Message msg = mMessages;

  15. if (msg != null) {

  16. final long when = msg.when;

  17. if (now >= when) {

  18. mBlocked = false;

  19. mMessages = msg.next;

  20. msg.next = null;

  21. if (Config.LOGV) Log.v("MessageQueue", "Returning message: " + msg);

  22. return msg;

  23. } else {

  24. nextPollTimeoutMillis = (int) Math.min(when - now, Integer.MAX_VALUE);

  25. }

  26. } else {

  27. nextPollTimeoutMillis = -1;

  28. }

  29. // If first time, then get the number of idlers to run.

  30. if (pendingIdleHandlerCount < 0) {

  31. pendingIdleHandlerCount = mIdleHandlers.size();

  32. }

  33. if (pendingIdleHandlerCount == 0) {

  34. // No idle handlers to run. Loop and wait some more.

  35. mBlocked = true;

  36. continue;

  37. }

  38. if (mPendingIdleHandlers == null) {

  39. mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount,

  40. ];

  41. }

  42. mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);

  43. }

  44. // Run the idle handlers.

  45. // We only ever reach this code block during the first iteration.

  46. for (int i = 0; i < pendingIdleHandlerCount; i++) {

  47. final IdleHandler idler = mPendingIdleHandlers[i];

  48. mPendingIdleHandlers[i] = null; // release the reference to the handler

  49. boolean keep = false;

  50. try {

  51. keep = idler.queueIdle();

  52. } catch (Throwable t) {

  53. Log.wtf("MessageQueue", "IdleHandler threw exception", t);

  54. }

  55. if (!keep) {

  56. synchronized (this) {

  57. mIdleHandlers.remove(idler);

  58. }

  59. }

  60. }

  61. // Reset the idle handler count to 0 so we do not run them again.

  62. pendingIdleHandlerCount = 0;

  63. // While calling an idle handler, a new message could have been

  64. livered

  65. // so go back and look again for a pending message without waiting.

  66. nextPollTimeoutMillis = 0;

  67. }

  68. }

  69. ......

  70. }

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

[java] view plaincopynativePollOnce(mPtr, nextPollTimeoutMillis);

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

到此,相信大家对"Android线程什么情况下会进入等待状态"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0