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
I believe this phenomenon should be quite common ...
(Geneva, February 26, 2024) At the Geneva Interna...
March 15, 2024: Mercury, bright Jupiter, and the ...
When doing store customer development activities,...
1. The quality of Baidu’s bidding account. The qu...
Author: Ye Jianfei The article comes from the Sci...
1. What is a matrix? The advanced method of accou...
With 468 million monthly active users, WeChat has...
How to prevent fumonisin poisoning in summer? Zha...
Information flow advertising is now the new favor...
With the continuous development of human civiliza...
Chengdu is a place you won't want to leave on...
Competitive product analysis occupies a very magi...
December 4, 14:40 Puning City, Guangdong Province...
September 27, 2022, Beijing time. For two asteroi...