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:
Runnable
run on? MainThread or another thread? Here is the docs about postDelay
Handler
is attached MainThread, so is Runnable
running on MainThread?
Runnable
is running on MainThread, why needs Handler
? According to my knowledge, Handler
is used to send messages between two threadsWhich 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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With