Nerd For Tech
Published in

Nerd For Tech

A journey from callback hell to Kotlin coroutines, episode 2

Photo by Marc Reichelt on Unsplash

You can find episode 1 here

Okay, now, after taking his nap, Ahmed felt good. He started his daily routine by Praying, taking his breakfast, then he continued researching about coroutines.

He wanted to know what’s behind that suspend keyword, he kept his progress reading,

Coroutines is the idea that a function can suspend its execution at some point and resume later on.

Ahmed is almost convinced to choose coroutines over other solutions. but wanted to write some code to see it in action. He found the following code snippet to show a simple coroutine at kotlinlang website

code snippet to show a simple coroutine, kotlinlang

He found the following output:


Interesting, but what is Scope and launch?, Ahmed asked, he then kept reading to get the answer:

Coroutine context and dispatchers

Coroutines always execute in some context…, The coroutine context is a set of various elements. The main elements are the Job of the coroutine…, and its dispatcher.

The coroutine context includes a coroutine dispatcher (see CoroutineDispatcher) that determines what thread or threads the corresponding coroutine uses for its execution. The coroutine dispatcher can confine coroutine execution to a specific thread, dispatch it to a thread pool, or let it run unconfined.

You can call launch{} and async{} functions with optional CoroutineContext, to determine which thread this coroutine uses for its execution.

Running the above snippet, Ahmed saw the following output:

Unconfined            : I'm working in thread main
Default : I'm working in thread DefaultDispatcher-worker-1
newSingleThreadContext: I'm working in thread MyOwnThread
main runBlocking : I'm working in thread main

So, after kept reading here, he got a good idea of how to run a coroutine.

Checking the performance

Te make sure that coroutines are light-weight, and not as expensive as real threads, Ahmed decided to run a million thread and do the same with coroutines, :D

snipper from kotlinlang, running 1M thread

This runs a 1'000'000 threads each of which adds to a common counter. This took more than 4 minutes and 11 seconds on his machine.

Lets see how long it took when he did the same with coroutines

snipper from kotlinlang, running 1M coroutines

This example completed in less than a second 😮, but it prints some arbitrary number, because some coroutines don’t finish before main() prints the result. Let's fix that.

This printed the correct sum.

He also wanted to make sure that our coroutines actually run in parallel. He add a 1-second delay() to each of the async 's, the resulting program shouldn't run for 1'000'000 seconds (over 11,5 days):

This took about 10 seconds , so yes, coroutines do run in parallel.

At that point, Ahmed was totally convinced to use coroutines over other solutions to handle threading and async. code in his kotlin applications.

He had a good idea about Coroutines to start using them, but of course he needs to read more and do practice in some real-world project. So maybe we can see what he found in next episodes :)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store