Kotlin Coroutine: Beginner Steps

Chiebuka Edwin
3 min readNov 21, 2018

There has been a buzz in the Kotlin world about coroutines and how they are better than other methods of handling asynchronous code.

I took a deep dive to see if it fulfils this promise.

Kotlin 1.3 was released sometime in October, with a new concept Coroutines, coroutines have previously been in experimental in previous releases of the Kotlin language.

On a more basic note, a Coroutines are lightweight threads which are also simple to work with, as they follow the normal flow in the code which makes it easier for people with no knowledge of coroutines to understand what a coroutine code actually does.

In the example code below, we simulate a sequential code which runs on the main thread.

If you are writing a sequential task and you have long running activity on the main thread, it will block the main thread until the long-running task is completed.

Blocking the main thread actually means that until that function is completed, everything would be on a standstill.

The above code also runs sequentially. it firsts prints 11, then waits 5 seconds to print 22 and 7. it is just identical to the code using Thread.sleep, only that it uses Coroutines and not threads directly.

Three new terminologies were introduced to the code above suspend, delay and runBlocking.

I will try to explain them.

The delay function is like Thread.sleep function, only that it does not really block the thread during its execution but rather suspends the coroutine itself.

Suspend functions are functions that can be started, paused and resumed so that the long-running task in the function does not affect the main Thread of the application. so in the performATaskAsync function above, the code is paused at the delay function for other parts of the sequential code to execute first before coming back to complete the function execution when the delay time has elapsed.

The runBlocking is a coroutine context that serves as a bridge between regular blocking code to codes that could be suspended. so it basically runs a new coroutine context and blocks the current thread when needed.

It's basically a bridge to run suspend functions in the main thread.

Lets Write a Coroutine code that does not block the main thread on execution of a long running background task.

In the Code above we create an new Coroutine context using GlobalScope.launch which provides us with a separate thread for our long running method.

The launch context returns a Job object which could be used to know if we still have a background task running and also could be used to cancel the task, in our case we use the returned Job object to suspend the coroutine till the job is completed using job.join method, failure to do this would have resulted to our code printing only 8 and 4 with out waiting 2 seconds to print the 12 which is running in the background.

A much better way to achieve the above would be launching in the current Coroutine scope.

Async Await in Coroutines

Taking a cue from other programming language, coroutines also provide async await which helps you run background tasks in parallel.

What happens in the code above is that both background tasks run on separate threads in parallel. So instead of the the above code to take 8 seconds to run, it takes 5 seconds.

Next up is Kotlin Coroutines in an android world

Thanks for reading.

You can follow me on twitter with @harrric_edwin

--

--

Chiebuka Edwin

I build tools for the gospel currently learning to build and run a scalable business