When operator new fails to satisfy a memory allocation request, it throws a exception (exception). Long ago, he returned a null pointer (null pointer), while some older compilers were still doing so. You can still reach the previous goal (to some extent), but I'm going to discuss it at the end of this article.Before operator new throws a exception in response to an unsatisfied memory request, it first invokes a error-handling function (error handler)
only needs to rewrite the method of processing the message in the Handler class, and when the newly started thread sends the message, the message is sent to the associated MessageQueue, and Handler will constantly fetch and process messages from MessageQueue -This will caus
Android Message Queuing model Thread,handler,looper,massage queueBoth the message queue and message loop for the Android system are for a specific thread, and a thread can exist (and, of course, not exist) a message queue and a message
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
The concept of handler :As the name implies, handler is "manipulating, dealing with the word" in English, and the official document gives the concept that handler allows you to send or process a message object or Runable object, both of which are associated with the thread's messag
Overview : Android uses messaging mechanisms to communicate between threads, threads create their own message loops through Looper, MessageQueue is a FIFO message queue, Looper is responsible for extracting messages from MessageQueue. and distributes to the message specifies the target handler object. The
Method One: (Java habits, not recommended in Android)Just beginning to touch Android threading programming, habits like Java, trying to solve the problem with the following codenew Thread ( new Runnable () {public void run () {Myview.invalidate ();}}). Start (); Functionality can be implemented to refresh the UI interface. This is not possible, however, because it violates the single-threaded model: Android UI operations are not thread-safe and must be executed in the UI thread.Method Two:
Let's take a look at a piece of code:
Thread thread = new Thread () {public void run () {// send a Message to the main Thread in the Child thread Message msg = new Message (); msg. what = 200; msg. obj = param; msg. arg1 = 3; handler. sendMessage (msg) ;};}; Handler
Handler Use Example 1This example is the simplest introduction to handler used, which is to bind handler to the thread it is creating.Handler Use Example 2Because this example uses the handler Message Queuing mechanism, that is, sending a
Method One: (Java habits, in the development of Android platform this is not possible, because it violates the single-threaded model)Just beginning to touch Android threading programming, habits like Java, trying to solve the problem with the following codeNew Thread (New Runnable () { public void Run () { Myview.invalidate (); } }). Start ();Functionality can be implemented to refresh the UI interface. This is not possible, however, because it violates th
Take you in-depth understanding of the Android handler mechanismWelcome reprint Please indicate sourceSpeaking of the message mechanism, we will certainly think of handler, because the Android system specifies that the main thread cannot block more than 5s, otherwise "application not responding" will appear. That is, you cannot take time-consuming operations (net
Handler Usage Summary of AndroidMethod One: (Java habits, in the development of Android platform this is not possible, because it violates the single-threaded model)Just beginning to touch Android threading programming, habits like Java, trying to solve the problem with the following codeNew New Runnable () {public void run () { myview.invalidate ()} }). Start ();Functionality can be implemented to refresh the UI interf
Handler mhandler = new handler (){@ OverridePublic void handlemessage (Message MSG ){// Do something.}}'''When we create 'handler' like this, 'android lint' will prompt us with a 'warning: In Android, Handler classes shoshould be static or leaks might occur .'.I have never c
// Multi-threaded logoff and handler. It does verify that handlemessage () runs in the thread where the handler is created. Private void testlogoff () {class myhandlerthread extends thread {@ overridepublic void run () {// todo auto-generated method stublogger. showlog ("loopthread --> enter"); loopthread. prepare (); logoff. loop (); // if no call is made, handler
Handler is known as the "Message Processor" in Android and is more commonly used in multithreading.
Handler provides an asynchronous message processing mechanism for Android that returns immediately after sending a message (SendMessage) to a
Handler Usage Summary of AndroidMethod One: (Java habits, in the development of Android platform this is not possible, because it violates the single-threaded model)Just beginning to touch Android threading programming, habits like Java, trying to solve the problem with the following codeNew New Runnable () {public void run () { myview.invalidate ()} }). Start ();Functionality can be implemented to refresh the UI interf
() method to update the interface; text. setText (updated the interface using Handler );}}}
Note: The Thread must call the start () method, otherwise the Thread will not be executed. The implementation result is as follows:
(3) sendMessage (), handleMessage () method
There are two very important methods in Handler: sendMessage () and handleMessage (). The sendMessage () method is used to send a
Descriptive narrative of the problemOnce upon a time, we used the original method to use handler will have the following a warm hint:classstaticor leaks might occurHere's a more specific description (warning on Android Studio, not knowing if Eclipse is the same)Since This Handler is declared as a inner class, it may prevent the outer class from being garbage collected. If the
Asynchronous explanation of Android development (1) Thread + Handler and androidhandler
Please respect others' labor achievements. Reprinted with the source: asynchronous explanation of Android development (1) Thread + Handler
Http://blog.csdn.net/fengyuzhengfan/article/details/40211589
In actual Android development projects, time-consuming operations such as file read/write and network access are often per
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.