What are the main differences between Handler, Thread and AsyncTask? [duplicate]

5

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

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

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

    
asked by anonymous 25.08.2015 / 13:20

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 have multitasking behavior. In Java, the Thread class 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 does not come to the case).

Handlers and Threads

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

For a thread to loop, you must create a message queue for it by calling the methods Looper.prepare() and Looper.loop() within the thread itself. In the case of the main thread the system itself already does these steps, so we already found it in loop by default.

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

AsyncTasks

A AsyncTask is a class that allows you to execute three code snippets in sequence: the first will be executed by the main thread of UI), the second by a secondary thread , and the third again from the main thread . 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 in Android (for example: triggering a "load" animation, running a background task, and then interrupting the animation ).

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

    
25.08.2015 / 13:29
3

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

  

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 the proper and easy use of UI threads. That   class allows you to perform background operations and post results   in Thread UI without having to manipulate Threads and / or Handlers

What about Handlers:

  

Handler allows you to send and process Message and Runnable objects   associated with a thread's MessageQueue

Translating:

  

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

Each Thread has a Fila de Mensagens in which it searches to process them until the queue is empty. This message queue can be processed after using Looper.prepare () and Looper.loop (), as reminded by @Piovezan in the comments and by this answer in SOen . Handler sends messages to this queue for processing.

These were the important hot spots in SOen. You can look at it in more detail.

    
25.08.2015 / 13:37
1

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

So there is no secret between AsyncTask and Handler . They just make it easier for us to work as a developer.

For example, if Program A calls the A () method, method A () could run on a different thread with the R program. You can easily check this out 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 Thread ), so anything you can do with Handler or AsyncTask , you can achieve using Thread as well.

What are the advantages of using Handler or AsyncTask ?

The most obvious reason is communication between the caller thread and the worker thread .

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

The difference between Handler and AsyncTask is: Use AsyncTask when Caller thread is the UI Thread . The Android documentation says:

  

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

    
25.08.2015 / 13:59
0

Dude, it's simple. The 2 (Thread and AsyncTask) have the same end goal, which is to process some action in backgroud, give a progress response if needed, and a final response (Handler). AsyncTask is the coolest way to do all of this, it came up later and is suggested for use, as well as being much easier to use.

To complement, handler is a way to display a user response through a Thread. AsyncTask already has this implemented in the class, making it easy to use.

I hope I have helped!

    
25.08.2015 / 20:32