Why use HandlerThread in Android

喜欢而已 提交于 2019-11-28 20:44:17

You would use HandlerThread in case that you want to perform background tasks one at a time and you want that those tasks will run at the order of execution.

For example if you want to make several network background operations one by one.

Yes, the HandlerThread has it's own looper and handlers could be created and post it, (so it would not block the main thread).

Normal way to use HandlerThread like this:

HandlerThread thread = new HandlerThread("A Handler Thread");
thread.start();
Handler handler = new Handler(thread.getLooper()){
    @Override
    public void handleMessage(Message msg) 
    {
    //....
    }
};

Because HandlerThread can create a Looper for Handler, it is a kind of convenient way.

When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- see official docs...

ρяσѕρєя K

As Doc says :

Handy class for starting a new thread that has a looper.
The looper can then be used to create handler classes.
Note that start() must still be called.

HanderThread class inherits from the Thread class, which encapsulates the Looper object, so that we do not care The Looper open and release details. Like in case of normal thread we need to use Looper.prepare() and Looper.loop() to convert it as a LooperThread.

if I don't use a HandlerThread (pass its Looper to Handler),does that mean in this case Handler use MainThread (UI Thread) 's Looper ?

Have a look at documentation of Handler

Handler ()

Default constructor associates this handler with the Looper for the current thread.

If your current thread is MainThread, it uses MainThread(UI Thread) Looper.

To explicitly associate Handler to your MainThread ( UI Thread), write below code.

Handler  mHandler = new Handler(Looper.getMainLooper();

If you write is as below, it uses HandlerThread Looper.

HandlerThread handlerThread = new HandlerThread("HandlerThread");
handlerThread.start();
Handler requestHandler = new Handler(handlerThread.getLooper());

If you have any network IO operation in Runnable task, you can't use Main thread looper. In that case, HandlerThread is handy to post Runnable task performing Network IO operation.

You can find example code @ How do I fix android.os.NetworkOnMainThreadException?

What result will get if Handler uses MainThread's Looper ? May cause mainThread blocked ?

If you send many events to MainThread Looper, they will execute on MainThread (UI Thread) itself. If there submitted tasks are taking more time for execution, MainThread will be blocked.

Check below post for internals of Looper:

What is the purpose of Looper and how to use it?

HandlerThread is useful when you want to execute a lot of background tasks, since it has its own looper. Normally if you post a message to a Handler it uses the MainThread's looper. This means that that the task is executed on the UI Thread. But in the case of HandlerThread, these tasks are executed on the worker thread. You can find a more detailed explanation here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!