Message for the Android object pool

Source: Internet
Author: User

In the Android system, message is often used in the communication between multiple threads, passing through the handler to pass through the messages between them. Today we are talking about the message feature in Android: Object pooling.

On Android, Google has an English explanation for the message:

* <p class= "Note" >while the constructor of Message is public, the best-by-get
* One of these is to call {@link #obtain message.obtain ()} or one of the
* {@link handler#obtainmessage handler.obtainmessage ()} methods, which'll pull
* Them from a pool of recycled objects.</p>

This means that although the message is constructed to be open to the public, it is best to use this method Obtainmessage () when we want to get a message, and this method can get the message you need from a reusable object pool. .

With the above meaning, we can know that the original message in fact maintains a pool of objects, when we use the end of a message, this message is likely to be stored in this object pool, In other words, there is a pool of objects in the message that holds the message we used. In fact, through the code, we can know that when the object pool does not have the message we need, it will automatically new a message. So don't worry obtainmessage () can't get a message.

Now look at how the object pool of the Android message is designed. In fact, the object pool inside the message is implemented by maintaining a single list of items! See below to find out.

There are several declarations and definitions of variables in the message:

    /*package*/Message Next;    Private static Final Object Spoolsync = new Object ();    private static Message SPool;    private static int spoolsize = 0;    private static final int max_pool_size = 50;
Next, point to the next available message;

Spoolsync is mainly used for synchronization, to ensure multithreading security;

Message SPool; Here the SPool is used to represent the currently available message, note that this is a static decorated message,

Spoolsize is used to record how many Messagel the current message object pool has.

Finally explain: In fact, the message in the maintenance of a single list structure to hold the message object!next forever to point to the next Message object, The spool here is obviously the table header of the list. This allows you to traverse the message within the entire object pool through the header sequence.

As I said above, when this method Obtainmessage () to get a message, it will be found in the object pool (this is the one-way list to find), if the object pool does not have the message we need, it will be a new message. Let's take a look at the code to prove what I said.

    public static Message obtain () {        synchronized (spoolsync) {            if (sPool! = null) {                Message m = sPool;                SPool = M.next;                M.next = null;                spoolsize--;                return m;            }        }        return new Message ();    
As you can see from the code above, if the list is not NULL, take the message of the linked list header and move the list head. No, it will be new Message ().

The last thing to say is, when a message is run out, how do you join the list? With the file Message.java, you will find a way to define a public in the message:

    public void Recycle () {        clearforrecycle ();        Synchronized (spoolsync) {            if (Spoolsize < max_pool_size) {                next = SPool;                SPool = this;                spoolsize++;}}}    
The above code can tell that the Recycle method is actually adding this message to the list (added to the head). So when is this method called?

In fact, when sending a message through handler, it is actually the message based on his delay to determine his position, add it to a queue inside the MessageQueue, operation this MessageQueue not only this handler , and this handler looper.

In fact, every handler need a looper, often we create a handler when actually does not indicate his looper, but he will automatically get to create this handler thread looper to act as their own looper, so, In Android, handler is often not indicated by the looper, in fact, is the main thread (which is what we often say UI thread) Looper.

Just now we said, Handler Looper actually also operate MessageQueue, actually a looper need a MessageQueue, my personal understanding is: Looper is the purpose of Operation MessageQueue, and handler controls the Looper and MessageQueue.

In fact, this: handler to MessageQueue inside to add the message to be processed, and looper from the MessageQueue to take out the message to be processed to start the processing process.

In Looper, it is actually by calling the method Loop () to take out the message that needs to be processed, and processing, the processing time will call this message recycle (), so that the finished message is added to the object pool!

Message for the Android object 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.