Asynchronous Apis Made easy (Kotlin code)

Today we will be covering a small subset of the dozens of ways one can perform asynchronous processing in Android, along with some Apis provided by Java, and known third party libraries.


To execute the AsyncTask, the following is done:

A list of strings is passed in as required by the type.

The execute() method is part of the AsyncTask class.

For those newbies learning to use the AsyncTask, and for those oldies who forgot how to use it, this is a very simple/straight forward example of us performing a background task on a list of strings.

The onPreExecute runs on its callers thread, that is the thread that called execute(), and is called before the doInBackground method is called and it runs a background thread.

The doInBackground returns some result which is then received in the onPostExecute, which runs in the main thread.


The code is very straight forward and self explanatory. A thread value is created, with a runnable passed in. The code inside the runnable gets called on a separate thread.


Handler usage is just as easy as Thread. A handler runs on a handler thread. The HandlerThread is a direct sub class of Thread (extends it) that takes in a thread name, and/or a priority using one of its two constructor methods. Just like the name suggests, this type of thread is for Handlers. The HandlerThread provides a looper which is used by its handler to to run a piece of code in the looper specified. The Handler uses this looper to determine what thread to run on (Note if no looper is passed in, it will just run on the current threads looper – in other words it will just run in the same thread it was called from).


Another easy threading example, the Timer class is a facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals


Time to step it up a noch.

The comments are very clear but there is something that we need to play closer attention to. Take a look at the subscribeOn and the observeOn calls. By carefully analyzing what is going we, we can see that subscribeOn runs every call that was made before it was called on the specified thread, and the observeOn calls everything that was called after in the specified thread. What happens if we call subscribeOn AFTER an observeOn call?

Lets take a look at this code and what gets printed out

This prints the following:

From looking at the log we can see that the SubscribeOn has no effect after an observeOn has been called. This is very important to keep in mind, when i started with RxJava, i made this exact same mistake and being an Rx noobie, took me sometime to figure out. The reason for this is because

…the SubscribeOn operator designates which thread the Observable will begin operating on, no matter at what point in the chain of operators that operator is called. ObserveOn, on the other hand, affects the thread that the Observable will use below where that operator appears. For this reason, you may call ObserveOn multiple times at various points during the chain of Observable operators in order to change on which threads certain of those operators operate.

Theres a great article talking about this more in depth here.


Introduced in Java 5, a Future

represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.

But this class has been improved a couple of times such as in Googles Guava library as a ListenableFuture, and also CompletableFutures (Java 8). Listenable and Completable futures are futures that allow the users to chain calls similar to RxJava and its operators.

Here is an example of what a normal Future (Java 5) implementation looks like

To run the future we just do

CompletableFutures example:

A CompletableFuture future is a Future that may be explicitly completed (setting its value and status), and may be used as a CompletionStage, supporting dependent functions and actions that trigger upon its completion.

and same as the future, to use it we just call the execute() method

Very similar to RxJava isn’t it? Kinda fun to play around with actually.

For the curious ones, there is a nice guide on CompletableFutures here.


There are other asynchronous APIs you can play with such as Java 9’s Reactive Streams, IntentServices, and Loaders, Job Schedulers, GcmNetWorkManager, Android Job (one of my favorites), Sync adapters, and dozens more.

Complete source coed can be found here.

Thank you for reading, Thats all for now!

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *