Why use HandlerThread in Android

19.6k views Asked by At

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 ?

6

There are 6 answers

0
ρяσѕρєя K On

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.

0
Ravindra babu On

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?

0
prak253 On

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

0
The_Martian On

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.

0
oznus On

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

1
Richard On

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