Android线程什么情况下会进入等待状态-成都快上网建站

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

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

10年的温县网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整温县建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“温县网站设计”,“温县网站推广”以来,每个客户项目都认真落实执行。

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

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

这个函数最关键的地方便是从消息队列中获取下一个要处理的消息了,即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. nativePollOnce(mPtr, nextPollTimeoutMillis); 

  10. synchronized (this) { 

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

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

  13. final Message msg = mMessages; 

  14. if (msg != null) { 

  15. final long when = msg.when; 

  16. if (now >= when) { 

  17. mBlocked = false; 

  18. mMessages = msg.next; 

  19. msg.next = null; 

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

  21. return msg; 

  22. } else { 

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

  24. } else { 

  25. nextPollTimeoutMillis = -1; 

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

  27. if (pendingIdleHandlerCount < 0) { 

  28. pendingIdleHandlerCount = mIdleHandlers.size(); 

  29. if (pendingIdleHandlerCount == 0) { 

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

  31. mBlocked = true; 

  32. continue; 

  33. if (mPendingIdleHandlers == null) { 

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

  35. ]; 

  36. mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers); 

  37. // Run the idle handlers. 

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

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

  40. final IdleHandler idler = mPendingIdleHandlers[i]; 

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

  42. boolean keep = false; 

  43. try { 

  44. keep = idler.queueIdle(); 

  45. } catch (Throwable t) { 

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

  47. if (!keep) { 

  48. synchronized (this) { 

  49. mIdleHandlers.remove(idler); 

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

  51. pendingIdleHandlerCount = 0; 

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

  53. livered 

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

  55. nextPollTimeoutMillis = 0; 

  56. ...... 

  57. }

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

[java] view plaincopynativePollOnce(mPtr, nextPollTimeoutMillis);

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

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


新闻标题:Android线程什么情况下会进入等待状态
转载源于:http://kswjz.com/article/iioscs.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流