Milan Hruban
10/12/2020, 4:17 PMwithContext call cancellable by timeout?
fun main() = runBlocking<Unit> {
withTimeout(200) {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
Thread.sleep(5000)
}
}
}Zach Klippenstein (he/him) [MOD]
10/12/2020, 4:19 PMsleep here represents an actual blocking call (like an InputStream read), you can use runInterrupting to interrupt the blocked thread when the coroutine is cancelled.Ian Lake
10/12/2020, 4:22 PMdelay(5000) is the coroutine equivalent to Thread.sleep(5000) that plays nicely with cancellation, etc.Milan Hruban
10/12/2020, 4:51 PMMarc Knaup
10/12/2020, 5:34 PMDispatchers.Default but thinking about it IO actually makes more sense 😄Milan Hruban
10/12/2020, 5:52 PMThread.sleep() call was there as a placeholder for any blocking call (database query in my particular case)gildor
10/12/2020, 11:56 PMspand
10/13/2020, 7:29 AMfun main() = runBlocking<Unit> {
withTimeout(200) {
GlobalScope.async(<http://Dispatchers.IO|Dispatchers.IO>) {
Thread.sleep(5000)
}.await()
}
}
In effect "leaking" the threads.. and just letting them fail or complete in the background.