Handler, Looper, message Asynchronous Message processing threading mechanism (hander message mechanism principle)

Source: Internet
Author: User

Handler, Looper, and message all have concepts related to the Android asynchronous message processing thread. So what is the asynchronous message processing thread? When the asynchronous message processing thread starts, it enters an infinite loop body, takes a message out of its internal message queue once per loop, and then callbacks the corresponding message handler, and then resumes the loop after the completion of a message. If the message queue is empty, the thread will block the wait. Detailed Summary: :Premise, the main thread UI, can only refresh the page and handle events, time-consuming data processing is in the child thread ... So how does the data of the child threads synchronize to the main thread? That requires the asynchronous message processing mechanism handler, Looper, message:::1 child thread Hander.sendmessage () to send a message message to the main thread, and the handler must correspond to a Looper object. When the main thread UI is created, it contains a Looper object. In the handler constructor, the looper of the current UI thread is associated with handler. 2 The Looper constructor creates a MessageQueue Message Queuing object, and the loop () method loops through the corresponding messages that are fetched from the message queue Cheng, and the callback handles the handlermessage. 3 multi-Threading when processing handler, it is easy to cause memory leak ... 4 because of the FIFO form based on the looper of the message queue, it will not result in a blocking race ... 5 in the UI main thread of the activity, there is no need to use explicit Looper initialization and start looping because the activity contains a Looper object that automatically manages Looper and handles messages sent over the child threads. 6 thread threads want to manage Message Queuing, call the Looper.prepare () method in the thread to initialize the message loop, and call Looper.loop () to keep the message loop running until the loop is stopped. So Looper is mainly to complete the function of MessageQueue and handler interaction. more simple saying ::::Android only allows the main thread to update the UI interface (non-thread-safe), the result of child threading processing, i.e. the main thread cannot be accessed directly. This will use the handler mechanism to solve this problem. Based on the handler mechanism, a handler object is obtained in the subnet enters upgradeable, which sends the data to the main thread message queue, which is called back when the message sent by Mainlooper from the message queue is handler. Handler's Handlermessage () method. Handler.post (New Runnable ()) and SendMessage (msg) differences and Runonuithread () methods(1) is to put the message in the message queue waiting for execution, the former is a Runnable object, the latter is a Message object, (2) The former will eventually be converted to SendMessage, but the final processing method is not the same, the former will execute the runnable run method The latter can be scheduled to execute in a thread. (3) There is no difference between the two, can update the UI, the difference is whether it is easy to maintain and so on. What is Handlerthread?Handlerthread inherits the thread, which is a threaded class that contains looper. Normally, in addition to the main thread, the worker thread is not looper, but in order to automatically cycle messages like the main thread, Android also customizes a worker thread--handlerthread class that contains Looper. Intentservice includes Handlerthread, so the message queue can be processed to process messages without blocking the race. Note ::The main thread of the UI is non-thread-safe, responsible for page refreshes, and the UI main thread of the response event single-threaded model is unsafe and can result in indeterminate results. Thread insecurity is simply understood as: when multithreaded access to resources, it is possible that multiple threads change the data successively, resulting in inconsistent data. Thread safety is simple to understand that when a thread accesses a feature resource, the resource process is protected, such as a lock mechanism, and the current thread waits until the lock is released, and the thread is safe until the lock is released.  Android's single-threaded model must adhere to two rules: 1.  Do not block the UI thread; 2. Do not access the UI component outside of the UI thread, that is, you cannot access the UI component on a child thread, only in the UI threads. 3 time-consuming is put into sub-threading, because the UI thread has the same priority as other normal threads. Being blocked for more than 5 seconds will prompt the ANR.

Handler, Looper, message Asynchronous Message processing threading mechanism (hander message mechanism principle)

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.