Relationship between message, Handler, MessageQueue, Looper in a single-threaded model

Source: Internet
Author: User

Handler Introduction:
aHandlerallows you to send and processMessageand therunableobjects, these objects, and a thread'sMessageQueueassociated. Each instance of a thread and a separate thread and the thread'sMessageQueueassociated. When you create a newHandler, it is bound together with the thread that created it. Here, threads we can also understand as threads ofMessageQueue. From this point of view,HandlerputMessageand therunableobject is passed to theMessageQueue, and when these objects leaveMessageQueuewhen theHandlerresponsible for enforcing them.

Handler There are two main uses:

( 1 ) to determine whether to perform one or more at a later point in time Message and the Runnable object.

( 2 ) on other threads (not Handler bound thread) to perform some action.


Scheduling Message , i.e. (1), can be done in the following ways:
Post (Runnable): Runnable executes on a thread bound by handler, that is, does not create a new thread.
Postattime (Runnable,long):
Postdelayed (Runnable,long):
Sendemptymessage (int):
SendMessage (Message):
Sendmessageattime (Message,long):
Sendmessagedelayed (Message,long):
Post This action lets you put runnable object in messagequeue,messagequeue When these messages are executed they are, of course, sorted in a certain order. sendmessage This action allows you to put the message objects are queued, these message object contains some information, handler hanlermessage (Message) message. of course, handlermessage (Message) must be by the handler

when posting or sending to a hanler , you can have three behaviors: when MessageQueue ready to process, define a delay time, define an exact time to process. The latter two allow you to implement Timeout,tick, and time-based behavior.

when your app creates a new process, the main thread (that is,UIthread) comes with aMessageQueue, thisMessageQueueManage Top-level application objects (likeActivities,broadcast Receiversand so on) and the main thread to create the form. You can create your own thread and pass aHandlercommunicate with the main thread. This is the same as before, throughPostand theSendMessageto complete, the difference is in which thread executes such a method. At the right time, given theRunnableand theMessagewill be inHandlerof theMessageQueuein isscheduled.


Message Introduction:
Message A class is defined as a message that contains a descriptor and any data object that is used to pass to the handler.message Object providing an additional two int Domain and a Object domain, which allows you to not use the assigned action in most cases.
Although the message 's constructor is public, the best way to get the message instance is to call the Message.obtain (), or the handler.obtainmessage () method, which gets one from the reclaimed object pool.


MessageQueue Introduction:
This is an underlying class that contains a List of the message. Looper is responsible for distributing these message. Messages is not directly added to a MessageQueue , but through The Messagequeue.idlehandler is associated to Looper.
You can get MessageQueue from the current thread by looper.myqueue () .


Looper Introduction:
Looper class is used to execute a message loop in a thread . By default, none of the message loops are associated to the thread. Call Prepare () in the thread to create a Looper, and then use Loop () to handle Messagesuntil the loop terminates.

Most of the interactions with message loops are through Handler.

The following is a typical threading Implementation with Looper.
Class Looperthread extends Thread {public      Handler Mhandler;            public void Run () {          looper.prepare ();                    Mhandler = new Handler () {public              voidhandlemessage (Message msg) {                  //process incomingmessages here              }          };                    Looper.loop ();      }  }

Relationship between message, Handler, MessageQueue, Looper in a single-threaded model

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.