What are the main differences between Handler, Thread and Asynctask?

Asked

Viewed 2,064 times

5

The Android documentation can end up being a little confusing for those who are starting to understand better what are the differences between a Handler, one Thread and a AsyncTask.

Handlers sane background Threads that enable communication with the user interface. Updating a progress bar, for example, can be done with a Handler, but can also be created with a AsyncTask, which somewhat confuses understanding on this topic.

What are the main differences amid Handler, Thread and AsyncTask?

  • No. I want something that exemplifies difference among the three.

  • 2

    Handler and thread are different things, can’t compare. Asynctask internally uses an Handler and a thread.

  • 1

    are different things but have a connection and can interact. I think what he wants is the explanation of how and when this happens. Not necessarily compare them.

  • Exactly, I’m looking to differentiate the three given that they work in the same context.

4 answers

6


Thread

A thread is a line of code execution within an application. An application can have multiple threads running at the same time. In other words, threads allow an application to behave multitasking. In Java, the class Thread is a representation of a thread of the Java Virtual Machine or JVM (which does not necessarily correspond to a thread of the host operating system, but this is not the case).

Handlers and Threads

For the difference between Handler and Thread, see this question. Note that Handlers are not Threads; Handlers have that name because they serve to deliver messages (a message or Message is basically the encapsulation of a Runnable, that is, a chunk of executable code) for threads who stay in loop awaiting the arrival of these code snippets to execute.

For a thread to stay in loop, it is necessary to create a message queue for it calling the methods Looper.prepare() and Looper.loop() within itself thread. In the case of thread the system itself already makes these steps, so we have already found it in loop by default.

We used a Handler when we want a thread secondary perform many messages, or when we want to deliver a message to the thread main. In the latter case we can avoid the use of the Handler through the method Activity.runOnUiThread() (if we are in a Activity), or through a AsyncTask.

Asynctasks

One AsyncTask is a class that allows you to execute three code snippets in sequence: the first will be executed by thread leading (thread UI), the second by one thread secondary, and the third again by the thread main. Underneath the plans this is implemented using Threads and Handlers.

AsyncTasks are intended to simplify the implementation of this sequence of steps, which is very common to happen on Android (for example: fire an animation of "loading", perform a task in the background, and then interrupt the animation).

This sequence is made thus, separated into threads and not all in the same thread, because the thread main is reserved to update the screen and can not perform tasks in the background under penalty of losing responsiveness.

3

AssyncTask and Handler are classes used to facilitate your work as a developer.

Asynctask Enables Proper and easy use of the UI thread. This class Allows to perform background Operations and Publish Results on the UI thread without having to Manipulate threads and/or handlers

Translating:

Assynctask allows proper and easy use of UI threads. This class allows you to run background operations and publish results in the Thread UI without having to manipulate Threads and/or Handlers

What about the Handlers:

Handler Allows you to send and process Message and Runnable Objects Associated with a thread’s Message

Translating:

Handlers allow you to send and process Message Objects and Executables associated with a Thread Message Queue.

Each Thread has a Fila de Mensagens where she seeks to process them until the line is empty. This message queue can be processed after using Looper.prepare() and Looper.loop(), as @Piovezan recalled in the comments and by that answer in Soen. The Handler send messages to that queue for them to be processed.

These were the important points of that answer on Soen. You can look at it in more detail.

  • 1

    Remember that a Thread by default is not in loop nor has a queue of messages. To have this behavior you need to call Looper.prepare() and Looper.loop() within the thread itself. The main thread already comes with this behavior.

  • well remembered, includes in the answer

1

If we look at the source code of AsyncTask and of Handler we can realize that the code is written purely in Java. (Of course, there are some exceptions , but this is not the most important).

So there’s no secret between AsyncTask and Handler. They only facilitate our work as a developer.

For example: if the Program A call method A() , method A() could be executed in a different segment with the Program R. You can easily check this using:

Thread t = Thread.currentThread();    
int id = t.getId();

Why should we use a new Thread? Many, many reasons.

Handler and AsyncTask are written in Java (internally they use a Thread), then all you can do with Handler or AsyncTask, you can achieve using a Thread also.

What are the advantages of using the Handler or the AsyncTask?

The most obvious reason is communicating between the Caller thread and the worker thread.

  • Caller Thread: The thread that calls the Worker Thread to perform a task. Caller Thread does not need to be the main thread). Of course, you can also communicate between two threads through other ways, but there are many disadvantages (and possible flaws) due to thread security issues.

The difference between Handler andAsyncTask is: Use AsyncTask when Caller thread is the UI Thread. To android documentation says that:

AsyncTask enables the coherent and easy to use Thread UI. This class allows you to perform background operations and then publish the results to the thread UI without worrying about threads or handlers handling the process.

  • The usefulness of AsyncTask is well explained, just need to explain what is a Handler in terms that a casual reader understands and also clarifies for what it serves (for example, although Handlers can post messages in worker threads, it makes no sense to use a Handler for a thread who is not in loop awaiting such messages, as in the case of a thread that will perform only one task).

0

Man, it is simple. The 2 (Thread and Asynctask) have the same ultimate goal, which is to process some action in backgroud, give a progress response if necessary, and a final response (Handler). Asynctask is the coolest way to do all this, it came up later and is suggested for use, besides being much easier to use.

To complement, Handler is a way to display a response to the user through a Thread. Asynctask already has this implemented in the class, making it easier to use.

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.