你的位置:首页 > 软件开发 > 操作系统 > 好玩的Handler

好玩的Handler

发布时间:2016-11-14 15:00:30
Android提供了Handler和Looper来满足线程间的通信;Handler和Activity的任务栈不同,它是先进先出原则;Handler:你可以构造Handler对象来与Looper沟通,以便push新消息到MessageQueue里,或者接口Looper从Messag ...

好玩的Handler

  1. Android提供了Handler和Looper来满足线程间的通信;
  2. Handler和Activity的任务栈不同,它是先进先出原则;
  3. Handler:你可以构造Handler对象来与Looper沟通,以便push新消息到MessageQueue里,或者接口Looper从MessageQueue取出的消息;
  4. Looper类用来管理特定线程内对象之间交换Message;
  5. 一个线程可以产生一个Looper对象,由他来管理此线程的MessageQueue(消息队列);
  6. MessageQueue:用来存放线程放入的消息;
  7. 每一个消息都需要制定的Handler来处理,通过Handler创建消息便可以完成此功能.Android引入了消息池.Handler创建消息时首先查询消息池中是否有消息存在,如果有,则直接取出,如果没有,则重新初始化一个消息实例.
  8. 使用消息池的好处是:消息不被使用时,并不作为垃圾回收,而是放入消息池中,可供下次Handler创建消息时使用.消息池提高了消息对象的复用,减少系统垃圾回收的次数.Message.obtain()来获取消息,最大数为50;
  9. 好玩的Handler 
  10. 通过查询源码后发现,Message提供了诸如以下的变量
    1. Message的recycleUnchecked()方法
    2. void recycleUnchecked(){
    3. // Mark the message as in use while it remains in the recycled object pool.
    4. // Clear out all other details.
    5. flags = FLAG_IN_USE; //int
    6. what =0; // int
    7. arg1 =0; // int
    8. arg2 =0; // int
    9. obj =null; // Object
    10. replyTo =null; // Messenger 信使,信差
    11. sendingUid =-1;
    12. when =0; // long
    13. target =null; // Handler
    14. callback =null; // Runable
    15. data =null; // Bundle
    16. synchronized(sPoolSync){
    17. if(sPoolSize < MAX_POOL_SIZE){
    18. next = sPool;
    19. sPool =this;
    20. sPoolSize++;
    21. }
    22. }
    23. }
  11. 重新优化下Handler

     

    海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

    原标题:好玩的Handler

    关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录