https://kotlinlang.org logo
#announcements
Title
# announcements
x

Xitij

10/05/2020, 8:49 AM
Copy code
suspend fun jobLifecycle () = coroutineScope {
   val job =  launch{
       println("Working....")
       delay(2000L)
       println("Done !")
    }

    delay(1000L)
    job.cancel() 
    // delay(1L) i have to do it to print correct value of job.isCompleted below
   
    println("isCompleted: ${job.isCompleted}") // it is printing "false" but if i delay for some time after cancelling the job it prints  "true".
   
}
is it my computer or it really takes some time to move job from cancelling to cancelled state where isCompleted becomes true after cancellation of job or is there something im missing out pls review..
z

Zach Klippenstein (he/him) [MOD]

10/05/2020, 3:03 PM
What dispatcher is this running on? If it's running on a single thread, it probably can't finish cancelling the job until the outer coroutine yields. If multiple threads, it's less deterministic, but since the cancellation might end up doing some work on another thread, that will take a little time too. I believe that the job should immediately move into the cancelling state though in both cases (eg isActive returns false).
2 Views