launch and async
In this page:
Using launch for Fire-and-Forget Work
launch { } starts a new coroutine intended for side effects, like printing or updating state, and returns a Job that represents that running task, but no computed value.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Using launch for Fire-and-Forget Work
import kotlinx.coroutines.*
fun main() = runBlocking {
val job = launch {
println("Doing background work")
}
job.join()
println("Background work complete")
}
Login to try C/C++/Java/PHP code in the editor
Using async for a Result
async { } starts a coroutine that computes and returns a value, wrapped in a Deferred<T>; calling .await() on it suspends until the result is ready and then returns it.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Using async for a Result
import kotlinx.coroutines.*
fun main() = runBlocking {
val deferred = async {
delay(50)
42
}
println("Result: ${deferred.await()}")
}
Login to try C/C++/Java/PHP code in the editor
Running Multiple async Tasks Concurrently
Starting several async calls before awaiting any of them lets all of those coroutines run concurrently, and the total time is closer to the longest single task rather than the sum of all of them.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Running Multiple async Tasks Concurrently
import kotlinx.coroutines.*
fun main() = runBlocking {
val first = async { delay(100); 10 }
val second = async { delay(100); 20 }
val total = first.await() + second.await()
println("Total: $total")
}
Login to try C/C++/Java/PHP code in the editor
Choosing Between launch and async
Use launch when you don't need a result back from the coroutine, and async when you do -- reaching for async and never calling .await() is a common source of wasted concurrency.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Choosing Between launch and async
import kotlinx.coroutines.*
fun main() = runBlocking {
launch { println("Just logging, no result needed") }
val value = async { 100 }.await()
println("Computed value: $value")
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting to call
.await()on aDeferredfromasync, meaning you never actually retrieve its computed result. - Using
asyncwhenlaunchwould do, adding unnecessary overhead when no return value is actually needed. - Starting several
asynccalls but calling.await()on each immediately instead of after starting all of them, losing the benefit of running them concurrently.
launch { }starts a coroutine that runs for its side effects and returns aJob, with no result value.async { }starts a coroutine that computes a result, returned as aDeferred<T>, retrieved later with.await().- Starting multiple
asynccalls before awaiting any of them lets them run concurrently rather than sequentially. - Both
launchandasyncare coroutine builders provided bykotlinx.coroutines.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: