Android 消息机制之Message
2017-03-15 13:59
106 查看
Android 消息机制之Message
Android
Android的消息机制中,Message的使用是很频繁的,处理消息,处理事件,处理其他我还没有探索的新世界。在Android的消息机制中,Message相当于承载消息的载体,Handler就是Message的处理者,而MessageQueue和Looper就是Message的搬运工。
在Message的内部有一个flag的标志位,用于标识当前Message的状态。有一个标志位是FLAG_IN_USE ,当Message在队列中,正在给Handler处理的时候,就会加上这个标志位,表示当前的Message正在使用,那么为什么要记录Message是否正在被使用呢。因为Message内部有一个对Message的回收机制。
因为Message作为Android系统消息的载体,那么若有大量消息需要处理的时候,就需要大量的Message,就需要不断的new Message吗,显然这样的很浪费时间的。所以Message内部弄了一个回收机制来管理Message。
Message的回收机制
和之前的文章一样,既然是回收机制,那么自然会想到的问题就是,怎么回收,回收之后怎么利用?我们在学习Handler的时候 ,Google推荐我们使用Message的静态方法obtain来得到一个新的Message实例。所以我们可以确定的是,回收之后的Message就是通过obtain来获取新的Message的。那我们来看看obtain的源码。我们能看到的Message有很多obtain的重载,但是都使用了下面的无参方法,所以我们只看这份代码就可以了
1.public static Message obtain() { 2. synchronized (sPoolSync) { 3. // sPool 就是Message的缓存池啦 4. if (sPool != null) { 5. // 下面几行代码就是链表的操作而已 6. Message m = sPool; 7. sPool = m.next; 8. m.next = null; 9. m.flags = 0; // clear in-use flag 10. // 缓存池的大小 11. sPoolSize--; 12. return m; 13. } 14. } 15. return new Message(); 16.}
obtain方法不难,而且代码很多,很容易理解吧。所以我们继续寻找Message是在哪里被回收的。我找到了下面的两个方法:
1./** 2. * Return a Message instance to the global pool. 3. * <p> 4. * You MUST NOT touch the Message after calling this function because it has 5. * effectively been freed. It is an error to recycle a message that is currently 6. * enqueued or that is in the process of being delivered to a Handler. 7. * </p> 8. */ 9.public void recycle() { 10. if (isInUse()) { 11. if (gCheckRecycle) { 12. throw new IllegalStateException("This message cannot be recycled because it " 13. + "is still in use."); 14. } 15. return; 16. } 17. recycleUnchecked(); 18.} 19. 20./** 21. * Recycles a Message that may be in-use. 22. * Used internally by the MessageQueue and Looper when disposing of queued Messages. 23. */ 24.void recycleUnchecked() { 25. // Mark the message as in use while it remains in the recycled object pool. 26. // Clear out all other details. 27. flags = FLAG_IN_USE; 28. what = 0; 29. arg1 = 0; 30. arg2 = 0; 31. obj = null; 32. replyTo = null; 33. sendingUid = -1; 34. when = 0; 35. target = null; 36. callback = null; 37. data = null; 38. 39. synchronized (sPoolSync) { 40. if (sPoolSize < MAX_POOL_SIZE) { 41. next = sPool; 42. sPool = this; 43. sPoolSize++; 44. } 45. } 46.}
可以看到上面的方法就是我们需要的啦,回收Message的方法。在recycle()一开始就判断了这个Message是否在使用,如果在使用就不回收啦。我之前一直以为Message的回收机制这些东西很高大上,其实在
recycleUnchecked里面只是对Message的所有成员变量清空,初始化一遍而已啊。最后缓冲池的大小再加一。
Message的其他东西
Message的源码不是很多,所以我还看了别的代码:1.public void copyFrom(Message o) { 2. this.flags = o.flags & ~FLAGS_TO_CLEAR_ON_COPY_FROM; 3. this.what = o.what; 4. this.arg1 = o.arg1; 5. this.arg2 = o.arg2; 6. this.obj = o.obj; 7. this.replyTo = o.replyTo; 8. this.sendingUid = o.sendingUid; 9. 10. if (o.data != null) { 11. this.data = (Bundle) o.data.clone(); 12. } else { 13. this.data = null; 14. } 15.}
这个方法是复制一个Message,除了Mesasge的data数据之外,其他都是浅拷贝的。我觉得这个在平时的开发中可能会写错的,所有今天写下来记录一下
有一点我还是没有想明白的,为什么Message要分同步和异步啊,有没有dalao留个言说说什么回事啊
相关文章推荐
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android的消息处理机制(图+源码分析)——Looper,Handler,Message
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- Android的消息处理机制:Message、Handlerhe和Looper源码解析
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- android的消息处理惩罚机制(图+源码解析)——Looper,Handler,Message
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android消息处理机制:Handler Thread Message Looper []
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android消息处理机制:Handler Thread Message Looper []
- (转)Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- [转] android的消息处理机制(图+源码分析)——Looper,Handler,Message
- Android的消息处理机制(图+源码分析)——Looper,Handler,Message
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- Android消息处理机制:Handler Thread Message Looper
- 解析Android消息处理机制 ——HandlerThread/Looper & MessageQueue & Message
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制