java - Is this example code just facile or would there be a reason here to use interrupt() -


i'm reading goetz's java concurrency in practice example code shown:

public final class indexer implements runnable {      private final blockingqueue<file> queue;      public indexer(blockingqueue<file> queue) {         this.queue = queue;     }      @override     public void run() {         try {             while (true) {                 queue.take();             }         } catch (interruptedexception e) {             thread.currentthread().interrupt();         }     }  } 

with description:

restore interrupt. cannot throw interruptedexception, instance when code part of runnable . in these situations, must catch interruptedexception , restore interrupted status calling interrupt on current thread, code higher call stack can see interrupt issued, demonstrated in listing 5.10 .

in example code, "code higher call stack" never see interrupt if code executed - or making wrong deduction? thread here dies after calling interrupt(), correct?

so way interrupt() useful if in within loop, correct?

the thread here dies after calling interrupt(), correct?

the executing thread not finish after interrupt, you're thinking thread#stop. thread thread may continue run after runnable completes. runnable task thread runs.

after task completes, important thread know interruption occurred? if thread needs respond other cancellation request , being done thread?

because runnable task don't have answers , such should let thread know interruption did occur thread can handle way wants to.


Comments