Today, obtain() and recycle() in Message.
The original address: blog.csdn.net/xmh19936688…
【Android advanced learning video collection 】Handler source interview key questions parsing (all) (video explanation is more comprehensive and more easy to understand a bit)
The body of the
-
The Android buffet Handler messaging mechanism fully resolves the context of obtain and Recycle in a Message
-
Provide obtain
-
Recovery and recycle
Provide obtain ()
In all overloaded methods of obtain, the first line is Message m = obtain(); That is, the method that calls the null parameter. Let’s take a look at the empty parameter method
public static Message obtain() { synchronized (sPoolSync) { if (sPool ! = null) { Message m = sPool; sPool = m.next; m.next = null; m.flags = 0; // clear in-use flag sPoolSize--; return m; } } return new Message(); }Copy the code
Obviously, this is a synchronous method:
-
SPoolSync is the lock object
-
This Object is initialized when it is defined. Private static final Object sPoolSync = new Object();
-
Then they read and write nothing.
-
Then sPool, followed by Message m = sPool; sPool = m.next;
-
So obviously, this is a linked list structure. SPool points to the current message and next points to the next message.
Before explaining this code, two things need to be made clear:
-
SPool is declared private static Message sPool; ; Next is declared /package/ Message next; .
-
That is, the former is shared by all examples of the class, while the latter is shared by each instance.
For now, to make it easier to understand, Message is abstracted as a linked list node structure in C, with the pointer field being the next field that points to the next Message, and the rest as data fields.
Assume the initial state of the list is as follows
Execute Message m = sPool; It becomes the following image
Continue sPool = m.ext;
Then m.ext = null;
The next m. lags = 0; sPoolSize–; return m; It means that the object m points to has been removed from the list and returned.
Recycling recycle ()
Then take a look at when sPoolSize increases. The recycle-unchecked () method and recycle-unchecked () method are just the way to go. The former is invoked by the developer for collection, while the latter performs the collection operation. Take a look at what recycling does:
void recycleUnchecked() { // Mark the message as in use while it remains in the recycled object pool. // Clear out 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++; }}}Copy the code
The first half of the paragraph needless to say, is obviously “reset” the object of each field. The second half is a synchronous code segment, again illustrated in the diagram (if the current code is message.recycle(), then the message object needs to be recycled). Assume the current linked list is as follows:
Execute next = sPool;
Perform sPool = this;
It is now clear that the Message class itself organizes a stack buffer pool. And use the methods obtain() and recycler() to remove and recycle.
Supplement:
The argument to handler.obtainMessage() is written like this:
Message android.os.Handler.obtainMessage(int what, int arg1, int arg2, Object obj)public final Message obtainMessage (int what, int arg1, int arg2, Object obj)
Since: API Level 1Same as obtainMessage(), except that it also sets the what, obj, arg1,and arg2 values on the returned Message.
Parameters
what Value to 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.
Copy the code
The difference between obtainMessage and new Message in Handler:
-
Obtainmessage () takes a MSG from the message pool without creating space
-
New needs to be re-applied, which is inefficient. Obtianmessage can be recycled.
The source code says:
//use Handler.obtainMessage(),instead of msg = new Message(); //because if there is already an Message object,that not be used by //any one ,the system will hand use that object,so You don't have to//create and object and allocate memory. // It is also another example of object recycling and reusing in android. Message msg = mHandler.obtainMessage(); msg.what = UPDATE_LISTVIEW; MSG. Obj = current + "/" + total + "; //this method is called from worker Thread,so we cannot update UI from here. msg.sendToTarget();Copy the code
Take a look at the following code:
Message msg = handler.obtainMessage(); msg.arg1 = i; msg.sendToTarget(); Message msg=new Message(); msg.arg1=i; handler.sendMessage(msg);Copy the code
conclusion
- The first way is that message gets from the Handler class so that you can send messages directly to that handler object
- The second way is to send a message directly by calling handler’s send message method.
The last
This article is in the open source project: github.com/xieyuliang/… Has been included, which contains different directions of self-study programming routes, interview questions set/interviews, and a series of technical articles, etc., resources continue to update…