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
The case analysis brought to you today is Fan Den...
When I wrote this title, my Zhihu community had j...
Just one sentence can expose you as a PR dog! &qu...
The hardtop version of the new generation Porsche...
[[133464]] Arstechnica: Microsoft is very ambitio...
There have been cases of suspected aerosol transm...
The arrival of mini programs , a new portal on th...
【51CTO.com original article】Seven years of hard w...
Reviewer of this article: Chen Haixu, Deputy Dire...
When placing advertisements, many advertisers pre...
The most comprehensive information about Xiaohongs...
Course Description: The course comes from Ant Com...
According to Japan's Jiji Press, Japan is neg...
In recent decades, scientists have come to unders...
Editor’s Note: Barbecue is nature's gift to m...