Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does handler.post(runnable) start a new thread?

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
    }
});
like image 370
xil3 Avatar asked Feb 06 '12 16:02

xil3


People also ask

Does handler run on the main thread?

Short answer: they all run on the same thread. If instantiated from an Activity lifecycle callback, they all run on the main UI thread.

What is the difference between thread and handler 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.

Does handler create a new 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.

What is Post runnable in Android?

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.


1 Answers

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

like image 51
waqaslam Avatar answered Sep 28 '22 01:09

waqaslam