Kotlin coroutines are a concurrency design pattern that you can use on Android to simplify code that executes asynchronously. Here is a schematic overview of their key components and workflow:
-
Coroutine Builders: They are used to start a new coroutine.
launch: Starts a new coroutine without blocking the current thread and returns a Job that can be used to cancel the coroutine.
async: Starts a new coroutine, returns a Deferred of the result of the coroutine. It is like launch, but with a result that can be awaited.
runBlocking: Blocks the current thread until the coroutine inside it completes. It's mainly used for bridging between blocking code and coroutines and should be used sparingly.
-
Coroutine Scope: Defines the scope in which coroutines are launched. It controls the lifecycle of coroutines through its Job.
GlobalScope: Coroutines launched in this scope live until they complete or the application is terminated.
- Custom scopes: You can define your own scope using
CoroutineScope and tie it to the lifecycle of an application component, like an Activity or ViewModel in Android.
-
Coroutine Context: Provides configurations for the coroutine, including the Job and the dispatcher.
Job: Represents the instance of the coroutine itself and can be used to cancel the coroutine.
Dispatcher: Determines what thread or threads the coroutine will run on (e.g., Dispatchers.Main, Dispatchers.IO, Dispatchers.Default, Dispatchers.Unconfined).
-
Suspending Functions: These are special functions that can be paused and resumed at a later time without blocking the thread on which they are executing.
suspend: A keyword that marks a function as suspending, meaning it can be paused and resumed.
-
Structured Concurrency: Kotlin coroutines promote structured concurrency, which means that coroutines are launched in a specific scope and are automatically canceled when the scope is destroyed.
The general workflow of using coroutines in Kotlin is:
- Define a coroutine scope to control the lifecycle of your coroutines.
- Use a coroutine builder to launch the coroutine in the desired scope.
- Inside the coroutine, call suspending functions to perform asynchronous operations without blocking the thread.
- Use coroutine context to manage the execution behavior of the coroutine, such as cancellation and threading.
- Optionally, handle the result of the coroutine, if it was started with
async, by calling await() on the returned Deferred.
Here is a simple example of using coroutines in Kotlin:
fun main() = runBlocking { // This builder blocks the main thread
launch { // Launch a new coroutine in the scope of runBlocking
delay(1000L) // Non-blocking delay for 1 second (default time unit is ms)
println("World!") // Print after delay
}
println("Hello,") // Main thread continues while the coroutine is delayed
}
In this example, runBlocking is used to block the main thread until the coroutine inside launch completes. delay is a suspending function that non-blockingly delays the coroutine for a given time. The output will be "Hello," followed by "World!" after a 1-second delay.