I’m trying to find out the difference between a thread and a handler. Does creating a new handler create a new thread?. When a new handler is run using post(), is it creating a new thread? Please explain
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Threads are generic processing tasks that can do most things, but one thing they cannot do is update the UI.
Handlers on the other hand are bound to threads that allow you to communicate with the UI thread (update the UI).
So for example show a toast or a update a progress bar via a message (Runnable) posted to a handler but you can’t if you start this runnable as a thread.
With handler you can also have things like MessageQueuing, scheduling and repeating.
I am yet to encounter a situation where I needed a thread in android.
I mostly use a combination of AsyncTasks and Handlers.
Handlers for the aforementioned tasks.
AsyncTasks for download/ data fetching and polling etc.
You can read the developer article here “Painless Threading” for more threading in android.
Correction:
Each Handler instance is associated with a single thread and that thread’s message queue. They are not threads in their own behalf. as described here.