HandlerThread is a thread class used in Android development to start a new thread with a Looper. It contains a Looper and a Handler, which can easily send and process messages. By processing tasks in HandlerThread and sending the results back to the main thread through Handler, data transfer and interaction between threads can be achieved. Principle AnalysisCreate a HandlerThread instance by calling the HandlerThread constructor and passing in a string as the thread name. HandlerThread inherits from Thread and is essentially a thread. Its construction method mainly performs some initialization operations. We know that the HandlerThread class is actually a Thread, and the start() method must call the run method of Thread. Let's take a look at the specific implementation of its run method. The Looper.prepate() method and the Loop.loop() method are called internally. If you are familiar with the Android asynchronous message mechanism, you will know that in the Android system, there are Looper objects, MessageQueue objects, and Handler objects. So through the run method, we can know that we created the Looper and MessageQueue of the HandlerThread thread we created. It should be noted here that before calling the Looper.loop() method, an empty implementation method onLooperPrepared() is called. We can implement our own onLooperPrepared method to do some Looper initialization operations; In the run method, there is a notifyAll() after the mLooper is created, and a wait() in getLooper(). Because mLooper is executed in a thread, and our Handler is initialized in the UI thread, that is, we must wait until the mLooper is created before we can correctly return to getLooper(). wait()``notify() is to solve the synchronization problem of the two threads. We need to initialize the Handler instance when using it: The HandlerThread's Looper object is passed into the Handler's construction method, so the Handler object has a reference to the Looper object in the HandlerThread thread. Call the Handler's sendMessage method to send a message, and the message can be received in the Handler's handleMessage method. Basic Usage
Create a HandlerThread instance by calling the HandlerThread constructor and passing in a string as the thread name. "mHandlerThread" is the name of the thread.
After creating a HandlerThread instance, you need to call the start() method to start the thread.
After the HandlerThread is started, a Handler instance associated with the HandlerThread is obtained by calling the getLooper() method and passing it to the Handler constructor, which is used to send and process messages in the HandlerThread.
Send a message to the HandlerThread using the Handler instance created in the previous step.
Process messages sent from the main thread or other threads in the handleMessage() method of Handler.
The message loop is stopped by calling the quit() or quitSafely() method of the Handler. The HandlerThread terminates after completing the current message processing.
|
>>: RESTful API Design and .NET Core Implementation
New car manufacturers often face a choice when en...
Review expert: Li Guangwang, Director of the Natu...
The newly released 2014 MacBook Air has a processo...
Where does the universe come from? People try to ...
In international economic activities, China has l...
I have been constantly changing devices from Noki...
In this case, the fission mechanism of inviting f...
With the continuous development of the Internet e...
According to Binjiang, the construction of the fi...
Review expert: Wang Xuejiang, professor of pathop...
On November 7, Luo Yonghao and his Smartisan offi...
As a major domestic hardware manufacturer, ASUS i...
In the fast-paced life, people often pursue simpl...
The college entrance examination is a major test ...
Today I would like to discuss with you how to inc...