Android asynchronous message processing mechanism prev (b): In-depth understanding of message pool

Source: Internet
Author: User

Copyright notice: This article from Springer's blog, reproduced please be sure to indicate the source.

In a joint discussion of the threadlocal, this article we look at the message that is often mentioned in the case of a pool of what is going on, talk less, get to the point.

For a slightly experienced developer, we get a message object using the following code when we use handler to send an asynchronous message to get message:

1 Message msg = Mhandler.obtainmessage ();

Instead of just the new one:

1 New Message ();

The main difference between the two is the use of the buffer pool concept above, if there is idle in the pool to use, no new message. The latter does not have this mechanism, directly new one to use.

Next, let's analyze how this cache pool is implemented.

Message Cache Pool Source Analysis

Handler the Obtainmessage () method in essence or the message in the call Obtain () method, here directly read the message in the Obtain () method Source:

1  Public StaticMessage obtain () {2         synchronized(spoolsync) {3             if(SPool! =NULL) {4Message m =SPool;5SPool =M.next;6M.next =NULL;7m.flags = 0;//Clear In-use Flag8spoolsize--;9                 returnm;Ten             } One         } A         return NewMessage (); -}

Line 3rd first determine whether spool is NULL, if NULL then directly execute 12 lines directly new a message returned, the entire method is finished, spool is what ghost? Defined as follows:

1 Private Static Message SPool;

See, is a Message object, spool is actually the equivalent of a head pointer, point to the cache pool in the first cached message, the analysis of all will naturally understand its role.

Continue down the analysis.

SPool is not null to enter 4-9 lines of code logic,sPool NULL indicates that there is an idle message in the cache pool.

line 4th records spool, and line 9th returns m as the return value of the entire method, that is, to return an idle message in the cache pool for external use without additional memory overhead.

line 5th spool points to the next cached object.

Line 6th M.next NULL, and here the most important logic is over, maybe you are still blind, what is this ah, actually very simple the cache pool of message is actually a data structure-one-way linked list.

The next step is to show me the power of drawing, and nothing is a diagram that cannot be solved:

Assume that there are three idle message:message1,message2,message3 in the cache pool at this time. Spool first point to head message1.

The 4th line of code is equivalent to the step ① in the diagram, nothing to explain.

The execution of line 5th is equivalent to the steps in the figure ②,spool point to the next cached message, which is message2.

The 6th line of code is equivalent to the steps in the figure ③,message1 and Message2 disconnected.

How to explain it is clear, in fact, it is very simple.

7,8 line is to clear the mark and change the size of Spoolsize, spoolsize to record the number of elements in the cache pool, the buffer pool size is limited, beyond the specified size can not be added to the inside.

Obtain () Summary

Well, the main logic is done,obtain () The main logic is to determine whether there is an idle message in the cache pool, if it exists, return the header message, and the pointer points to the next idle message, The message of the head is then disconnected from the linked list. If no idle message exists, the direct new one returns directly.

The logic above is the operations taken from the cache pool, and when is it stored in the cache pool? We continue to analyze downward.

The recycle () method in the message class is used to reclaim the exhausted Mesage, and this message is received in the cache pool, is this true? Let's look at the source code to find out:

1  Public voidRecycle () {2         if(Isinuse ()) {3             if(gcheckrecycle) {4                 Throw NewIllegalStateException ("This message cannot is recycled because it"5+ "is still on use.");6             }7             return;8         }9 recycleunchecked ();Ten}

The recycle method primarily determines whether the current message is in use, throws an exception if it is being used, calls the recycleunchecked () method without being used, and then looks at recycleunchecked ():

1 voidrecycleunchecked () {2         //Mark the message as in use while it remains in the recycled object pool.3         //Clear all other details.4Flags =Flag_in_use;5what = 0;6Arg1 = 0;7arg2 = 0;8obj =NULL;9ReplyTo =NULL;TenSendinguid =-1; OneWhen = 0; Atarget =NULL; -callback =NULL; -data =NULL; the  -         synchronized(spoolsync) { -             if(Spoolsize <max_pool_size) { -Next =SPool; +SPool = This; -spoolsize++; +             } A         } at}

4-14 The main thing is to clear some current tags.

17 Rows,max_pool_size is the maximum number of cached message caches in the specified cache pool, and continues down if the current number of stores is less than the specified maximum number of caches.

18,19 line is the focus, but also to show my strong drawing ability, a picture to solve:

For example, the list in the cache pool for message2,message3,spool points to the head message2.

At this point, message1 is recycled to perform the recycle () operation. Final execution to recycleunchecked () of 18, 19 line logic.

18 Line: The equivalent of the message1 in the diagram of the next pointer to spool, when spool point to Message2, that is, Message1 and Message2 link, that is, the ① operation in the diagram.

Line 19: Spool repositions to the currently recycled message, here is message1. Equivalent to the ② operation in the diagram

Recycle () Summary

Well, to here recycling is finished, the most important is 18, 19 lines of Logic,recycle () The most important is to put the current message into the cache Chiling head.

To this, I want to explain the end, the core is the data structure of the single link list of the actual application, if one-way chain list you are very familiar with, I think here should be very easy to understand, even if not very familiar, think carefully also should be able to understand, here is not difficult to understand.

This is the end of this article, the next Android asynchronous message processing mechanism is fully resolved.

Android asynchronous message processing mechanism prev (b): In-depth understanding of message pool

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.