Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between post(Runnable) and sendMessage(Message) in Handler

I just want to know what is the exact difference between using sendMessage (Message msg) and post (Runnable r).

Since both these methods are going to run in Main UI Thread even if we have Seperate Runnable.

like image 905
saravanan Avatar asked Jan 29 '17 04:01

saravanan


People also ask

What is the purpose of post () method related to a handler?

There are two methods are in handler. Post() − it going to post message from background thread to main thread using looper. sendmessage() − if you want to organize what you have sent to ui (message from background thread) or ui functions.

What is Handler and runnable?

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue . 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 a Looper .

What is the difference between thread and handler thread in android?

The main difference between Handler and Thread is that a handler is a function or a method that is capable of performing a specific task while a thread is a small, lightweight execution unit within a process.

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. run () : Starts executing the active part of the class' code.


1 Answers

Behind the scenes they actually call the same code. SO it isn't a big concern. SendMessage may be slightly more efficient (fewer objects used because the post will create a Message object), but by so little as to not matter at all. Using sendMessage you can add a data object as well as a runnable, but you can do that with a Runnable if you aren't using an anonymous one and pass it in via constructor.

So the long and short of it is there isn't much of one. Use whichever is more convenient (which tends to be post).

like image 164
Gabe Sechan Avatar answered Sep 25 '22 11:09

Gabe Sechan