原文地址:

http://www.cnblogs.com/android007/archive/2012/05/10/2494766.html

话说在工作中第一次接触android 的Handler 的时候,不知道怎么去关注性能。

记得当时这么写的:

Message msg = new Message()
msg.what = xxx;
msg.arg1 = xxx;
msg.arg2 = xxx;
handler.sendMessage(msg);

这样写也没有绝得不好,反正当时项目的功能实现了。(性能上还可以)

后来没事的时候看了看handler 的其他的方法,就看到了obtainMessage()这个方法.很奇怪,不知道为何还要出来的方法

本来上面的那段code 就能实现handler 的功能了,为什么还要出现他,后来百度google 一把,大家说 什么性能上有差别之

类的。。。。。结果可想而知(大部分的文章都是你抄我的我抄你的,技术就是这样出来的。。鄙视这种抄写别人博客文章而

不著名转载出处的人)。于是我就去看源码能否看到一些好的描述。

Message msg = handler.obtainMessage();
msg.what = xxx;
msg.arg1 = xxx;
msg.arg2 = xxx;
msg.obj = xxx; ....................

看看这两段代码其实就是方法不一样 ,参数都一样。但是为何实现的效果一样还要分离出来这么多方法呢?

到源码去看个究竟吧!

先去看sendMessage()这个方法。。。。它调用的是Handler 中的sendMessage(Message msg)

源码 片段1  如下:

/**
     * Pushes a message onto the end of the message queue after all pending messages
     * before the current time. It will be received in {@link #handleMessage},
     * in the thread attached to this handler.
     
     * @return Returns true if the message was successfully placed in to the
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     */
    public final boolean sendMessage(Message msg)
    {
        return sendMessageDelayed(msg, 0);
    }

 再看handler.obtainMessage()源码 片段2 如下:

/**
    * Returns a new {@link android.os.Message Message} from the global message pool. <br>   * More efficient than creating and allocating new instances. <br>    * The retrieved message has its handler set to this instance    <br>   * (Message.target == this).
    * If you don't want that facility, just call Message.obtain() instead.
    */
   
 
   public final Message obtainMessage()
   {
       return Message.obtain(this);
   }

  

上面这两段都是Handler 里面的方法,不过在片段1  我们可以看到Message是我们作为参数传过去的,片段2的则是我们

Message帮我们处理,它调用obtain(Handler h)方法,之后我们还要调用Message中sendToTarget()这个方法。

看一下Message.obtain(Hanlder h) 的源码  代码片段3如下:

/**
   * Same as {@link #obtain()}, but sets the value for the <em>target</em> member on the Message return   * ed.
   * @param h  Handler to assign to the returned Message object's <em>target</em> member.
   * @return A Message object from the global pool.
   */
  public static Message obtain(Handler h) {
      Message m = obtain();
      m.target = h;
 
      return m;
  }

  再看 sendToTarget() 源码     代码片段4 如下:

/**
   * Sends this Message to the Handler specified by {@link #getTarget}.
   * Throws a null pointer exception if this field has not been set.
   */
  public void sendToTarget() {
     <span style=""> target.sendMessage(this);</span>
  }

  这里的target就是handler,sendToTarget()又在调用handler的 sendMessage 方法了。。。

看到这里也许有的人很疑惑,这样转来转去,转了一圈怎么又回到Handler的 sendMessage方法了?那么性能比较一说

还有其他的证据么?(难道调用的方法多性能就低么?也许有这么一点原因,不过这里几乎可以不考虑这一点性能损耗的)

那么性能的比较证据应该从哪里找呢?

  其实细心的同学已经看到了,注意看源码的注释,

/**
* Returns a new {@link android.os.Message Message} from the global message pool. More efficient than
* creating and allocating new instances. The retrieved message has its handler set to this instance (Message.target == this).
* If you don't want that facility, just call Message.obtain() instead.
*/

这里我们的Message 已经不是 自己创建的了,而是从MessagePool 拿的,省去了创建对象申请内存的开销。。。。。

到这里大家应该都明白了。所以以后使用的时候尽量使用 Message msg = handler.obtainMessage();的形式创

建Message,不要自己New Message 至于message产生之后你使用obtainMessage 或者是 sendMessage 效率影响

并不大.同时我们也要注意以后谈论性能的时候要找准位置,譬如这里性能的问题不是在调用 obtainMessage 和 sen

dMessage 的方法上,而是调用他们之前对象的创建问题上。

