tags:

views:

122

answers:

2

What is the difference when a class extend from Handler and Thread?

As described in developer.android.com
... Each Handler instance is associated with a single thread and that thread's message queue. ...

Does the thread has no message queue ?

Any benefit for a class extend from Handler?

+3  A: 

There is a major difference between a Thread and a Handler.

The Android Handler class is used for communication between other Runnable/Thread and the one that it has been created in. By posting to a given Handler, you can add something for execution on its thread. You can also send messages from one thread and handle them on another.

Using Handler, for example, is the preferred way to do delayed execution, instead of using TimerTask. You can also notify your main thread that your worker thread has finished some task, with Handler as an alternative of sending Intent.

From Android Developer's site:

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

Dimitar Dimitrov
+1  A: 

Handlers are able to send and receive messages, perform various operations.

At low level, each handler has its own thread, but Handlers can communicate each other.

For instance, you can have a handler doing some stuff, and the sending results to another handler and so on.

LucaB