0

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).

t0m
  • 3,004
  • 31
  • 53
  • Do not do a single OS call that takes two minutes. Divide it into smaller chunks (e.g., 120 calls that each take one second), and call `yield()` after each chunk. – CommonsWare Jun 14 '22 at 17:32
  • I cannot. Operation is `Os.ftruncate`. It's Android API method. – t0m Jun 14 '22 at 17:35

0 Answers0