Search code examples
androidmultithreadingandroid-asynctaskandroid-handler

Handler vs AsyncTask vs Thread


I got slightly confused about the differences between Handlers, AsyncTask and Threads in Android. I've read quite a few blogs and questions here in StackOverflow.

Handler are background threads that provide you to communicate with the UI. Updating a progress bar, for instance, should be done via Handler. 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. Later you can interact with the UI.

Thread however can't interact with the UI, provide more "basic" threading and you miss all the abstractions of AsyncTask.

However, I would like to have a socket connection run in service. Should this be run in a handler or a thread, or even an AsyncTask? UI interaction is not necessary at all. Does it make a difference in terms of performance which I use?

Meanwhile, the documentation has been majorly improved.


Solution

  • As the Tutorial on Android background processing with Handlers, AsyncTask and Loaders on the Vogella site puts it:

    The Handler class can be used to register to a thread and provides a simple channel to send data to this thread.

    The AsyncTask class encapsulates the creation of a background process and the synchronization with the main thread. It also supports reporting progress of the running tasks.

    And a Thread is basically the core element of multithreading which a developer can use with the following disadvantage:

    If you use Java threads you have to handle the following requirements in your own code:

    • Synchronization with the main thread if you post back results to the user interface
    • No default for canceling the thread
    • No default thread pooling
    • No default for handling configuration changes in Android

    And regarding the AsyncTask, as the Android Developer's Reference puts it:

    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.

    AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

    Update May 2015: I found an excellent series of lectures covering this topic.

    This is the Google Search: Douglas Schmidt lecture android concurrency and synchronisation

    This is the video of the first lecture on YouTube

    All this is part of the CS 282 (2013): Systems Programming for Android from the Vanderbilt University. Here's the YouTube Playlist

    Douglas Schmidt seems to be an excellent lecturer

    Important: If you are at a point where you are considering to use AsyncTask to solve your threading issues, you should first check out ReactiveX/RxAndroid for a possibly more appropriate programming pattern. A very good resource for getting an overview is Learning RxJava 2 for Android by example.