async
If you call 2 suspend functions without async one after another, then the first one will need to finish. Before the second one get to starts. With async they they can run in parallel.
Without async -> Sequential.
From Without Async
Go to text ā
package com.glassthought.sandbox
import gt.sandbox.util.output.Out
import kotlinx.coroutines.*
val out = Out.standard()
fun main() = runBlocking {
// If you call suspend functions without async, then the first one will
// need to finish. Before the second one starts.
val resultOne = fetchDataOne()
val resultTwo = fetchDataTwo()
out.println("Combined result: ${resultOne + resultTwo}")
}
// Simulated data fetch function
suspend fun fetchDataOne(): Int {
delay(1000) // Simulate a network or heavy computation delay
out.println("Fetched data one")
return 10
}
// Another simulated data fetch function
suspend fun fetchDataTwo(): Int {
delay(1500) // Simulate a network or heavy computation delay
out.println("Fetched data two")
return 20
}
Command to reproduce:
gt.sandbox.checkout.commit 62f135ba00ec4f7e3094 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew run"
Recorded output of command:
> Task :app:checkKotlinGradlePluginConfigurationErrors SKIPPED
> Task :app:processResources NO-SOURCE
> Task :app:compileKotlin
> Task :app:compileJava NO-SOURCE
> Task :app:classes UP-TO-DATE
> Task :app:run
[2024-11-13T04:25:30.419896Z][elapsed-since-start: 1058ms][tname:main/tid:1] Fetched data one
[2024-11-13T04:25:31.967076Z][elapsed-since-start: 2576ms][tname:main/tid:1] Fetched data two
[2024-11-13T04:25:31.970922Z][elapsed-since-start: 2579ms][tname:main/tid:1] Combined result: 30
BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed
With Async -> Parallel
From Eg1
Go to text ā
package com.glassthought.sandbox
import gt.sandbox.util.output.Out
import kotlinx.coroutines.*
val out = Out.standard()
fun main() = runBlocking {
// Start two async operations in parallel
val deferredOne = async { fetchDataOne() }
val deferredTwo = async { fetchDataTwo() }
// Wait for results
val resultOne = deferredOne.await()
val resultTwo = deferredTwo.await()
out.println("Combined result: ${resultOne + resultTwo}")
}
// Simulated data fetch function
suspend fun fetchDataOne(): Int {
delay(1000) // Simulate a network or heavy computation delay
out.println("Fetched data one")
return 10
}
// Another simulated data fetch function
suspend fun fetchDataTwo(): Int {
delay(1500) // Simulate a network or heavy computation delay
out.println("Fetched data two")
return 20
}
Command to reproduce:
gt.sandbox.checkout.commit ffbd344f68e987e4c229 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew run"
Recorded output of command:
> Task :app:checkKotlinGradlePluginConfigurationErrors SKIPPED
> Task :app:compileKotlin UP-TO-DATE
> Task :app:compileJava NO-SOURCE
> Task :app:processResources NO-SOURCE
> Task :app:classes UP-TO-DATE
> Task :app:run
[2024-11-13T04:21:47.368979Z][elapsed-since-start: 1061ms][tname:main/tid:1] Fetched data one
[2024-11-13T04:21:47.868301Z][elapsed-since-start: 1533ms][tname:main/tid:1] Fetched data two
[2024-11-13T04:21:47.871792Z][elapsed-since-start: 1536ms][tname:main/tid:1] Combined result: 30
BUILD SUCCESSFUL in 2s
2 actionable tasks: 1 executed, 1 up-to-date
Children