Coroutines in Kotlin

07 / Jun / 2024 by Hemant Kumar 0 comments

In modern applications, especially those involving network requests, file I/O, or complex computations, handling asynchronous operations is essential to maintain responsiveness and performance. Traditionally, developers have used callbacks, threads, futures, and AsyncTask in Android to manage asynchronous tasks. However, these methods often lead to several challenges.

This diagram briefly explains the difference between ‘Synchronous’ and ‘Asynchronous’ task execution.

Challenges of Asynchronous Calls

  1. Callbacks: Callbacks are functions passed as arguments to other functions, to be executed after the completion of a task. This can lead to “callback hell” or “pyramid of doom,” where nested callbacks become hard to read and maintain.
  2. Threads: Threads allow parallel execution but come with the overhead of managing thread creation, synchronization, and potential issues like thread exhaustion.
  3. Futures and Promises: Futures provide a way to handle asynchronous results but can be cumbersome, especially when chaining multiple asynchronous operations.
  4. AsyncTask (Android-specific): AsyncTask was a common way to perform background operations in Android without blocking the main thread. However, it has several drawbacks:
    Memory Leaks: If an `AsyncTask` is not properly managed, it can cause memory leaks by holding a reference to the enclosing `Activity` or `Fragment`.
    Configuration Changes: `AsyncTask` does not handle configuration changes (like screen rotations) gracefully, leading to potential crashes or lost results.
    Lifecycle Management: Managing the lifecycle of `AsyncTask` is complex and error-prone, especially with nested or multiple tasks.

Why We Need Coroutines and What They Solve

Kotlin coroutines offer a more manageable way to handle asynchronous operations by allowing you to write non-blocking code sequentially. They provide:

  • Simplified Concurrency: Coroutines eliminate the need for callbacks, reducing complexity and making code easier to read and maintain.
  • Structured Concurrency: Coroutines provide a structured approach to concurrency, where the lifecycle of coroutines is tied to their scope, ensuring proper cancellation and resource management.
  • Lightweight Threads: Coroutines are lightweight, allowing you to run thousands concurrently without the overhead of traditional threads.
  • Android-Specific Solutions:
    • Lifecycle Awareness: With coroutine scopes tied to lifecycle components like viewModelScope, you can easily manage coroutines’ lifecycles, preventing memory leaks and ensuring proper cancellation.
    • Main-Safe Operations: Using Dispatchers.Main, you can safely update the UI from coroutines without blocking the main thread.
    • Handling Configuration Changes: Coroutines can seamlessly handle configuration changes by using scopes that respect the Android lifecycle.

Creating Coroutines

Kotlin provides several ways to create and manage coroutines. The most common coroutine builders are launch, async, and withContext.

launch

The `launch` function creates a new coroutine that executes a block of code asynchronously. It returns a Job representing the coroutine’s lifecycle.

import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        println("Coroutine started")
        delay(1000) // Simulate a task with delay
        println("Coroutine completed")
    }

    println("Main thread continues to execute")
    job.join() // Wait for the coroutine to complete
    println("Main thread waits for the coroutine to finish")
}
  • `delay`: This function pauses the coroutine for a specified time without blocking the underlying thread.
  • `join`: This function waits for the coroutine to complete.

async

The `async` function creates a new coroutine that executes a block of code asynchronously and returns a `Deferred` representing the result of the coroutine’s computation.

import kotlinx.coroutines.*

fun main() = runBlocking {
    val deferred = async {
        println("Async computation started")
        delay(1000) // Simulate a long-running computation
        42 // Computation result
    }

    println("Main thread continues to execute")
    val result = deferred.await() // Await the result of the computation
    println("Async computation result: $result")
}
  • `await`: This function waits for the result of the Deferred coroutine.
  • If you don’t use `await`, the coroutine will still run, but the result won’t be retrieved:

withContext

The `withContext` function allows you to switch the context of a coroutine temporarily while preserving the coroutine’s state.

import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Main) {
        println("Running on Main Dispatcher")

        val data = withContext(Dispatchers.IO) {
            println("Fetching data on IO Dispatcher")
            delay(1000) // Simulate network request
            "Data from network"
        }

        println("Received data: $data")
    }
}

Coroutine Scope

A coroutine scope defines the lifecycle and boundaries of the coroutines launched within it. It ensures that all coroutines started within the scope are cancelled when the scope itself is cancelled.

