Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which thread does Runnable run on?

I want to update UI every 100ms. After searching in StackOverflow, I found a solution using Runnable and Handler like this

final Handler handler = new Handler();
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        //update UI here

        handler.postDelayed(this, 100);
    }
};
runnable.run();

It works! But I have some questions:

  1. Which thread does this Runnable run on? MainThread or another thread? Here is the docs about postDelay enter image description here

Handler is attached MainThread, so is Runnable running on MainThread?

  1. If Runnable is running on MainThread, why needs Handler? According to my knowledge, Handler is used to send messages between two threads
like image 219
Qk Lahpita Avatar asked Dec 06 '17 08:12

Qk Lahpita


2 Answers

Which thread does this Runnable run on?

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        //update UI here

        handler.postDelayed(this, 100);
    }
};
runnable.run()

This Runnable runs on the current thread, i.e. the thread that invokes this code. It doesn't magically create, or constitute, another thread. Runnable.run() is only a method call.

The subsequent executions of this thread, by the Handler, in whatever thread the Handler runs in, do essentially nothing except reschedule themselves. It's hard to believe this is a solution to anything.

like image 163
user207421 Avatar answered Oct 03 '22 22:10

user207421


Handler is attached MainThread, so is Runnable running on MainThread?

From Handler documentation:

Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

If you want to run your Runnable on different Thread, you can use HandlerThread .

Related post:

Why use HandlerThread in Android

If Runnable is running on MainThread, why needs Handler? According to my knowledge, Handler is used to send messages between two threads

There are two main uses for a Handler:

  1. To schedule messages and runnables to be executed as some point in the future
  2. To enqueue an action to be performed on a different thread than your own.

If you are using only MainThread,Handler is useful to send message at some point of time in future. If you are using different Threads, Handler is useful to communicate between the threads.

like image 38
Ravindra babu Avatar answered Oct 03 '22 23:10

Ravindra babu