Kotlin coroutines let to cancel a coroutine:
suspend fun forgettingTheBirthDayRoutine() {
coroutineScope {
val workingJob = launch {
workingConsciousness()
}
launch {
delay(2000L)
workingJob.cancel()
workingJob.join()
logger.info("I forgot the birthday! Let's go to the mall!")
}
}
}
The first time the workingConsciousness
reaches a suspending point after the invocation of workingJob.cancel()
, it will be canceled, as will its children’s coroutines.
Is it possible to achieve the same behavior using structured concurrency in Java (StructuredTaskScope
and its subclasses)?