DEV Community

Cover image for Dispatchers in Kotlin Coroutines
Amit Shekhar
Amit Shekhar

Posted on • Edited on • Originally published at outcomeschool.com

Dispatchers in Kotlin Coroutines

Hi, I am Amit Shekhar, Co-Founder @ Outcome School • IIT 2010-14 • I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

In this blog, we are going to learn about the Dispatchers in Kotlin Coroutines including the types of Dispatchers available for use in Coroutines.

This article was originally published at Outcome School.

These concepts are important when it comes to Android Interviews.

Before jumping into the types of Dispatchers, we must know how Dispatchers help Coroutines when we use them in our Android project.

Dispatchers help Coroutines in deciding the thread on which the task has to be done. We use Coroutines to perform certain tasks efficiently. Coroutines run the task on a particular thread. This is where the Dispatchers come into play. Coroutines take the help of Dispatchers in deciding the thread on which the task has to be done.

Dispatchers in Kotlin Coroutines are like Schedulers in RxJava.

Very frequently we use the following Dispatchers in our Android project:

  • Dispatchers.Default
  • Dispatchers.IO
  • Dispatchers.Main

However, when we go through the documentation, we have one more:

  • Dispatchers.Unconfined

So, there are 4 types of Dispatchers in Kotlin Coroutines:

  • Dispatchers.Default
  • Dispatchers.IO
  • Dispatchers.Main
  • Dispatchers.Unconfined

Today, we will learn about all of them one by one with the example use cases. Let's start with the Dispatchers.Default.

Dispatchers.Default

We should use Dispatchers.Default to perform CPU-intensive tasks.

Example use cases:

  • Doing heavy calculations like Matrix multiplications.
  • Doing any operations on a bigger list present in the memory like sorting, filtering, searching, etc.
  • Applying the filter on the Bitmap present in the memory, NOT by reading the image file present on the disk.
  • Parsing the JSON available in the memory, NOT by reading the JSON file present on the disk.
  • Scaling the bitmap already present in the memory, NOT by reading the image file present on the disk.
  • Any operations on the bitmap that are already present in the memory, NOT by reading the image file present on the disk.

So, now we know where we can use the Dispatchers.Default.

We can think of Dispatchers.Default as a Schedulers.computation() of RxJava.

launch(Dispatchers.Default) {
    // Your CPU-intensive task
}
Enter fullscreen mode Exit fullscreen mode

Now, it's time to learn about the Dispatchers.IO.

Dispatchers.IO

We should use Dispatchers.IO to perform disk or network I/O-related tasks.

Example use cases:

  • Any network operations like making a network call.
  • Downloading a file from the server.
  • Moving a file from one location to another on disk.
  • Reading from a file.
  • Writing to a file.
  • Making a database query.
  • Loading the Shared Preferences.

So, now we know where we can use the Dispatchers.IO.

In a nutshell, anything related to file systems or networking should be done using Dispatchers.IO as those tasks are IO-related tasks.

We can think of Dispatchers.IO as a Schedulers.io() of RxJava.

launch(Dispatchers.IO) {
    // Your IO related task
}
Enter fullscreen mode Exit fullscreen mode

Now, it's time to learn about the Dispatchers.Main.

Dispatchers.Main

We should use Dispatchers.Main to run a coroutine on the main thread of Android. We all know where we use the main thread of Android. Mainly at the places where we interact with the UI and perform small tasks.

Example use cases:

  • Performing UI-related tasks.
  • Any small tasks like any operations on a smaller list present in the memory like sorting, filtering, searching, etc.

So, now we know where we can use the Dispatchers.Main.

We can think of Dispatchers.Main as a AndroidSchedulers.mainThread() of RxAndroid(RxJava bindings for Android).

launch(Dispatchers.Main) {
    // Your main thread related task
}
Enter fullscreen mode Exit fullscreen mode

Now, it's time to learn about the Dispatchers.Unconfined.

Dispatchers.Unconfined

As per the official documentation: A coroutine dispatcher that is not confined to any specific thread. It executes the initial continuation of a coroutine in the current call frame and lets the coroutine resume in whatever thread that is used by the corresponding suspending function, without mandating any specific threading policy.

It does not change the thread. When it is started, it runs on the thread on which it was started. If it is resumed, it runs on the thread that resumed it.

If I put it simply: We should use Dispatchers.Unconfined when we do not care where the coroutine will be executed.

launch(Dispatchers.Unconfined) {
    // Your task for which you do not care about the thread on which it should run.
}
Enter fullscreen mode Exit fullscreen mode

Actually, I have not used Dispatchers.Unconfined in any of my Android projects so I am not adding any example use cases. If I find anything, I shall add that here.

This was all about the Dispatchers in Kotlin Coroutines.

That's it for now.

Thanks

Amit Shekhar

Co-Founder @ Outcome School

You can connect with me on:

Read all of our blogs here.

Top comments (0)