Handler: Handling MessagesMessage: MessagesMessageQueue: Message QueuingLooper:messagequeue's managerMessageQueue:Message Queuing. Storing multiple Message.messagequeue with FIFO is managed through handler, through Looper.prepare () The MessageQueue object is created automatically while the Looper is created. The UI main thread creates the
, receive the message sent by the mainline Cheng thread, and process it. Childrenlooperthread childerlooperthread=new Childrenlooperthread ();
Childerlooperthread.start ();
public class Childrenlooperthread extends thread{
@Override
public void Run () {
Looper.prepare ()//Create Looper
Mhandler=new Handler () {
@Override
public void Handlemessage (msg) {
TODO auto-generated Method Stub
LOG.E ("TAG", "Mhandl
Looper plays the role of the message loop in the message loop, he will not stop to take out the messages from the MessageQueue, if there is a message to deal with, will not be blocked. Creating a looper creates a MessageQueue when the Looper is created Private Looper (Boolean quitallowed) { mqueue = new Messa
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
Looper is driven by calling the loop method to carry out a message loop: Blocking out a message from MessageQueue, and then letting handler process the message, the loop method is a dead loop method.How do you stop the message loop? We can call the Looper quit method or the Quitsafely method, which is slightly different.Looper's Quit method source code is as foll
Java Official website Description of the Looper object:
public class Looperextends ObjectClass used to run a message loop for a thread. Threads By default don't have a message loop associated with them; To create one, call prepare () in the thread, the "is" to run the loop, and then loop () to have it process messages until the The loop is stopped.
Most interaction with a message loop is through the Handler
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 *
Copy Code code as follows:
Package Com.yanjun;
Import android.app.Activity;Import Android.os.Bundle;Import Android.os.Handler;Import Android.os.HandlerThread;Import Android.os.Looper;Import Android.os.Message;
public class Handleractivity extends activity {
@Overridepublic void OnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate);Setcontentview (R.layout.main);Implements the ability to use Looper to process message qu
store messages sent through Handler, is based on FIFO execution, and is used internally for the structure of a single-linked list.
HandlerResponsible for sending messages and processing messages.
LooperResponsible for the message loop, loop out MessageQueue inside the message, and give the corresponding Handler to handle.
When the app starts, a UI thread is turned on, and the message loop is s
1. First Looper.prepare () saves an looper instance in this thread, and then holds a MessageQueue object in the instance, because Looper.prepare () can only be called once in a thread. So MessageQueue will only exist in one thread.2. Looper.loop () causes the current thread to enter an infinite loop, which reads the message from the MessageQueue instance and then callbacks the Msg.target.dispatchMessage (msg) method.3,
results, then you are already familiar with the use of handler, if you do not know the result of the above operation, please go ahead.Before I start analyzing the reasons, I would like to ask the following questions:1. How does the message and runnable emitted by handler be passed to the UI thread?2. Under what circumstances does the message and runnable sent through h
The previous article mentions that the UI thread of an Android system is a thread with a message loop (Looper) mechanism, while Android also provides a Handlerthread class that encapsulates a message loop (Looper) that can bind handler () objects, A message is sent to the thread through the SendMessage () function of handler
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
The collation of a post:
Handler must the main thread be instantiated? The difference between New Handler () and New Handler (Looper.getmainlooper ())If you instantiate without parameters: Handler Handler = new Handler (), then th
: mProgressBar.setMessage("Image downloading failure!"); mProgressBar.dismiss(); break; } } }; Message msg = mHandler.obtainMessage(MSG_LOAD_FAILURE, null); mHandler.sendMessage(msg);
So what happened behind Handler's post/sendMessage? Let's take a look at this mystery.
First, we will analyze the Handler constructor:
final MessageQueue mQueue; final
); }//handler The default constructor will eventually be transferred to this method Public Handler(Callback Callback, BooleanAsync) {if(Find_potential_leaks) {final classif((Klass.isanonymousclass () | | klass.ismemberclass () | | klass.islocalclass ()) (klass.getmodif Iers () modifier.static) = =0) {//It turns out there has been a test printLOG.W (TAG,"The following
The following content is translated from: https://www.tutorialspoint.com/springmvc/springmvc_beannameurlhandlermapping.htmDescription: The sample is based on spring MVC 4.1.6.The following example shows how to use the bean Name Url Handler Mapping using the Spring WEB MVC framework. The Beannameurlhandlermapping class is the default handler mapping class that maps URL requests to the names of the beans that
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.