Using `coroutineScope` Function:

The coroutineScope function creates a new coroutine scope and suspends the execution until all coroutines within the scope are complete. It’s typically used for managing a group of related coroutines.

import kotlinx.coroutines.*

fun main() = runBlocking {
    coroutineScope {
        launch {
            delay(1000)
            println("Task 1 from coroutineScope")
        }
        launch {
            delay(1500)
            println("Task 2 from coroutineScope")
        }
    }
    println("coroutineScope is over")
}

Using `GlobalScope`:

GlobalScope starts coroutines at the global level, independent of any lifecycle. It’s suitable for long-running background tasks that need to persist throughout the application’s lifetime.

import kotlinx.coroutines.*

fun main() {
    GlobalScope.launch {
        delay(1000)
        println("Task from GlobalScope")
    }
    Thread.sleep(1500) // Ensure the coroutine has time to complete
}

Using `SupervisorScope`:

The supervisorScope function is similar to coroutineScope, but it treats failures in child coroutines differently. In a supervisorScope, the failure of one child does not cancel the other children.

import kotlinx.coroutines.*

fun main() = runBlocking {
    supervisorScope {
        launch {
            delay(500)
            println("Task from supervisor scope")
        }
        launch {
            delay(1000)
            throw RuntimeException("Failure in child coroutine")
        }
    }
    println("Supervisor scope completed")
}

Using `viewModelScope`:

`viewModelScope` is part of the Android architecture components. It is designed to handle the lifecycle of coroutines in a ViewModel, automatically cancelling them when the ViewModel is cleared.

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.*

class MyViewModel : ViewModel() {
    fun fetchData() {
        viewModelScope.launch {
            // Perform a network request
            delay(1000)
            println("Data fetched")
        }
    }
}

Job and Its Use Cases

A `Job` represents a cancellable unit of work within a coroutine. It allows you to manage the lifecycle of a coroutine, including its creation, completion, and cancellation.

import kotlinx.coroutines.*

fun main() = runBlocking {
    // Launch a coroutine and get its Job
    val job = launch {
        println("Coroutine started")
        delay(1000) // Simulate a task
        println("Coroutine completed")
    }

    println("Main thread continues to execute")
    // Join the job to wait for coroutine completion
    job.join()
    println("Main thread waits for the coroutine to finish")
}

Use Cases Explained

  • Job Creation and Lifecycle: Launch a coroutine and store its Job to manage its lifecycle.
  • Checking Job Status: Use job.isActive to check if the job is running, complete or cancelled.
  • Job Cancellation: Cancel the job after 500 ms using job.cancelAndJoin() or job.cancel() and job.join(), which cancels and waits for completion.
  • Handling Cancellation Exceptions: Handle cancellation in a try-catch block for cleanup or additional actions.
  • Final Block: Ensure code runs in the ‘finally’ block for necessary cleanup, regardless of completion or cancellation.

Suspend Functions

Suspend functions can be paused and resumed. They enable coroutines to perform asynchronous operations without blocking the thread. Suspend functions can only be used within coroutines or other suspend functions. For example, you might use a suspend function to perform a network request or read data from a file.

import kotlinx.coroutines.*

suspend fun fetchData(): String {
    delay(1000) // Simulate network request
    return "Data from network"
}

fun main() = runBlocking {
    val data = fetchData()
    println(data)
}

Dispatchers

Dispatchers control which thread a coroutine runs on. Common dispatchers include:

  • `Dispatchers.Default`: For CPU-intensive tasks.
  • `Dispatchers.IO`: For I/O-intensive tasks.
  • `Dispatchers.Main`: For interacting with the UI.
  • `Dispatchers.Unconfined`: Starts the coroutine in the caller thread but only until the first suspension point. After that, it resumes in whatever thread is used by the ‘suspending’ function.
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Default) {
        // Perform CPU-intensive task
    }
    launch(Dispatchers.IO) {
        // Perform I/O-intensive task
    }
    launch(Dispatchers.Main) {
        // Update UI
    }
    launch(Dispatchers.Unconfined) {
        // Resume in the caller thread until the first suspension point
    }
}

By leveraging dispatchers, you can ensure that coroutines execute in the appropriate context, optimizing performance and responsiveness in your application.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *