2

In many sources I found that Thread.interrupted() method clears interrupt status of thread, but in none of them there was explanation of the reason why this method works exactly in this way.

I still feel confused a little because of lack of understanding of this reason and lack of understating of what problem designers of java tried to solve by clearing interrupt status.

I will appreciate very much if someone could explain that and show some example.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
Igor Dotsenko
  • 85
  • 1
  • 8
  • Because otherwise there is no way of clearing it. – user207421 Feb 27 '16 at 17:58
  • It looks as pretty weird mechanic to change interrupt status in case if it is the only reason of why this method do that :) – Igor Dotsenko Feb 27 '16 at 18:11
  • Not at all. It's a test and clear. Otherwise you would have to provide a setter method, or rather an unsetter method, which would allow a thread to lie about its own interrupt status. – user207421 Feb 27 '16 at 18:16

2 Answers2

3

The idea behind thread interruption is that one thread may signal another to request that it interrupt is regular processing to divert its attention to some thread-specific special action. What a thread actually does in response depends entirely on the code running in that thread.

There are two main ways in which a Thread can determine whether it has been interrupted:

  1. Several Thread and Object methods will throw an InterruptedException if invoked in a thread whose interrupted status is set, or if a thread is interrupted while the method is executing. The interrupted status is cleared in this event, presumably because the exception is considered adequate notice of the interruption.
  2. Code running in the thread can invoke Thread.interrupted() or Thread.currentThread().isInterrupted() to proactively test for an interrupt. The former also resets the interrupted status; the latter does not, likely because it is an instance method -- interrupts must not be lost in the event that one thread calls the isInterrupted() method of a different one.

The techniques that cause the interrupt status to be reset do so in order that the thread is able to handle subsequent interruptions. The key point here is perhaps that thread interruption is not intended to necessarily cause the interrupted thread to shut down (although that is indeed one response that a thread can make). It is a more general mechanism.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • So as I understood, the main reason is to make interruption handling more flexible and to provide an opportunity for thread to be continued if needed. – Igor Dotsenko Feb 27 '16 at 18:28
  • @IgorDotsenko, I'd say rather that the intended purpose is to make interruption handling *easier* and *more convenient*, in light of the fact that interruption is a mechanism that affords a thread receiving multiple, separate interruptions. The alternative would be to require threads to manually reset their interrupt status at need -- but in fact, that's what `interrupted()` already does. It happens *also* to return the previous value of the interrupt status. – John Bollinger Feb 27 '16 at 23:15
0

From jdoc

An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate

and

By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.

So Thread.interrupted clears the flag because it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.

Explanation https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html

Eugene Kirin
  • 568
  • 3
  • 16