The relationship among Message, Handler, MessageQueue, and logoff in a single-thread model.

Source: Internet
Author: User

The relationship among Message, Handler, MessageQueue, and logoff in a single-thread model.
Handler introduction:
A Handler allows you to send and process Message and Runable objects, which are associated with the MessageQueue of a thread. Each thread instance is associated with a separate thread and the MessageQueue of the thread. When you create a new Handler, it is bound with the thread that created it. Here, we can also understand the thread as the MessageQueue. From this point of view, Handler passes the Message and Runable objects to MessageQueue, and when these objects leave MessageQueue, Handler is responsible for executing them.

Handler has two main purposes:

(1) determine to execute one or more Message and Runnable objects at a certain time point in the future.

(2) Add some actions to be executed in other threads (not Handler binding threads.


Scheduling Message, that is, (1), can be completed using the following methods:
Post (Runnable): Runnable is executed on the thread bound to handler, that is, no new thread is created.
PostAtTime (Runnable, long ):
PostDelayed (Runnable, long ):
SendEmptyMessage (int ):
SendMessage (Message ):
SendMessageAtTime (Message, long ):
SendMessageDelayed (Message, long ):
The post action allows you to queue Runnable objects into MessageQueue. When MessageQueue receives these messages, execute them, of course, in a certain order. The sendMessage action allows you to queue Message objects. These Message objects contain some information. Handler's hanlerMessage (Message) processes these messages. of course, handlerMessage (Message) must be overwritten by the sub-class of Handler. This is what programmers need to do.

When posting or sending is sent to a Hanler, you can perform the following three actions: When MessageQueue is ready, it will process the request, define a delay time, and define a precise time for processing. The latter two allow you to implement timeout, tick, and time-based behaviors.

When your application creates a new process, the main thread (that is, the UI thread) comes with a MessageQueue, which manages the top-level application objects (such as activities and broadcast receivers) and the form created by the main thread. You can create your own thread and communicate with the main thread through a Handler. This is done through post and sendmessage, just like before. The difference is in which thread to execute this method. When appropriate, the given Runnable and Message will be Scheduled in the MessageQueue of Handler.


Message introduction:
The Message class defines an information that contains a descriptor and any data object. This information is used to pass to Handler. the Message Object provides two additional int fields and one Object field, which allows you to skip the allocation action in most cases.
Although the Message constructor is public, the best way to obtain the Message instance is to call Message. obtain (), or Handler. obtainMessage () method. These methods obtain one from the recycle Object pool.


MessageQueue introduction:
This is an underlying class that contains the message list. Logoff is responsible for distributing these messages. Messages are not directly added to a MessageQueue, but are associated with logoff through MessageQueue. IdleHandler.
You can obtain MessageQueue from the current thread through logoff. myQueue.


Logoff introduction:
The logoff class is used to execute the message loop in a thread. By default, no message loop is associated with a thread. In the thread, call prepare () to create a logoff, and then use loop () to process the messages until the loop ends.

Most of the interactions with message loop are handled by Handler.

The following is a typical thread implementation with logoff.
   
class LooperThread extends Thread {      public Handler mHandler;            public void run() {          Looper.prepare();                    mHandler = new Handler() {              public voidhandleMessage(Message msg) {                  // process incomingmessages here              }          };                    Looper.loop();      }  }

Related Article

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.