'Is there a good way to distinguish spurious wake up and Thread.interrupt()?

I want to make a thread which can be interrupted at any time while be guarded against spurious wake ups. The problem here is that both spurious wakes as well as interrupts work the same: they throw InterruptedException

void anyMethodCalledByThread() {
  // .. a lot of work before
  while (wakingUpCondition) {
     try {
       lock.wait()
     } catch (InterruptedException e) {
       // is it spurious wake up and I should just ignore it?
       // or is it actual interrupt and I should do:
       // Thread.interrupt();
       // return;
       // and check interruption status in methods above to abort all tasks?
     }
  }
  // .. a lot of work after
}

From that I see, there is no way to distinguish them with just jdk, even Condition is of no use. Only possible solution I see is to use some extra volatile boolean per thread, but this makes Thread.interrupt() essentially useless by itself.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source