0

I'm familiar with both what an interrupt is used for (to put it roughly: asking the interrupted thread kindly to terminate or at least stop its work as soon as conveniently possible, instead of killing it immediately) as well as how to handle it properly (in most common cases, maybe not the tricky ones).

But I'm having a hard time to understand who (if not my own code) could even call Thread.interrupt() in the first place, and when this "third party interrupt" could occur.

I'm finding lots of information on why anybody would want to interrupt a thread, but hardly anything about who would do that for "my" threads unless I coded it myself.

So on Android, if my own app code does not contain any calls to Thread.interrupt() or something similar like AsyncTask<,,>.cancel(), will any thread I start ever be interrupted at all?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
LWChris
  • 3,320
  • 1
  • 22
  • 39
  • @Tim Castlijns From the answer there: "[...] in theory, any 3rd-party thread pool or thread management framework could legitimately do something like this." Android **is** a thread management framework. So - can/does Android interrupt my threads, and if yes, when? – LWChris Sep 17 '18 at 10:21
  • Yes it can. "One example is if your Runnable objects are executed using an ExecutorService and shutdownNow() is called on the service" – Tim Sep 17 '18 at 10:25
  • @TimCastelijns I am using `new Thread(myRunnable).start();`, no obvious `ExecutorService` (which I imagine would be a thread pool for example) involved. But I know Android is a lot different from the usual JVM regarding threading, so does my runnable eventually land on anything that qualifies as such an `ExecutorService`? – LWChris Sep 17 '18 at 10:38
  • not sure. Let me reopen so someone can answer for you – Tim Sep 17 '18 at 10:44
  • Is this relevant? https://stackoverflow.com/questions/3590000/what-does-java-lang-thread-interrupt-do – mevets Sep 17 '18 at 21:57
  • @mevets No. I am aware of what calling `Thread.interrupt()` does/means and **why** someone could do it. But I want to know real scenarios where that method is called on Android (if not by me). Most answers say "third party frameworks could". Android is a giant third-party framework, and the code of the `Thread` class doesn't really help unfortunately. – LWChris Sep 18 '18 at 11:16

0 Answers0