How to stop long running disk operation in Thread or Coroutine in Android?

37 views Asked by At

Let's say we have this pseudo code in Android:

fun writeData(fd: File, data: Data) {
    timeout (1000) { //max running time is limited to 1000ms
        val operation = create thread/coroutine { // Thread or Coroutine
            Os.write(fd, data) // <--- let say it's blocking operation for 2 minutes
        }.start()
    }
}

How can I immediately stop Thread or Coroutine? When I call operation.interrupt() (operation.cancel() in the case of coroutine) thread or coroutine is waiting to be completed and running 2 minutes (all the time until Os.write(fd, data) is finished). I need to abort operation immediately, because it is a long disk operation (don't want to wait 2 minutes).

0

There are 0 answers