Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a Thread and a Handler

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

like image 476
abat Avatar asked Feb 24 '12 18:02

abat


People also ask

What is the difference between handler vs AsyncTask vs thread?

Using Handlers you have the advantage of MessagingQueues , so if you want to schedule messages or update multiple UI elements or have repeating tasks. AsyncTask are similar, in fact, they make use of Handler , but doesn't run in the UI thread, so it's good for fetching data, for instance fetching web services.

What is a handler used for?

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 .

How many handlers can a thread have?

There can be any number of handlers for one single thread. So, the looper is providing the thread with the facility to run in a loop with its own MessageQueue.

What is the difference between looper and handler?

Each Looper has its own MessageQueue. Looper drains the MessageQueue. Handler is created for some particular Looper and is used to post messages to it and handle these messages when they are processed.


2 Answers

A thread defines a process running. Like you have a main (UI thread) in android. and all other threads run in background.(in parallel).

Handler is completely different, it is like initiating the task defined in a handler..

To clear out your confusion, and perform threading in android you must read : http://android-developers.blogspot.com/2009/05/painless-threading.html

and i would suggest AsyncTask instead of using Thread in all cases.

like image 31
Rahul garg Avatar answered Oct 13 '22 12:10

Rahul garg


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.

like image 121
MahdeTo Avatar answered Oct 13 '22 11:10

MahdeTo