Inter-thread communication: Handler, Looper, MessageQueue, Message (end)

Source: Internet
Author: User
Tags message queue

Overview:  To facilitate communication between threads, the Handler mechanism simplifies the development of multithreading through collaboration between Handler and Looper, MessageQueue, and Message classes. The thread's interaction is encapsulated into a message, and the thread that implements the Handler mechanism calls the Looper loop () method by Handler the message into the MessageQueue message queue, Looper  The role of a message distributor is reflected.  In the Loop () method, a message is obtained from the queue MessageQueue, and the obtained message is dropped to the Handler responsible for processing the message.   Handler determines which method is ultimately passed to message processing based on whether callback (Runnable) in the message is empty and whether the callback Mcallback (Handler.callback) in the Handler is empty. --------------------------------------------------------------------------------------------------------------- -------- in the Handler mechanism, each role is divided into the following:1. Handler: Responsible for sending and processing messages (message) 2. Looper: Responsible for the distribution of messages 3. MessageQueue: Responsible for the management of messages 4.  Message: With the int parameter what and arg1 and arg2, these are to simplify the data set by the SetData (bundle bundle), and the parameter obj of an arbitrary object. If desired, you can also take a Runnable type of parameter callback,---------------------------------------------------------------------------- ---------------------------------------- The first part: Handler
    • creation of Handler :
1. If the parameter has Looper, assign the Looper to mlooper otherwise assign the Looper corresponding to the current thread to Mlooper.   2. Get the message queue mqueue that corresponds to Looper. 3. If the argument of the constructor contains an instance of Handler.callback, Callback assigns it to the Mcallback variable.
  
    • Handler Message Delivery:
1. There are two kinds of message content to be sent Runnable another.
by tracking the implementation of the post, you can find that the Runnable is also converted to a Message object.
 
Figure 1:
Figure 2:
2. Common methods for message-sending calls are: Post (Runnable), postdelayed (Runnable,long), and sendemptymessage (int), sendMessage (message) , sendmessagedelayed (Message,long) These methods will be attributed to: Sendmessageattime ()
in Sendmessageattime, the target parameter corresponding to the Message is set to the current Handler, and the MSG is placed in the Messag
Equeue queue,
3. Method callbacks corresponding to message processing:
handle Message processing has three locations:
its 1: When the message is a Runnable message, the MSG callback (that is, the Runnable object) is is not empty, call Msg.callback.run () directly to execute the callback.
2: When we construct the Handler, we have passed the Handler.callback callback object, then the Mcallback is not empty (known by the Handler constructor), then
  Call Handler.callback the Handlemessage () method that corresponds to this interface.
3: If there are no previous two, the handlemessage is overloaded to handle the corresponding callback logic when creating Handler subclasses or anonymous objects.
 
Part II: Looper
    • Initialization of the Looper:
when calling Looper's static method prepare (), a Looper object is created and set to a static local variable in the county.
in the Looper constructor, you can see that a MessageQueue object is created, MessageQueue as a message queue.
    • Invocation of the Looper.loop () method
A thread called prepare () just prepares the message queue and enters the message queue waiting for The loop () method that calls Looper:
through the Loop () method  Implementation, you know that the corresponding message queue is obtained first from the corresponding Looper in the current thread, and then the next message is fetched from the queue of messages, and the loop is exited directly when the message obtained is NULL.
If MSG is not NULL, the DispatchMessage method of the corresponding Handler object in message is called to distribute the messages. The specific distribution of the message has been mentioned above.
you can see in the Queue.next () statement that the For loop is a dead loop, and you know that when the thread calls the loop () method, it does not continue to
Instead of waiting for the message list (queue) to return the message, and then drops the message to Handler for processing.
 
 

Inter-thread communication: Handler, Looper, MessageQueue, Message (end)

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.