Created
June 14, 2022 11:37
-
-
Save MasoudFallahpour/c93bdaf1e59ea95c3d7bf169052c819e 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
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