Android sendMessage vs Obtainmessage (Sendtotarget) comparison

Source: Internet
Author: User

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

Remember when you wrote this:

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 any pending messages     * before the current time. I t be is received in {@link #handleMessage},     * in the thread attached to this handler.     *       * @return Returns True if the message is 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)    {        return0);    }

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

/*    *     * 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.      */        Public final Message obtainmessage ()    {        return message.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 ()}, 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) {        = obtain ();         = h;         return m;    }

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 have not been set.      */     Public void Sendtotarget () {       <span style="background-color: #ff0000"> 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.

Deepen your reading:

Handler communication has been according to the original copy of the write for a long time, today only suddenly found, still really do not understand the parameters inside, tragedy Ah, in the handler.obtainmessage () the parameters are written like this:
Message android.os.Handler.obtainMessage (int what, int arg1, int arg2, Object obj)

Public final Message obtainmessage (int, int arg1, int arg2, Object obj)
SINCE:API Level 1
Same as Obtainmessage (), except that it also sets the "What," obj, Arg1,and arg2 values on the returned Message.

Parameters
What Value is assign to the returned Message.what field.
Arg1 Value to assign to the returned MESSAGE.ARG1 field.
Arg2 Value to assign to the returned MESSAGE.ARG2 field.
Obj Value to assign to the returned Message.obj field.

Returns
A message from the global Message pool.
I don't know what this means:
01-27 02:41:47.853:info/system.out (581): msg:{what=1001 when=6260065 arg1=1002 arg2=1003 obj=Main Thread Send for info! }
01-27 02:41:47.853:info/system.out (581): msg.what:1001
01-27 02:41:47.853:info/system.out (581): msg.arg1:1002
01-27 02:41:47.853:info/system.out (581): msg.arg1:1003
01-27 02:41:47.892:info/system.out (581): Msg.Obj:Main Thread Send for info!
What I've defined above, why is there that when, in most clicks:
01-27 02:50:39.522:info/system.out (581): msg:{what=1001 when=6791734 arg1=1002 arg2=1003 obj=Main Thread Send for info! }
01-27 02:50:39.522:info/system.out (581): msg.what:1001
01-27 02:50:39.532:info/system.out (581): msg.arg1:1002
01-27 02:50:39.532:info/system.out (581): msg.arg1:1003
01-27 02:50:39.572:info/system.out (581): Msg.Obj:Main Thread Send for info!
Only that when is the change, that when to take what value, it's operating mechanism and purpose is, please heroes can explain, thank you ...

and send it to removmessage.

Removemessages method in hander

My understanding:
1, the premise of this method is to call the previous sendemptymessagedelayed (0, time), meaning delay time to execute the handler msg.what=0 method;

2, in the delay time is not up to the premise, the implementation of Removemessages (0), the above handler msg.what=0 method is cancelled;

3, in the delay time has arrived, handler in the Msg.what=0 method has been executed, then executes Removemessages (0), does not work.

4, the method will be handler corresponding message queue messages emptied, through the msg.what to find the corresponding word.

5, when there is no message in the queue handler will not work, but not handler will stop, when there is a new message in the queue comes in, will continue processing execution.

Android sendMessage vs Obtainmessage (Sendtotarget) comparison

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.