Last active
June 21, 2022 14:50
-
-
Save MasoudFallahpour/5d900c5c94e5444efb2062e0e19422af 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 && 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