Message, messagequeue, logoff, Handler

Source: Internet
Author: User

Message, messagequeue, logoff, Handler
I. Several key concepts
1. messagequeue: a data structure. It is a message queue where messages are stored. Each thread can have at most one messagequeue data structure.
Messagequeue is not automatically created when a thread is created. A logoff object is usually used to manage the messagequeue of the thread. When the main thread is created, a default logoff object is created, and a message queue is automatically created when the logoff object is created. Other non-main threads do not automatically create logoff. When necessary, they are implemented by calling the prepare function.

2. message: the message object, the object stored in the message queue. A Message Queue contains multiple messages. Obtain the object of the message instance, usually using the static method obtain () in the message class. This method has multiple overloaded versions to choose from; it does not necessarily create a new instance directly. Instead, it first checks whether there is any available message instance from the message pool. If yes, It is retrieved and returned. If no message instance is available in the message pool, a message object is created with the given parameters. When removemessages () is called, the message is deleted from the message queue and put into the message pool. In addition to the above method, you can also get a message instance through the obtainmessage () of the handler object.

3. Logoff: the manager of messagequeue. Each messagequeue cannot be separated from the logoff object. The creation of the logoff object is implemented through the prepare function. At the same time, each logoff object is associated with a thread. You can obtain the logoff object of the current thread and create a messagequeue object. Besides the default logoff for the main thread, other threads do not have messagequeue objects by default. Therefore, messages cannot be accepted. If you need to accept it, define a looper object (through the prepare function) by yourself, so that the thread has its own Looper object and messagequeue data structure.

Logoff extracts the message from messagequeue and submits it to handlemessage of handler for processing. After the processing is complete, call message. Recycle () to put it into the message pool.

4. Handler:
The handler of the message handler is responsible for encapsulating the information to be passed into a message and implementing it by calling the obtainmessage () of the handler object;
Send the message to logoff, which is implemented by sendmessage () of the handler object. Then, logoff puts the message into messagequeue.
When the logoff object sees that messagequeue contains a message, it is broadcasted. After the handler object receives the message, it calls the handlemessage () method of the corresponding handler object to process it.
Each thread has a logoff,
Logoff has a message queue
Logoff is an endless loop that keeps processing messages in the message queue.

Handler is a data structure created for convenient multi-thread processing or asynchronous processing. It has two members, one pointing to the Logoff of the thread created by handler and the other is the message queue of the thread. Therefore, calling the POST method of handler actually places the message to the Message Queue of the thread (Note: it is not the current thread. If handler is created in the main thread, call the POST method of handler in a sub-thread to send messages to the Message Queue of the main thread.) each message has a target Member, when handler sends a previous message, the target setting of the message is called itself.

After the logoff receives the message, it will process it and send the message to the target. That is to say, after the message is circled and returned to handler, handler can handle it. This process seems like a detour. In fact, it is necessary to call handler post in sub-thread, while handler's processing method is called in the main thread, which is accompanied by a thread scheduling.

Message, messagequeue, logoff, Handler

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.