Trts
Trts

Reputation: 1069

How can I make interrupt() work?

Could you have a look at the picture. Now the A step ago I executed this.interrupt(). You can see that this.isInterrupted() is false. I was scrutinyzing - "this" has not changed. It had the same id (12). So, why there is no result of the interrupt() method?

enter image description here

Added later. Code as text:

@Override
protected void letUsFinalise() {
    this.interrupt();
}

Upvotes: 0

Views: 54

Answers (2)

JB Nizet
JB Nizet

Reputation: 691625

The javadoc of interrupted() says:

Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. In other words, if this method were to be called twice in succession, the second call would return false

(emphasis mine)

BTW, interrupted() is a static method. It should be called on the Thread class, not in this. And given that it has a side-effect, you should create a watch in your debugger for it. Watch isInterruped() instead.

Upvotes: 2

Felipe Sulser
Felipe Sulser

Reputation: 1195

Please note that interrupt() may not stop the execution of the thread immediately. Debugging a multithreaded program may not be as simple as that as the execution may be completely different. Therefore I suggest you to do the following:

while(!Thread.foo.isInterrupted()){
   //wait or  do something
   }

Upvotes: 0

Related Questions