Skip to content

Instantly share code, notes, and snippets.

@MasoudFallahpour
Created June 14, 2022 11:37
Show Gist options
  • Save MasoudFallahpour/c93bdaf1e59ea95c3d7bf169052c819e to your computer and use it in GitHub Desktop.
Save MasoudFallahpour/c93bdaf1e59ea95c3d7bf169052c819e 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) {
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