2

I am reading Interrupts from Oracle Docs. I am unable to figure out the following part. It states that

What if a thread goes a long time without invoking a method that throws InterruptedException? Then it must periodically invoke Thread.interrupted, which returns true if an interrupt has been received. For example:

for (int i = 0; i < inputs.length; i++) {
    heavyCrunch(inputs[i]);
    if (Thread.interrupted()) {
        // We've been interrupted: no more crunching.
        return;
    }
}

I am scratching my head to understand, what does it mean by What if a thread goes a long time without invoking a method that throws InterruptedException? Secondly, what is the usage of Thread.interrupted(), it is a way, that thread can send a interrupt to itself? Whats the practical usage of this scenario? Thanks.

Ravi
  • 30,829
  • 42
  • 119
  • 173
benz
  • 4,561
  • 7
  • 37
  • 68
  • This might helps you understand ... http://stackoverflow.com/questions/1555990/difference-between-abort-and-interrupt-in-threads-in-net – Shazhad Ilyas Aug 07 '13 at 09:55

6 Answers6

5

This is a technique to keep the thread available for interruption.

Thread.interrupted() : checks whether present thread (itself) was interrupted by some other thread and clears the interrupted status flag. So it asks itself whether I was interrupted by someone to exit from what I was doing while I was performing a BIG BIG task and not listening to someone.

Imagine what would have happened if that thing was not done.

Suppose one iteration of heavyCrunch() takes 1 min worth of time. So n iterations will take n minutes.

Now suppose after starting the program you decide that you want to exit the program and terminate the program gracefully. So you interrupt the thread that is doing the heavy crunch. BUT the thread is unknown of the fact that you have interrupted it as it is not checking for the interrupt status. So the program will not end until N Minutes have not completed and you will have to wait for long time.

So to gracefully terminate the thread, it should always keep checking the interrupt status to respond if someone else has requested interruption.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
  • thanks for explaining in a detailed manner. I have a followup question to it, i was reading the documentation, it says that if Thread.interrupted() is invoked, it clears the status. So does it mean, even if it is not interrupted, it will set the interrupt status to true? – benz Aug 07 '13 at 10:04
  • No if it was not interrupted it will not set the flag. Only in case when the flag is true, calling the function will cause the flag to be false. and not the inverse. – Narendra Pathai Aug 07 '13 at 10:05
  • Oh great. So isn't it better to call isInterrupted() rather then interrupted(), if it is just to check whether thread has been interrupted or not? – benz Aug 07 '13 at 10:06
  • 3
    `Thread.interrupted()` is just given for shorthanding this `Thread.currentThread().isInterrupted()`. Internally even `Thread.interrupted()` uses `currentThread().isInterrupted()`. – Narendra Pathai Aug 07 '13 at 10:12
  • thankyou so much for making me understand this. Thanks a ton. – benz Aug 07 '13 at 10:14
3

If you interrupt the thread running this code

for (int i = 0; i < inputs.length; i++) {
    heavyCrunch(inputs[i]);
}

it will only set interrupted status in the thread but it will not stop it

Thread.interrupted tests if interrupted status is set (and clears it) so by adding

if (Thread.interrupted()) {
   return;
}

to the loop you make the code interruptible

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
3

Besides the fact that the phrase

What if a thread goes a long time without invoking a method that throws InterruptedException?

is deeply unclear, i suppose they mean the following:

Usually, if you have a thread that does some work in a while(true) loop, that is, a thread that does not terminate for a long time, you will probably place ANY function that throws InterruptedException in that thread (i.e. Thread.sleep(), a socket read, or anything!). This way, when your thread will be noticed an Interruption, one of those functions will catch it and you will be able to QUIT what you are doing in the thread (the thread does not just magically terminate itself).

Here comes what the phrase wanted to say:

WHAT IF YOU DO NOT WANT/HAVE to use these functions? Then you should use Thread.interrupted() to check whether you should QUIT doing what the thread is doing in the same way you would do if you catched an InterruptedException.

I hope this was clearer than the doc...

Lake
  • 4,072
  • 26
  • 36
0

You can use interrupted() stop nicely any long running thread by intrrup thread somewhere and check the condition by Thread.interrupted() and based on this come out of run method.

Let say you one thread is blocked on some monitor and somewhere else that thread got interrupted which inturn will throw InterruptedExceptionand can come out of block state with interrupted status true.

I think interrupt is the best way to achieve stop long running task because an interrupt will unblock some blocking IO and synchronization requests. A bespoke solution cannot do this.

May help you.

Community
  • 1
  • 1
amicngh
  • 7,831
  • 3
  • 35
  • 54
0

interrupt() sets from outside a flag in the object that can then be queried periodically by Interrupted() in the run() method.

Sai Kishore
  • 326
  • 1
  • 7
  • 16
to4dy
  • 128
  • 1
  • 1
  • 10
0

I am scratching my head to understand, what does it mean by What if a thread goes a long time without invoking a method that throws InterruptedException?

A: If other thread call the interupt() method of this thread, and if this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException. Otherwise, just set the interrupt status.

Secondly, what is the usage of Thread.interrupted(), it is a way, that thread can send an interrupt to itself? What's the practical usage of this scenario?

A: Thread.interrupted() is use to detect current thread's interrupt status, return true if it's set and then clear the status. You check this in order to respond to other thread's interrupt call, such as throw an InterruptedException and exit the thread or just exit.

Sai Kishore
  • 326
  • 1
  • 7
  • 16
Harry.Chen
  • 1,582
  • 11
  • 12