The difference between Android message and Obtainmessage

Source: Internet
Author: User

a few days ago the need to implement an Ethernet function saw the EthernetSourcesection, see the Source section in the message processing, found that there are some different places:  When I am dealing with messages:  1, first create Handler object:  private Handler Handler = new Handler () {public void Handlemessage (message msg) {switch (msg.what) {case 0: break;  default:break;}};};  2, then message processing:  private void Testhandlermessage () {Message msg = new Message (); msg.what = 1;handler.sendmessage ( msg);}   But I see other people. The source code section deals with this:   1, first create Handler object:  private Handler Handler = new Handler () {public void Handlemessage (Message msg) {switch (msg.what) {case 0: break;  default:break;}};};   2, then message processing:  private void Obtainmessagetest () {Message msg = Handler.obtainmessage (); msg.what = 1; Handler.sendmessage (msg);}   By comparison we will find that the two ways to get the instance of the message is different, so I read the source code, sure enough:  view obtainmessage () source code:  diagram 1:    Enter obtain method:   Figure 2:  Re-enter Objtain method:  diagram 3:    Enter message method:  diagram 4:   Summary:  The comments in the obtain method in Figure 3 above make it very clear that a new message instance is returned from the entire Messge pool, which is used in many cases because it avoids assigning new objects   If it is this person, It is possible to avoid creating objects by calling the Obtainmessage method to get the message object, thereby reducingLess memory overhead.

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.