Why use HandlerThread in Android

牧云@^-^@ 提交于 2019-11-27 13:03:42

问题


In android , Handler can be used to post / handle message, 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 ?

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


回答1:


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).




回答2:


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...




回答3:


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.




回答4:


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?




回答5:


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




回答6:


Everyone seems to explain what it does and how it is used but forgot to explain that this thread needs to be cleanup after use by the developer, otherwise it leaks. After use you have to call

thread.quit()

to quit without processing message in the queue or

thread.quitSafely();

to process messages currently in the queue.



来源:https://stackoverflow.com/questions/10712959/why-use-handlerthread-in-android

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