If the handler was instantiated in the main UI thread, does a post with a Runnable create a child thread that gets added to the message queue, or does it just get run in the UI thread?
handler.post(new Runnable(){
public void run() {
// do stuff
}
});
Short answer: they all run on the same thread. If instantiated from an Activity lifecycle callback, they all run on the main UI thread.
When you use new Thread(r). start() , you actually created a new thread and run task asynchronously. When you use new Handler(). post(r) (or Message ), you added the Runnable object to Looper and execute the code later in the same thread.
You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same post or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.
post :post causes the Runnable to be added to the message queue, Runnable : Represents a command that can be executed. Often used to run code in a different Thread.
No, it doesn't create a new thread. It simply executes your runnable on the thread your handler is attached to, which in this case means your UI thread
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