Last active
December 12, 2022 02:53
-
-
Save dandvl/997244229e1951bd2f80335bfa0a17d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() = runBlocking(Dispatchers.Default) { | |
val j = GlobalScope.launch { | |
val m = measureTimeMillis { | |
val answer1 = async { networkCall() } | |
val answer2 = async { networkCall2() } | |
println(answer1.await()) | |
println(answer2.await()) | |
} | |
println("m: $m") | |
} | |
j.join() | |
} | |
suspend fun networkCall() : String { | |
delay(3000L) | |
return "Answer 01" | |
} | |
suspend fun networkCall2() : String { | |
delay(3000L) | |
return "Answer 02" | |
} | |
// prints | |
// Answer 01 | |
// Answer 02 | |
// m: 3018 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
testImplementation(kotlin("test")) | |
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.* | |
import kotlin.system.measureTimeMillis | |
fun main() = runBlocking(Dispatchers.Default) { | |
val j = GlobalScope.launch { | |
val m = measureTimeMillis { | |
var ans1 = "" | |
var ans2 = "" | |
val answer1 = launch { ans1 = networkCall() } | |
val answer2 = launch { ans2 = networkCall2() } | |
answer1.join() | |
answer2.join() | |
println(ans1) | |
println(ans2) | |
} | |
println("m: $m") | |
} | |
j.join() | |
} | |
suspend fun networkCall() : String { | |
delay(3000L) | |
return "Answer 01" | |
} | |
suspend fun networkCall2() : String { | |
delay(3000L) | |
return "Answer 02" | |
} | |
// prints | |
// Answer 01 | |
// Answer 02 | |
// m: 3019 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment