mechanism in the Android system. Android uses Looper, handler to implement the message loop mechanism. The android message loop is thread-specific, and each thread can have its own message queue and message loop.
The Looper in the Android system is responsible for managing the thread's message queues and message loops. The Looper object of the current thread is
recycled). The following figure is the structure of the message pool:
1.2.MessageQueue
MessageQueue has a column for the message that is received:
Messagequeue.mmessages (static variable)-> (message and Message.next)-> (Messages and Message.next), ..., and the following figure is to receive message queues:
The upper-layer code will eventually invoke the MessageQueue enqueuemessage function by putting a message into the MessageQueue via a handler SendMessage function. Enqueuemessage the re
Note: This is just a procedure for SendMessage, post is similarIf we need to send a message, we call the SendMessage method
Public Final Boolean SendMessage (Message msg)
{return
sendmessagedelayed (msg, 0);
}
This method will call the following method
Public Final Boolean sendmessagedelayed (Message msg, long Delaymillis)
{
if (Delaymillis
Then set the delay time and continue calling the Sendmessageattime method
public boolean sendmessageattime (msg, long
First, Introduction①, we cannot access the UI null control in a sub-thread, it is time to put the update UI operation into the main thread only by handler②, Handler's composition: MessageQueue and Looper support③, MessageQueue: function: Stores a set of messages that provide insertions and deletions in the form of queues. The fact is that a single-linked list of data structures is used to store message lists.④, Lo
the thread, and the thread waits for an unknown time. This will result in a very inefficient access to the UI. The Android messaging mechanism was created to address the problem of accessing the UI in a child thread.2. What is Android's messaging mechanismThe message mechanism of Android mainly refers to the operating mechanism of handler.When it comes to handler, it is necessary to mention another three concepts, namely, message, MessageQueue, Looper
default. After the main thread is started, the UI is drawn first, then it enters a message loop (loop), waits and executes various messages and events from the system, various user click/touch events, message events sent by other threads, and so on. This is a common pattern of threading, which is to enter a "Wait command", "Execute command/message", "Wait for command/message" loop.So how do other non-UI threads interact with the main thread that enters the message loop? It depends on the handle
of the above run method is to call lorule. prepare and lorule. loop To build a loop thread. It is worth mentioning
Yes. The onLooperPrepared method is called before the loop is started in the run method. The implementation of this method is empty. You can implement this method in the subclass. The function of this method is
Perform initialization before the thread loop. You can also choose not to implement this method, depending on your needs. It can also be seen that Google engineers also cons
removed from the message. Follow the principle of FIFO.2 HandlerResponsible for sending messages to the message container, which is MessageQueue.3 Looper Rotation deviceBy calling its own loop method, the message is continuously fetched from the message queue and sent to target (that is, handler) to process the message. When there is no rotation message in the message queue, it is in a blocked state.Let's take a practical look at how handler works:
thread, and handler constantly gets and processes the message from MessageQueue This causes the method in the handler class to process the message to be recalled. new Thread (new Runnable () {@Override public void run () { try {Thread.Sleep ( 2000 catch (Interruptedexception e) {E.prin Tstacktrace (); The Message msg = Handler.obtainmessage (); Msg.what = Constant.user_login; Msg.obj = "You have not registered yet, are you quick to login" New Handler () { publicvoi
(); } //Worker Threads Private classWorkthreadextendsThread {@Override Public voidrun () {//...... Handle more time-consuming operations//send message to Handler when processing is completeMessage msg =NewMessage (); Msg.what=completed; Handler.sendmessage (msg); } }}In this way, we can solve the problem of thread safety, the complex task processing to the child thread to complete, and then the child thread through the handler object to inform the main thread, the main threa
"Android Development Art Exploration" reading notes (ix)--Four componentsNO1:The internal storage structure of Message Queuing MessageQueue is not a real queue, but rather a single-linked list of data structures to store message lists, because single-linked lists have advantages over insertions and deletionsNo2:ThreadLocal can store and provide data in different threads with no interference , and the looper of each thread can be easily obtained throug
Andriod provides handler and Looper to meet the communication between threads. Handler first-out principle. The Looper class is used to manage message exchange (Messageexchange) between objects within a particular thread.1) Looper: A thread can produce a Looper object that manages the MessageQueue (Message Queuing) of
Andriod provides handler and Looper to meet the communication between threads. Handler first-out principle. The Looper class is used to manage message exchange (Messageexchange) between objects within a particular thread.1) Looper: A thread can produce a Looper object that manages the MessageQueue (Message Queuing) of
Handler, loiter, HandlerThread analysis, handlerthreadloiter
Handler must have been used in the process of writing Android code, especially when the blocking operation thread is used to update the UI thread. Handler is used properly to prevent many multi-thread exceptions.
Logoff is also known to everyone, but the logoff is generally not used to write application code. However, the key to the actual Handler's Message processing lies in logoff.
The following is a summary after I have read the rel
New one handler in child threads why does the following error be reported?Java.lang.RuntimeException:Can ' t create handler inside thread that have not called looper.prepare ()This is because the handler object is in the same thread as its caller, and the calling thread is blocked if the delay operation is set in handler. Each handler object binds to a Looper object, and each Looper object corresponds to a
Mainactivity as follows:Package Cc.c;import Android.app.activity;import Android.os.bundle;import android.os.handler;import Android.os.looper;import Android.os.message;import android.widget.textview;/** * Demo Description: * * Example steps are as follows: * 1 child threads send messages to the child thread itself * 2 after receiving 1 of the message, the child thread sends a message to the main thread * 3 receives a 2 message, the main thread sends a message to the child thread * * To implement
Mainactivity such as the following:Package Cc.c;import Android.app.activity;import Android.os.bundle;import android.os.handler;import Android.os.looper;import Android.os.message;import android.widget.textview;/** * Demo Description: * * Demo sample process such as the following: * 1 child threads to the child thread itself Send Message * 2 after receiving 1 message, the child thread sends a message to the main thread * 3 receives a 2 message, the main thread sends a message to the child thread *
responsibility of the other child threads not to meddle with objects on the UI screen
Because Android wants the UI thread to be able to give users a quick response to their requirements. If the UI thread spends too much time doing the behind the scenes, and the user waits for more than 5 seconds after the UI event occurs, Android will apologize to the user.
When we launch an app, Android will be born with a new process ( process), and load the app into this newly-born sess
First, Looper classfirst, you need to know a class, Looper class, as the name implies, Loo per is the meaning of the circulator, then Looper the existence of a class is to make an ordinary thread into a thread that will loop, and we can understand it as an elixir of immortality and eat it without getting old. Normal. Threa D class only needs to execute Looper.pre
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.