I'm trying to get timeout functionality with a blo...
# coroutines
w
I'm trying to get timeout functionality with a blocking function I don't control. Conceptually, I want something that launches the blocking function on a thread, launches a timer on another thread; if the timer expires, it cancels the first thread and returns. How do I get this? The following illustrates this,
After
is always printed even though it shouldn't be.
Copy code
fun main(args: Array<String>) = runBlocking<Unit> {
    launch(CommonPool) {
        while (true) {
            withTimeoutOrNull(500) {
                println("Before")
                blockingFn()
                println("After")
            }
        }
    }
    delay(10000)
}

fun blockingFn() {
    Thread.sleep(2000)
}