5 minutes read

You already know about the different ways to process multiple tasks simultaneously in Kotlin. One of them is internal concurrency, which involves working with the coroutines library. In this topic, you will learn about the official Kotlin coroutines library, understand why it is not built into the language, and see how to set it up.

What is kotlinx.coroutines

Coroutines are a Kotlin library developed by JetBrains. It is multiplatform and available for all major Kotlin targets: JVM, JS, Native, and Android.

The library provides many methods of dealing with simultaneous tasks in Kotlin. You might wonder what the specifics are, but let's take it step by step: before we can discuss the most useful methods in detail, we need to understand a few general things about coroutines.

An important fact about the library is that it is open-sourced, just like Kotlin itself. You can find the code in the GitHub repository, where anyone can see the process of development as well as report issues and suggest changes.

Another resource worth checking out is the official documentation web page where you can find a variety of guides and examples. Reading through the documentation can help you understand coroutines better.

kotlinx.coroutines as a library

Note that coroutines are not bundled with the Kotlin standard library — they are a dependency. If you haven't worked with external libraries before, don't worry: it's not a big deal. There are several reasons why coroutines are separated. Here are two major reasons:

  • Not every program needs internal concurrency. If coroutines were built-in, all Kotlin programs would take up more memory space because they contain redundant code.

  • Some developers don't want to use the official implementation of internal concurrency. When coroutines are a plugable module, you can easily plug not the official one but the one that best suits your particular situation.

In other words, making coroutines an external library gives the users more freedom. This way, no-one is forced to use this particular implementation or even use internal concurrency at all!

Maven dependency

One of the most convenient ways to add the kotlinx.coroutines library to the project is using a Maven dependency. Here, we will show how to do this in Gradle, but other systems will most likely have their own ways to add Maven dependencies. Feel free to check out the description on GitHub for the up-to-date information about different build systems and platforms.

The instructions are very easy. Just add the following lines to the build.gradle(.kts) file:

repository {
    mavenCentral()
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8")
}

In the above snippet, we first define the repository that Gradle will search for the library, and then specify the dependency itself. When we were writing this topic, the up-to-date version was 1.3.8; you can find the number of the latest version on GitHub.

Validation

You need to make sure that kotlinx.coroutines is installed properly. You can test this file:

import kotlinx.coroutines.delay

suspend fun main() {
    println("Hello")
    delay(500)
    println("World!")
}

If you did everything correctly, the program will compile and output will be the following:

Hello
World!

Conclusion

Now you know that coroutines come in a form of a library. You also understand how to set it up in your project. Let's make sure you're prepared to start working with coroutines by solving a few tasks!

83 learners liked this piece of theory. 7 didn't like it. What about you?
Report a typo