Skip to content

Instantly share code, notes, and snippets.

@manoj-mili
Created December 14, 2021 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-mili/c6fc38e8c5045c0f05e2b4e6474f8f8d to your computer and use it in GitHub Desktop.
Save manoj-mili/c6fc38e8c5045c0f05e2b4e6474f8f8d to your computer and use it in GitHub Desktop.
Cancel Single job in Coroutine
coroutineScope.launch {
val job = coroutineScope.launch {
var counter = 0
while (true) {
counter++
println("Current Counter is $counter")
}
}
delay(1000)
println("Cancelling Job")
job.cancel()
}
@manoj-mili
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment