Relationship between Android thread handlerthread low.message messagequeue Handler

Source: Internet
Author: User

1. Relationship between messagequeue and message

Message Chinese is the smallest unit of Message Processing and thread processing. It contains a processed dataset or an operation, and tells the destination what to do. Each messagequeue contains a message. Each message is not directly inserted into messagequeue, but through messagequeue. idlehandler and looper work together, put the message into messagequeue, and addidlehandler (messagequeue. idlehandler handler) and removeidlehandler (messagequeue. idlehandler handler) method, put messagequeue. idlehandler is pushed into messagequeu.

 

2. Relationship between thread and handlerthread

Handlerthread is a thread with a loose loop.

 

3. logoff Introduction

Logoff is a loop. It must be bound to a fixed thread. The getthread () method can be used to obtain the thread object bound to the logoff, and getmainlogoff () can be used to obtain the logoff object in the main thread, myloue () can get the loageobject in the current thread, and get the messagequeue object in the current thread through myqueue.

4. Relationship between thread, handlerthread, and logoff

It is generally declared that a thread does not carry a logoff loop, but it can be declared through logoff. the prepare () method adds logoff; logoff to a thread. loop () executes the messagequeue in the thread bound to the loopger until the loopger ends. Loop. quit () ends the loop. The instance code is as follows:

  class LooperThread extends Thread {      public Handler mHandler;            public void run() {          Looper.prepare();                    mHandler = new Handler() {              public void handleMessage(Message msg) {                  // process incoming messages here              }          };                    Looper.loop();      }  }
Fortunately, Android provides us with a method to directly generate a loose thread and instantiate a handlerthread object. The loose object of the handlerthread can be obtained through the handlerthread. getlooper () method.
5. Relationship between handler and thread handlerthread low.message messagequeue
The handler class is used to send and process messages/runnable. A handler class is associated with a unique thread, and the thread is the thread that generates the handler,
It sends the message to the messagequeue in the thread that generates the message, and then executes the message when the message is out of the queue.
 post(Runnable),postAtTime(Runnable, long),postDelayed(Runnable, long)Is used to process runnable objects;
 sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message, long), AndThe sendmessagedelayed (message, long) method is
It is used to process message objects. It is generally executed immediately, but we can set latency and execute it cyclically.
You can use the handlemessage (Message MSG) method to receive messages.
Instantiation: 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.