The main function of handler is to switch a task to a specified thread to execute. Android's messaging mechanism mainly involves three classes: Handler, Looper, MessageQueue:Now suppose a scenario:There are two threads, threads 1 and 2, calling Looper.prepare () in thread 1, creating a Handler object handler, calling Looper.loop ().call Handler.sendmessage () in thread 2 to send the message, and eventually the message will be handler.handlemassage ()
must be associated with a looper, and the two are one by one corresponding, note that this is very important oh! In addition, Looper is responsible for taking out a message from its internal MessageQueue to the handler for processing. Since our handler here is implemented in the UI thread, after such a handler and message mechanism, we can go back to the UI thread.
handler: The worker who handles the retu
(); } //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 is referring to the Windows message loop mechanism to implement Android's own message loop.Android uses Looper, handler to implement the message loop mechanism, the Android message loop is for threads (each thread can have its own message queue and message loop).In Android, Looper is responsible for managing thread Message Queuing and message loops. We can get the
HandlerthreadIn the last Android time-consuming task _handler introduced the handler mechanism, and introduced a simple example of a common thread that generates Looper and communicates using handler mechanisms. We know that there is no looper in the normal thread, and it is not good to use the handler mechanism in normal thread space, and it would be a little cumbersome to do it each time as in the previou
mechanism in the Android system. Android uses Looper, handler to implement the message loop mechanism. The message loop for Android 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 thread Message Queuing and message loops. The Looper object of the current thread is ob
The Android Message Queuing modelAndroid is referring to the Windows message loop mechanism to implement Android's own message loop. Android uses Looper, handler to implement the message loop mechanism, the Android message loop is for threads (each thread can have its own message queue and message loop). In Android, Looper is responsible for managing thread Message Queuing and message loops. We can get the
Android Handler logoff and MessageQueue message mechanism Principle
Android message processing class:
Logoff, Handler, MessageQueue, Message, ThreadLocal, ThreadLocal. Values, and HandlerThread.Logoff: the thread does not have a message loop by default. To create a message loop for a thread, call prepare (), and then call loop () the method enters the message loop (this means that the thread will keep repeating this method ). For example:
class LooperThread extends Thread { public Handler m
Android's message mechanism is mainly handler operating mechanism, and the handler mechanism, but also need and MessageQueue and looper combination. MessageQueue the message queue, although called a queue, but its internal structure is not composed of queues, but the use of a single-linked list to store messages. MessageQueue is only responsible for storing messages, not processing messages (this refers to the rotation of the message),
answer. There are 1500 nodes. The normal Hungary algorithm should time out. Enable hopcroft-carp algorithms. G ++ 6156 Ms 9428 K */ # Include # Include # Include String . H> # Include # Include Using Namespace STD; /* **************************************** * *** Bipartite Graph Matching (hopcroft-carp algorithm ). Initialization: G [] [] calling of the adjacent matrix: res = maxmatch (); NX, NY needs to be initialized !!! Time Complexity: O (V ^ 0.5 E) this method is applicable to
/* **************************************** *****Bipartite Graph Matching (hopcroft-Carp'sAlgorithm).Initialization: G [] [] adjacent matrixCall: res = maxmatch (); NX, NY to initialize !!!Time Complexity: O (V ^ 0.5 E)Suitable for binary matching with large data volumes**************************************** ****** */ Const Int Maxn = 3001 ; Const Int INF = 1 28 ; Int G [maxn] [maxn], MX [maxn], my [maxn], NX, NY; Int DX [maxn], Dy [maxn], DIS; Bool
Question:A boat walks in the water and wants to reach its destination. Then there are eight directions. Use 0 ...... 7: North, Northeast, east, southeast ......, But it has a direction at any point, and when its next direction is the same as its own wind direction at the moment (that is, the next direction is the same as its number ), this step does not require energy consumption. Otherwise, the energy consumed is 1 unit of energy. Then the minimum energy required for the boat to reach the desti
Xiaomi box will appear the user authentication failure problem, then how to solve the user authentication failure problem? Let the small part below to tell you, read the following believe you will know.
1, first download "VST Full aggregation" or "sofa desktop", we take the sofa desktop as an example, download good on the USB stick, insert the box.
2, unplug the box power and then plug in, and so the box restart, see the Start screen is always click
When learning an Android thread, using a child thread directly in the UI thread to update the contents of the TextView display will have the following error: Android.view.viewroot$calledfromwrongthreadexception:only the Original thread that created a view hierarchy can touch it views.The general idea is that only the thread that created the control can update the contents of the control.In Android, if you want to manipulate the UI, you have to do it in the UI thread, which is the main thread, an
pool,
Creates a message object with the given parameters. When removemessages () is called, the message is deleted from the message queue and put into the message pool. Except for the above
You can also get a message instance through the obtainmessage () of the handler object.
3. Logoff:
Is the manager of messagequeue. Each messagequeue cannot be separated from the logoff object. The creation of the logoff object is implemented through the prepare function. At the same time, each logoff object
From the source code analysis Handler mechanism, the source code handler Mechanism
In Android, to update the ui, We must update the ui in the main thread. When the main thread is blocked for more than 5 seconds, an anr exception occurs, will cause program crash. Therefore, some time-consuming operations must be placed in the Child thread, but the ui update operations cannot be performed in the Child thread. To solve this problem, Android has designed the handler mechanism, the appearance of han
Original Address:Deep understanding of the fundamentals of Android Message Processing systemsHoarnThe Android app is also message-driven and, as a rationale, should provide a message loop mechanism. In fact, Google referenced the Windows message loop mechanism, also implemented in the Android system message loop mechanism.Android uses Looper, handler to implement the message loop mechanism, the Android message loop is for threads (each thread can have
Handler Looper poll MessageQueue Message Object1The main thread is called when it is created, Public Static void Preparemainlooper() {} constructor method. Public Static void Preparemainlooper() {Prepare (false);synchronized(Looper.class) {if(Smainlooper! =NULL) {Throw NewIllegalStateException ("The main Looper has already been prepared."); } Smainlooper = Mylooper (); } }2The prepare is called in
Message Pool. If yes, It is retrieved and returned. If no Message instance is available in the Message Pool, a Message object is created with the given parameters. When removeMessages () is called, the Message is deleted from the Message Queue and put into the Message Pool. In addition to the above method, you can also get a Message instance through the obtainMessage () of the Handler object.
3. Logoff: the manager of MessageQueue. Each MessageQueue cannot be separated from the logoff object. T
1, scheduling principleAndriod 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 MessageQueu
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.