0

In this code

public class NoncancelableTask {
    public Task getNextTask(BlockingQueue<Task> queue) {
        boolean interrupted = false;
        try {
            while (true) {
                try {
                    return queue.take();
                } catch (InterruptedException e) {
                    interrupted = true;
                    // fall through and retry
                }
            }
        } finally {
            if (interrupted)
                Thread.currentThread().interrupt();
        }
    }

    interface Task {
    }
}

I understand that setting the interrupted status lets the caller of this method retain the status of interruption to do something with it according to the methods policy.

However Goetz also says that this method is used for "activities that do not support cancellation"

So my question is whats even the point of doing that if its never even going to go into the finally block and back up the call stack to its caller?

Michael
  • 41,989
  • 11
  • 82
  • 128
katiex7
  • 863
  • 12
  • 23
  • What do you mean with “its never even going to go into the finally block”? – Holger Jan 04 '18 at 10:10
  • It seems that if an interruptException is thrown it will just be consumed and the while true loop will just run again and I don't see how this code will ever reach inside the finally block unless some other exceptions are thrown – katiex7 Jan 04 '18 at 15:42
  • 1
    There is a `return` statement, isn’t it? – Holger Jan 04 '18 at 15:43
  • 1
    I don't know how I missed that after staring at this code for so long .__. – katiex7 Jan 04 '18 at 16:13

0 Answers0