Summary of three methods to enable threads in Android

In multi-threaded programming, we often use three classes: handler, thread and runnable. Have you figured out the relationship between them?

First of all, the minimum unit of CPU allocation for Android is a thread, and the handler is generally created in a thread, so the handler and thread are bound to each other and correspond to each other one by one.

Runnable is an interface, and thread is a subclass of runnable. So they are both a process.

As the name suggests, handlerthread is a thread that can handle message loops. It is a thread with looper that can handle message loops.

Rather than binding a handler to a thread, it is better to say that a handler corresponds to a looper one by one.

Handler is the bridge between activity and thread / runnable. The handler runs in the main UI thread, and it and child threads can pass data through the message object

1. First, the first enabling method is to implement a thread by inheriting the thread class and overriding the run method

start-up

new Thread(new MyRunnable()).start();

2. The second enabling method creates a runnable object

start-up

new Thread(new MyRunnable()).start();

Another enabling method

3. The third enabling method starts the thread through the handler

The above summary of the three ways to enable threads in Android is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

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
分享
二维码
< <上一篇
下一篇>>