日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android應(yīng)用程序消息處理機(jī)制(Looper、Handler)分析(6)

調(diào)用以下函數(shù)的時(shí)候,有可能會讓線程進(jìn)入等待狀態(tài)。

什么情況下,線程會進(jìn)入等待狀態(tài)呢?

兩種情況,一是當(dāng)消息隊(duì)列中沒有消息時(shí),它會使線程進(jìn)入等待狀態(tài);;二是消息隊(duì)列中有消息,但是消息指定了執(zhí)行的時(shí)間,而現(xiàn)在還沒有到這個時(shí)間,線程也會進(jìn)入等待狀態(tài)。

消息隊(duì)列中的消息是按時(shí)間先后來排序的,后面我們在分 析消息的發(fā)送時(shí)會看到。

這個函數(shù)最關(guān)鍵的地方便是從消息隊(duì)列中獲取下一個要處理的消息了,即MessageQueue.next函數(shù),它實(shí)現(xiàn)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. }

執(zhí)行下面語句是看看當(dāng)前消息隊(duì)列中有沒有消息:

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

這是一個JNI方法,我們等一下再分析,這里傳入的參數(shù)mPtr就是指向前面我們在JNI層創(chuàng)建的NativeMessageQueue對象了,而參數(shù) nextPollTimeoutMillis則表示如果當(dāng)前消息隊(duì)列中沒有消息,它要等待的時(shí)候,for循環(huán)開始時(shí),傳入的值為0,表示不等待。


網(wǎng)頁標(biāo)題:Android應(yīng)用程序消息處理機(jī)制(Looper、Handler)分析(6)
當(dāng)前路徑:http://www.dlmjj.cn/article/djjhehp.html