Created
September 10, 2024 20:41
-
-
Save dulimarta/abb18c05821ffde64733d1e029a975ac to your computer and use it in GitHub Desktop.
CS357 Coroutine Example
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
package edu.gvsu.computing.cs357 | |
import kotlinx.coroutines.* | |
import kotlin.system.measureTimeMillis | |
fun sample1() { | |
println ("Begin Sample 1") | |
runBlocking { | |
launch { | |
println("Begin A") | |
delay(5000) | |
println("End A") | |
} | |
launch { | |
println("Begin B") | |
delay(3000) | |
println("End B") | |
} | |
println("Begin C") | |
delay(7500) | |
println("End C") | |
} | |
println ("End Sample 1") | |
} | |
fun sample2() { | |
println("Begin of sample 2") | |
runBlocking { | |
launch { | |
coroutineScope { | |
launch { | |
println("Begin Code 1") | |
delay(2000) | |
println("End Code 1") | |
} | |
launch { | |
println("Begin Code 2") | |
delay(3000) | |
println("End Code 2") | |
} | |
println("Begin Code 3") | |
delay(2000) | |
println("End Code 3") | |
} | |
println("Begin Code 4") | |
delay(1500) | |
println("End Code 4") | |
} | |
launch { | |
println("Begin Code 5") | |
delay(4000) | |
println("End Code 5") | |
} | |
} | |
println("End of Sample 2") | |
} | |
fun main() { | |
val elapse = measureTimeMillis { | |
sample2() | |
} | |
println("Elapse time: $elapse") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment