Created
December 14, 2021 04:43
-
-
Save manoj-mili/c6fc38e8c5045c0f05e2b4e6474f8f8d to your computer and use it in GitHub Desktop.
Cancel Single job in Coroutine
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
coroutineScope.launch { | |
val job = coroutineScope.launch { | |
var counter = 0 | |
while (true) { | |
counter++ | |
println("Current Counter is $counter") | |
} | |
} | |
delay(1000) | |
println("Cancelling Job") | |
job.cancel() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remember as this is not co-operative cancellation the job will still continue as it not aware of cancellation which was triggered i.e. there is no way for the block inside while loop to know if the job is still active or not