码迷,mamicode.com
首页 > 移动开发 > 详细

Android Handler研究(1)

时间:2017-10-26 11:36:05      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:阻塞   prepare   ui线程   pip   linu   对象   执行   pipe   nat   

a. 解决问题
  跨线程通信(UI线程)
b. 用途
  1. 延时执行message或runnable
  2. 子线程执行耗时操作
c. 原理
  Message: 消息实体
  MessageQueue: 消息队列
  Looper: 轮询消息队列
d. 注意点
  1. UI线程Handler如何初始化的
  ActivityThread的static main方法中执行Looper.prepareMainLooper();
  2. 任意线程实现消息队列
    new Thread(){

      run(){
      //1. 准备Looper对象
      Looper.prepare();
      //2. 子线程创建Handler
      handler = new Handler(){
        handlerMessage(){
        }
      }
      //3. 轮询方法
      Lopper.loop();
      }

    }.start();  

3. 为什么主线程不会因为Looper.loop()死循环卡死?
  利用Linux管道(Pipe/epoll),简单说就是在主线程的MessageQueue没有消息时,便阻塞在loop的messageQueue.next()中的nativePollOnce()方法里.
  所以说,主线程大多数时候处于休眠状态,不会消耗大量cpu资源

Android Handler研究(1)

标签:阻塞   prepare   ui线程   pip   linu   对象   执行   pipe   nat   

原文地址:http://www.cnblogs.com/qishuai/p/7735486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!