In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Android Handler". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Android Handler.
Introduction and use of ThreadLocal
Everyone is familiar with threading Thread, but it may be much stranger to ThreadLocal. Although we don't know much about it, it has been available as early as the JDK1.2 version and is widely used. For example, hibernate,EventBus,Handler uses ThreadLocal for thread-related operations. If you look at it purely from the name ThreadLocal, it has a strong flavor of "local thread"; however, after taking a sip, it turns out that it doesn't smell like this at all. In fact, ThreadLocal is not used to operate on any local thread, but to implement copies of data from different threads. When using ThreadLocal to maintain a variable, it provides an independent copy of the variable for each thread that uses the variable; each thread can change its own copy independently without affecting the corresponding copy held by other threads. Therefore, the actual role of ThreadLocal does not match the meaning implied by its name, and perhaps it would be more appropriate to rename it ThreadLocalVariable (thread local variable).
Next, let's take a look at how ThreadLocal is used through an example.
/ * * original author: * Brother Gu's younger brother * * blog address: * http://blog.csdn.net/lfdfhl * / private void testThreadLocal () {mThreadLocal.set ("Tokyo fever"); new HotThread1 (). Start (); new HotThread2 (). Start (); hot3=mThreadLocal.get () Try {Thread.sleep (1000 4); Log.i (TAG, "variable value obtained by HotThread1:" + hot1); Log.i (TAG, "variable value obtained by HotThread2:" + hot2); Log.i (TAG, "variable value obtained by MainThread:" + hot3) } catch (Exception e) {}} private class HotThread1 extends Thread {@ Override public void run () {super.run (); mThreadLocal.set ("Beijing fever"); hot1=mThreadLocal.get () }} private class HotThread2 extends Thread {@ Override public void run () {super.run (); mThreadLocal.set ("Nanjing fever"); hot2=mThreadLocal.get ();}}
View the output:
Variable value obtained by HotThread1: variable value obtained by Beijing hot HotThread2: variable value obtained by Nanjing hot MainThread: Tokyo heat
In this code, ThreadLocal is used to save data of type String, and different values are set for ThreadLocal in the main thread and two child threads, and then these values are taken out respectively. Combined with the output log, we can find that the same ThreadLocal object is accessed in different threads, but the value obtained by mThreadLocal.get () is different; that is to say, they do not influence each other but remain independent of each other. Once we understand this feature of ThreadLocal, it will be much easier for us to understand how Looper works.
The relationship between Looper, thread, and message queue
Google officially recommends that developers use Handler to implement asynchronous refresh of UI. We have also adopted this proposal very well in our daily work: first, we establish the Handler in the main thread, then use handler.sendMessage (message) in the child thread to send the message to the main thread, and the final message is processed in handleMessage (Message msg) {}. This routine is all too familiar to everyone; now, from a different point of view, let's try to set up a Handler in a child thread.
Private class LooperThread extends Thread {@ Override public void run () {super.run (); Handler handler=new Handler (); / / doing something}}
The code here is simple: LooperThread inherits from Thread and creates a new Handler in its run () method.
Mm-hmm, run it again, wow, wrong report:
Can't create handler inside thread that has not called Looper.prepare ().
Hey, it's a bit of a bad start. I made a mistake as soon as I tried. . It's all right. Life is just endless setbacks and hopes. It doesn't count as a trifle. Since it is the fault of invoking the constructor of Handler, let's start with the source code of the constructor and find out:
Public Handler () {this (null, false);} public Handler (Callback callback) {this (callback, false);} public Handler (Callback callback, boolean async) {if (FIND_POTENTIAL_LEAKS) {final Class
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.