Kotlin

Kotlin Coroutines Examples

The Kotlin programming language has a potent feature called Kotlin coroutines that makes asynchronous and concurrent programming easier. It gives the programmers a mechanism to create a simultaneously executable code without stopping the execution thread which enables them to create the applications that are more effective, scalable, and responsive. Coroutines are controlled by the Kotlin runtime, as opposed to conventional threads, which are controlled by the operating system. They can all be started, stopped, resumed, and canceled while still being in the middle of their execution.

In this guide today, we will demonstrate the use of coroutines using the Kotlin code examples.

Syntax:

Here’s the general syntax of coroutines in Kotlin:

Import the necessary packages and define a suspend function.

import kotlinx.coroutines.*
suspend fun YourSuspendFunction() {}

 

Create a coroutine using one of the coroutine builders.

val YourCoroutine = CoroutineScope(Dispatchers.Main).launch {}

 

Use the suspending functions within the coroutine using the suspend modifier.

suspend fun YourOtherSuspendFunction() {}

 

Use delay to suspend the coroutine for a specific duration and use the withContext() function to switch the coroutine context.

val YourCoroutine = CoroutineScope(Dispatchers.Main).launch {
    YourSuspendFunction()
    YourOtherSuspendFunction()
}

 

Knob the error within coroutine using the try-catch blocks.

val YourCoroutine = CoroutineScope(Dispatchers.Main).launch {
    try {
        //  logic
    } catch (e: Exception) {
        // Exception handling
    }   }

 

Use async to perform the computations concurrently and retrieve the results.

Cancel a coroutine using cancel or cancelAndJoin.

val myCoroutine = CoroutineScope(Dispatchers.Main).launch {
    // logic
    cancel()
    // logic after cancellation
}

 

Example 1:

To understand coroutines, we try the following Kotlin code example. This code demonstrates the usage of coroutines in Kotlin using the “kotlinx.coroutines” library. Let’s go through the code step-by-step to understand it.

The first line imports all the necessary classes and methods from the “kotlinx.coroutines” package to work with coroutines using the “import” keyword. The “fun” keyword is utilized to start the main() function which is the entry point of this program. The println(“Before coroutine”) line prints the “Before coroutine” message to the console using Kotlin’s “println” function. GlobalScope.launch is a function that generates a new coroutine in the overall scope. It allows you to execute the code concurrently without blocking the main thread or tying the coroutine to any specific context or lifecycle. Therefore, we use the GlobalScope.launch here to create a new coroutine.

The “launch” function creates a new coroutine in the context. The coroutine code is defined within a block that starts with “{“ and ends with “}”. Inside the coroutine block, the delay() function is called with the “2000” value. The delay method is a hanging method that pauses the execution of the coroutine for a definite time. In this case, it suspends the coroutine for 2 seconds (2000 milliseconds).

After the “delay” function, println(“Inside coroutine”) is executed. It displays the “Inside Coroutine” message to the console. Once the coroutine block is finished, the next line println(“After coroutine”) is executed. It displays the “After coroutine” text to the console. To ensure that the coroutine has enough time to complete its execution, the main thread pauses using the Thread.sleep() function. The Thread.sleep() function suspends the main thread for the stated time in milliseconds. In this case, it pauses for 3 seconds (3000 milliseconds). After the pause, the program finishes, and the execution terminates.

import kotlinx.coroutines.*
fun main() {
    println("Before coroutine")
    GlobalScope.launch {
        delay(2000) // Suspend the coroutine for 1 second
        println("Inside coroutine")
    }
    println("After coroutine")
   Thread.sleep(3000)
}

 

The “Before coroutine” and “After coroutine” messages are printed immediately because they are executed on the main thread. However, the “Inside coroutine” message is printed after a delay of 1 second because it is executed inside the launched coroutine. The main thread is then paused for an additional 2 seconds which allows the coroutine to complete its execution and print its message before the program terminates.

Example 2:

Now, it’s time to demonstrate the usage of coroutines for asynchronous programming. Therefore, we provided another Kotlin code example. Let’s explain the code step-by-step.

The code starts with the import of the kotlinx.coroutines package which provides support for coroutines in Kotlin code. Here, the performTask() function is defined as a suspend function. The suspend modifier indicates that this function can be suspended which allows the other code to execute while waiting for some operation to complete.

In this case, the performTask() function simulates some task that takes 2 seconds to complete using the “delay” function from the kotlinx.coroutines package. After the delay, it throws a RuntimeException with the “Something went wrong” message. The main function is defined without any arguments. It uses the runBlocking function, also from the kotlinx.coroutines package, to create a new coroutine scope that slabs the main function until all coroutines that started in this scope are done.

Inside the runBlocking scope, we use the exception handling using the try-catch block. This is done to handle any exceptions that may happen throughout the processing of the coroutines. The async() method is called within the try block. The async() function is used to launch a new coroutine that performs a computation asynchronously. It yields a Deferred object that denotes the output of the computation.

In this case, the performTask function is invoked inside the async block. The await() function is called here on the Deferred object that is given back by async. This function suspends the coroutine until the output of the computation is provided. Once the result is available, it is allocated to the output variable. Finally, the result is printed using the “$result” variable in the println() function of Kotline, i.e. “Result: $result”. If an error arises within the processing of the coroutines, it will be caught by the catch block. The caught exception is assigned to the variable “e”, and the error message that it holds is printed using the println() function statement as “Error: ${e.message}”.

import kotlinx.coroutines.*
suspend fun performTask(): String {
    delay(2000)
    throw RuntimeException("Something went wrong")
}
fun main() = runBlocking {
    try {
        val result = async { performTask() }.await()
        println("Result: $result")
    } catch (e: Exception) {
        println("Error: ${e.message}")
    }
}

 

It launches a coroutine using the async function, performs a task asynchronously inside that coroutine using the performTask() function, and handles any exceptions that may occur during the execution.

Conclusion

Overall, Kotlin Coroutines offers a strong and adaptable foundation for producing organized, sequential asynchronous, and concurrent programming, as clear from the provided code examples. They provide the programmers with the ability to make use of concurrent execution advantages while avoiding the complications that are brought on by conventional threading models which results in high-performance, scalable, and responsive systems. The primary advantage of employing coroutines is the capacity to write the asynchronous code imperatively and sequentially, much like blocking the code. As a result, the code is easier to comprehend, maintain, and write than in a more conventional callback.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.