1

Directly from this oracle tutorial:

Interrupts

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. This is the usage emphasized in this lesson.

"It's up to the programmer to decide.." Where is it possible to program that particular behaviour?Does "It's very common for the thread to terminate" means that the standard behaviour terminates the thread?

EDIT: For example we have the acquireUninterruptibly method from Sempaphore class which if receives an interrupt it just delays the waiting time of the amount of time that the interrupt caused to be interrupted. Probably in this case the interrupt handling consists of only a Thread.yield()? Thanks in advance.

3 Answers 3

2

Usually the method like sleep throws InterruptedException.

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;
    }
}

In this simple example, the code simply tests for the interrupt and exits the thread if one has been received. In more complex applications, it might make more sense to throw an InterruptedException:

if (Thread.interrupted()) {
    throw new InterruptedException();
}

Now Answering your questions:

  1. from the above example you can see that you can handle the InterruptedException by your own if it is not handled by the method
  2. As you can handle this for those methods, you can do other tasks rather than exiting. But it is definitely most common to exit thread after an interrupt occurs.

Source: javadoc

Sign up to request clarification or add additional context in comments.

2 Comments

Tnx for replying, check out my question update. Do you agree it's just a Thread.yield() call?
I think you can use yield when interrupt called
1

An interrupt occurs when there is a problem (even exists some other case) and the program 'll switch to INTERRUPT SERVICE ROUTINE. It may handle the problem there and continue execution or terminate if it can't be solved.

In java, even "Exception" (Interrupt) we normally write such routines (which may cause interrupt) and catch(Exception) where we catch it and handle it. if the problem can't be solved we TERMINATE it.

Comments

0

When you set the interrupt it sets a flag, it's more complicated than this but that is the basic behaviour. You program points to detect an interrupt by using an

if(Thread.currentThread.isInterrupted())

or use a function which does basically this. You shouldn't assume that something magical happens. At the end of the day everything is implemented using code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.