Master Canceling: 5 Proven Ways to Abort Coroutines Fast

At some point, you’ll face a situation where you need to halt a coroutine that’s already underway. Let’s explore the process of doing so.

Terminating a Job

A job has several functions at its disposal, including cancel, which appears to be the ideal s…


This content originally appeared on DEV Community and was authored by Emily Johnson

At some point, you'll face a situation where you need to halt a coroutine that's already underway. Let's explore the process of doing so.

Terminating a Job

A job has several functions at its disposal, including cancel, which appears to be the ideal solution. Moreover, we can utilize join to wait until the job has completed its termination.

The example below demonstrates a job being terminated:

runBlocking {
  val job = launch(Dispatchers.Default) {
    for (i in 0..1000) {
      delay(50)
      println("$i..")
    }
    println("Job is completed")
  }
  delay(500)
  println("Terminating")
  job.cancel()
  job.join()
  println("Terminated and done")
}

The output will be:

0..
1..
2..
3..
4..
5..
6..
7..
8..
Terminating
Terminated and done

For more information on canceling Kotlin coroutines like a pro, check out this article: 5 Essential Techniques.


This content originally appeared on DEV Community and was authored by Emily Johnson


Print Share Comment Cite Upload Translate Updates
APA

Emily Johnson | Sciencx (2024-09-19T22:27:19+00:00) Master Canceling: 5 Proven Ways to Abort Coroutines Fast. Retrieved from https://www.scien.cx/2024/09/19/master-canceling-5-proven-ways-to-abort-coroutines-fast/

MLA
" » Master Canceling: 5 Proven Ways to Abort Coroutines Fast." Emily Johnson | Sciencx - Thursday September 19, 2024, https://www.scien.cx/2024/09/19/master-canceling-5-proven-ways-to-abort-coroutines-fast/
HARVARD
Emily Johnson | Sciencx Thursday September 19, 2024 » Master Canceling: 5 Proven Ways to Abort Coroutines Fast., viewed ,<https://www.scien.cx/2024/09/19/master-canceling-5-proven-ways-to-abort-coroutines-fast/>
VANCOUVER
Emily Johnson | Sciencx - » Master Canceling: 5 Proven Ways to Abort Coroutines Fast. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/19/master-canceling-5-proven-ways-to-abort-coroutines-fast/
CHICAGO
" » Master Canceling: 5 Proven Ways to Abort Coroutines Fast." Emily Johnson | Sciencx - Accessed . https://www.scien.cx/2024/09/19/master-canceling-5-proven-ways-to-abort-coroutines-fast/
IEEE
" » Master Canceling: 5 Proven Ways to Abort Coroutines Fast." Emily Johnson | Sciencx [Online]. Available: https://www.scien.cx/2024/09/19/master-canceling-5-proven-ways-to-abort-coroutines-fast/. [Accessed: ]
rf:citation
» Master Canceling: 5 Proven Ways to Abort Coroutines Fast | Emily Johnson | Sciencx | https://www.scien.cx/2024/09/19/master-canceling-5-proven-ways-to-abort-coroutines-fast/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.