abarnybox
abarnybox

Reputation: 139

How exactly do I interrupt a thread?

I'm trying to figure out exactly how to interrupt a thread. I know this has been asked before but I'm not sure I've understood correctly.

I currently have:

public class Test implements Runnable {
    public void run() {
        int i = 0;
        while(!Thread.currentThread().isInterrupted() && i < 100) {
            System.out.println(i++);
        }
    }
    public static void main(String args[]) {
        Thread x = new Thread(new Test());
        x.start();
        for (int i = 0; i < 100; i++) {
            System.out.println(i+100);
            if (i == 75) {
                x.interrupt();
            }
        }
    }
}

Which has both threads count to 100. It's a simple example, I know but it occurs to me that if I was doing something more complicated in the run method then a while loop on the outside might not be good enough. So what I'm asking is:

If I have for example 50 lines of code in the run method and I want to interrupt the thread wherever in those 50 lines of code it is then would I have to have loads of ifs to check it hasn't been interrupted between each line? As far as my understanding of the whole thing at the moment goes, that would be the case...which makes me think I've missed something.

Thanks for the help :)

Upvotes: 1

Views: 129

Answers (2)

Solomon Slow
Solomon Slow

Reputation: 27190

Adding to what Ian Roberts already said,

If you are not writing the top-level application (i.e., if the code that you are writing is going to be called by anybody else), then there is only one reasonable way to handle an InterruptedException;

  • Safely abort whatever operation your caller asked you to do,
  • Make sure to leave things in a state where your caller can start a new operation if that's what your caller wants to do, and
  • re-throw InterruptedException.

Code that behaves any other way is likely to surprise whoever calls it, and surprising the caller is never a good idea.

If you are writing the top-level application, then go ahead and handle InterruptedException any way that suits you. (Just hope that the libraries that you call won't do anything that surprises you.)

Upvotes: 0

Ian Roberts
Ian Roberts

Reputation: 122414

Your understanding is correct - interrupt is intended for co-operative rather than pre-emptive interruption. It is up to the target thread to decide when it is safe to be interrupted, and to make the appropriate checks at that time. Certain blocking operations will throw InterruptedException if the thread is interrupted while blocked, but other than that it's up to you to check Thread.interrupted() as appropriate. In most algorithms you don't want to allow interruption at arbitrary points, but only at well defined places where you know the state will be consistent.

Note that you should make the checks using Thread.interrupted() rather than Thread.currentThread().isInterrupted(), as the former resets the interruption flag after checking it. Once isInterrupted returns true it will continue to return true on every call until the flag is reset, which is probably not what you want.

Upvotes: 2

Related Questions