Comparison between Handler.sendmessage and Handler.obtainMessage.sendToTarget

Source: Internet
Author: User

Original address:

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

The first time I contacted Android handler at work, I didn't know how to focus on performance.

Remember when you wrote this:

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

This is not a bad writing, anyway, the function of the project was realized. (can also be used in performance)

Later when it was okay to look at the other methods of handler, I saw the Obtainmessage () this method. It's weird, I don't know why.

Originally above that paragraph code can realize handler function, why still appear he, later Baidu Google A, everybody say what performance has difference

Class of ..... The results can be imagined (most of the articles are you copy me I copied you, technology is like this out of. Despise the copying of other blog posts and

A person who is not famous for reproducing the source). So I went to see if the source code can see some good description.

Message msg = Handler.obtainmessage (); msg.what = Xxx;msg.arg1  = xxx;msg.arg2  = xxx;msg.obj    

Look at these two pieces of code is actually the method is different, the parameters are the same. But why do the results have to be separated out so many ways?

Go to the source code to see what it is!

First go to see SendMessage () This method .... It calls the SendMessage (Message msg) in handler.

Source fragment 1 is as follows:

/**     * 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.     */    publicfinal booleansendMessage(Message msg)    {        returnsendMessageDelayed(msg, 0);    }

Look again Handler.obtainmessage () source fragment 2 is as follows:

/**    * 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.    */     publicfinal Message obtainMessage()   {       returnMessage.obtain(this);   }

  

The above two paragraphs are handler inside the method, but in fragment 1 we can see that the message is we passed as a parameter, fragment 2 is our

The message helps us, it calls the obtain (Handler h) method, and then we call the Sendtotarget () method in the message.

Take a look at Message.obtain (Hanlder h) Source code snippet 3 is as follows:

/**   * 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.   */  publicstaticMessage obtain(Handler h) {      Message m = obtain();      m.target = h;      returnm;  }

Look again Sendtotarget () source code fragment 4 is as follows:

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

The target here is Handler,sendtotarget () and the SendMessage method of calling Handler ...

See here maybe some people are very puzzled, so turn around, turned a circle how to return to handler SendMessage method? So, the performance comparison.

Is there any other evidence? (is the method called more performance low?) There may be a reason for this, but it's almost impossible to consider the performance loss.

So where does the comparative evidence of performance come from?

In fact, the careful classmate has seen, pay attention to the source of comments,

/**
* 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 it handler set to this instance (Message.target = = this).
* If you don ' t want this facility, just call Message.obtain () instead.
*/

Here our message is not created by ourselves, but taken from Messagepool, eliminating the overhead of creating an object to request memory ....

It should be clear to all of you here. So try to use the Message msg = Handler.obtainmessage () in the form of

Build a message, do not own the new message as soon as the message is generated you use obtainmessage or sendMessage efficiency impact

It's not big. At the same time, we should also pay attention to the performance of the time to find the location, such as the performance of the problem is not in the call Obtainmessage and Sen

The Dmessage method, but instead calls them on the creation of the object before the problem.

Comparison between Handler.sendmessage and Handler.obtainMessage.sendToTarget

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.