Concurrency in Android: Intro

When we talk about concurrency in Android the first thing that may come to our mind is AsyncTask; If so, think again. An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread.

That was just before that Google announced that the famous AsyncTask class is deprecated!

In the description Google says:

AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks or crashes on configuration changes. It also has inconsistent behavior on different versions of the platform, swallows exceptions from doInBackground, and does not provide much utility over using Executors directly.

Which was completely true! everybody who works with AsyncTask can talk about their experiences with AsyncTask and some unexplainable behavior that AsyncTask does. So maybe to deprecate it was the only solution!

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.

In the following posts, we are going to take a lock into the alternative(or the original) methods to implement concurrent tasks in Android os. We are going to solve some samples using the original java.util.concurrent package! Stay tuned!