Skip to content

Instantly share code, notes, and snippets.

@MasoudFallahpour
Last active June 21, 2022 14:50
Show Gist options
  • Save MasoudFallahpour/5d900c5c94e5444efb2062e0e19422af to your computer and use it in GitHub Desktop.
Save MasoudFallahpour/5d900c5c94e5444efb2062e0e19422af to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.*
fun main() = runBlocking {
val startTime = System.currentTimeMillis()
val job = launch(Dispatchers.Default) {
var nextPrintTime = startTime
var i = 0
while (i < 5 && isActive) { // <-- HERE!
if (System.currentTimeMillis() >= nextPrintTime) {
println("job: I'm sleeping ${i++} ...")
nextPrintTime += 500L
}
}
}
delay(1300L)
println("main: I'm tired of waiting!")
job.cancelAndJoin()
println("main: Now I can quit.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment