Created
October 21, 2022 02:28
-
-
Save alokomkar/a1491e92c5c24bd0bbe4e30fdbacf683 to your computer and use it in GitHub Desktop.
Coroutine scope builder
This file contains hidden or 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
| // Sequentially executes doWorld followed by "Done" | |
| fun main() = runBlocking { | |
| doWorld() | |
| println("Done") | |
| } | |
| // Concurrently executes both sections | |
| suspend fun doWorld() = coroutineScope { // this: CoroutineScope | |
| launch { // Coroutine 1 | |
| delay(2000L) | |
| println("World 2") | |
| } | |
| launch { // Coroutine 2 | |
| delay(1000L) | |
| println("World 1") | |
| } | |
| println("Hello") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment