"Android Buffet" handler message mechanism completely parse (a) the ins and outs of obtain () and recycle () in message

Source: Internet
Author: User

"Android Buffet" handler message mechanism completely parse (a) the ins and outs of obtain () and recycle () in message

    • Android Buffet Handler message mechanism completely parse the ins and outs of obtain and recycle
      • Provide obtain
      • Recycling Recycle

Offer obtain ()

In all overloaded methods of obtain, the first row is the Message m = obtain(); method that invokes the null parameter.
Let's take a look at this null parameter method

public   Static  Message obtain  () {synchronized  ( Spoolsync) {if  (sPool! = null ) {Me            Ssage m = sPool;            SPool = M.next;            M.next = null ; M.flags = 0 ;            //clear In-use flag  spoolsize--;        return  m; }} return  new  Message ();}  

Obviously, this is a synchronous method, that is, the sPoolSync lock object, which is initialized when it is defined private static final Object sPoolSync = new Object(); , and then is read-only and not written.
Then there is, and it sPool Message m = sPool;sPool = m.next; is clear that this is a list structure. sPoolpoint to the current message, pointing to the next next message.
Before explaining this code, you need to be clear about two points: sPool declare as private static Message sPool; ; next /*package*/ Message next; That is, the former is shared for all examples of the class, and the latter is for each instance.
Now for the sake of understanding, we will be Message abstracted as a list node struct in C, where the pointer field is the field for the next message next , and the others are treated as data fields.
Let's say the list is initially state as follows

Execution Message m = sPool; becomes

Continue to sPool = m.next;

Then m.next = null;

The next step is m.flags=0;sPoolSize--;return m; to indicate that the object m is pointing to has been removed from the list and returned.

Recycled Recycle ()

And then see sPoolSize when it's self-increasing. Retrace can find recycle() methods and recycleUnchecked() methods. The former is for developer calls to recycle, which performs the recycle operation. Let's see what the recycling operations are all about:

voidRecycleunchecked () {//Mark the message asinchUse whileIt remainsinchThe Recycled object pool.//Clear all other details.    Flags = Flag_in_use; what =0; Arg1 =0; Arg2 =0; obj =NULL; ReplyTo =NULL; Sendinguid =-1; when=0; target =NULL; callback =NULL; data =NULL; Synchronized (Spoolsync) {if(Spoolsize < Max_pool_size)            {next = SPool; SPool = This;        spoolsize++; }    }}

The first half of the paragraph does not have to be said, obviously the "reset" to change the object's fields. The second half is a synchronous code snippet, which is also explained by the diagram (assuming the current code is message.recycle() , the object that needs to be recycled message ).
Suppose the current list is as follows:

Execution next=sPool;

Implementation sPool=this ;

It is now clear that the message class itself organizes the buffer pool of a stack structure. and use obtain() methods and recycler() methods to remove and put.

Android Buffet Handler message mechanism to fully parse (a) the ins and outs of obtain () and recycle () in message

Related Article

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.