Analyze the usage of handlerthread thread in android with an example

1、 Introduction and use examples of handlerthread

What the hell is handlerthread? Its essence is a thread, but the handlerthread will help us prepare a looper for external use when starting. In short, it makes it easier for us to use the handler in the sub thread. For example, without handlerthread, we need to use the handler in the sub thread. The writing method is as follows:

With handlerthread, we don't have to manage looper ourselves. We can understand why when analyzing the source code.

A brief introduction to the use of handlerthread:

First, we need to initialize HandlerThread and call its start method, that is, to open the thread:

Next, initialize a handler and pass the looper in mhandlerthread as a constructor parameter to the handler:

This ensures that hnadler runs on the child thread. And you need to call the quit method or quitsafe method of handlerthread at an appropriate time, such as when the activity is destroyed:

Comparison between quit() and quitsafe() methods (only some conclusions are given here, and the source code can be viewed by yourself):

Similarities:

After calling, the messagequeue message queue does not accept new messages to join the queue.

difference:

The quit method empties all messages in the messagequeue message pool. The quitsafe method will only clear all delayed messages in the messagequeue message pool (delayed messages refer to messages sent through sendmessagedelayed or postdelayed methods). Non delayed messages will not be cleared and will continue to be sent to the handler for processing.

Next, let's take a complete look at the source code of the handlerthread example:

Run the program, and you will see that the log is displayed every two seconds on the console. As for the use of handlerthrea, that's all. It's almost enough to understand the above small example.

2、 Source code analysis of handlerthread

The handlerthread source code is very short, and the comments are less than 100 lines. All of them are directly posted here:

You can see from the first line that its essence is a thread.

The constructors in lines 6-9 and 17-20 are also very simple. During initialization, we can define the thread name and pass in the thread priority.

After initialization, the development thread will execute the run method logic immediately after calling start ().

30-41 lines of code. The most important thing is to call the loop. Prepare () and loop. Loop () methods to prepare a loop for our child thread. And the variable mlooper is used to record, which is returned when the getloop () method is called.

However, if you are careful, you must find that there is a notifyAll () in the run () method and a wait () in getlooper (). Why do you add these birds?

We found that in the handlerthread example, the creation of the handler is completed in the main thread. When creating, we need to call getloop() of handlerthread to obtain the constructor passed to the handler as a parameter, and the creation of the loop is created in the sub thread. There is a thread synchronization problem here. For example, when we call getloop(), run() in handlerthread The method has not been executed and the mloop variable has not been assigned. At this time, the wait () waiting logic is executed until the mloop in the run () method is assigned, and then notifyall() is executed immediately, and then getloop() can correctly return the mloop.

I see. If you don't know, you need to take some time to understand it. Well, the main part of the source code will be analyzed. I believe you have a certain understanding of handlerthread.

Handlerthread is relatively simple to understand. Well, this article is over. I hope it will be helpful to you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>