Handler.sendMessage 与 Handler.obtainMessage.sendToTarget比较的更多相关文章

  1. Handler sendMessage 与 obtainMessage (sendToTarget)比较

    转自:http://iaiai.iteye.com/blog/1992196 obtainmessage()是从消息池中拿来一个msg 不需要另开辟空间new new需要重新申请,效率低,obtian ...

  2. Handler sendMessage 与 obtainMessage (sendToTarget)

    这篇文章讲的很好: http://www.cnblogs.com/android007/archive/2012/05/10/2494766.html 两种用法: 1. private void se ...

  3. Android sendMessage 与 obtainMessage (sendToTarget)比较

    话说在工作中第一次接触android 的Handler 的时候,不知道怎么去关注性能. 记得当时这么写的: Message msg = new Message() msg.what = xxx; ms ...

  4. sendMessage 与 obtainMessage (sendToTarget)比较

    我们平时在做到多线程问题的时候可能利用Handler去传递Message,其中,经常使用的就是 1.new Handler().obtainMessage().sendToTarget(); 2.ne ...

  5. Handler类和Handler,Loop,MessageQueue的工作原理

    原文地址:http://blog.csdn.net/xiyangyang8/article/details/50754771 Handler类的作用主要有两种: 1.在新启动的线程中发送消息. 2.在 ...

  6. Handler消息传递机制——Handler类简洁

    Handler类的主要作用有两个: 在新启动的线程中发送消息. 在主线程中获取.处理消息. 上面的说法很简单,只要分成两步即可:在新启动的线程中发送消息:然后在主线程上获取.并处理消息.但这个过程涉及 ...

  7. Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别?

    一个帖子的整理: Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别如果你不带参数的实例化:Handler ...

  8. new Handler()和new Handler(Looper.getMainLooper())的区别

    一个帖子的整理: Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别如果你不带参数的实例化:Handler ...

  9. W/MessageQueue: Handler (android.os.Handler) {4241f8f8} sending message to a Handler on a dead thread

    缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的 稍微纤细一点儿的信息是: Handler (andro ...

随机推荐

  1. 【原创】开源.NET排列组合组件KwCombinatorics使用(二)——排列生成

           本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...

  2. iOS开发之手势识别

    感觉有必要把iOS开发中的手势识别做一个小小的总结.在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextVi ...

  3. 构建自己的PHP框架--创建组件的机制

    在之前的博客中,我们完成了基本的Model类,但是大家应该还记得,我们创建数据库的pdo实例时,是hard好的配置,并且直接hard在Model类中. 代码如下: public static func ...

  4. 使用纯前端JavaScript 实现Excel IO

    公司最近要为某国企做一个**统计和管理系统, 具体要求包含 Excel导入导出 根据导入的数据进行展示报表 图表展示(包括柱状图,折线图,饼图),而且还要求要有动画效果,扁平化风格 Excel导出,并 ...

  5. mysql表名查询sql

    select table_schema,table_name,engine from information_schema.tables where table_schema not in('info ...

  6. C# 打印PDF文件之使用不同打印机打印所有页面或部分页面

    C# 打印PDF文件之使用不同打印机打印所有页面或部分页面 最近在逛国外各大编程社区论坛的时候,发现很多人都在问一个关于PDF文件打印的问题:打印时如何选择非默认打印机并设置打印页面的范围.而一般情况 ...

  7. JS魔法堂:Data URI Scheme介绍

    一.前言 上周五公司内部的Any Topic Conf.上我和同事们分享了这个主题,有同事说这个有用,有同事说这个没啥用,后来还延伸到网站性能的话题上,大家讨论的激烈程度让我觉得这次选题还不错.本篇先 ...

  8. Hadoop入门学习笔记---part2

    在<Hadoop入门学习笔记---part1>中感觉自己虽然总结的比较详细,但是始终感觉有点凌乱.不够系统化,不够简洁.经过自己的推敲和总结,现在在此处概括性的总结一下,认为在准备搭建ha ...

  9. 小白Linux入门 二

    参考: http://edu.51cto.com/lesson/id-11222.html CPU中有计算单元 控制单元.它通过桥接芯片与存储器进行匹配 其中北桥是高速 南桥是低速 包括IDE USB ...

  10. 我的runtime学习笔记

    0.简介: OC方法不同于C语言函数,属于动态调用过程,在编译的时候并不能决定真正调用哪个函数,只有在真正运行的时候才会根据函数的名称找到对应的函数来调用. 至于其他理论上的东西不必讲太多,编程讲的就 ...