ryyst
ryyst

Reputation: 9791

Java - interrupting threads?

I got a question about interrupting threads in Java. Say I have a Runnable:

public MyRunnable implements Runnable {
    public void run() {
        operationOne();
        operationTwo();
        operationThree();
    }
}

I want to implement something like this:

Thread t = new Thread(new MyRunnable());
t.run();

... // something happens
    // we now want to stop Thread t

t.interrupt(); // MyRunnable receives an InterruptedException, right?

... // t is has now been terminated.

How can I implement this in Java? Specifically, how do I catch the InterruptedException in MyRunnable?

Upvotes: 3

Views: 1782

Answers (5)

GETah
GETah

Reputation: 21419

I think the answers above will pretty much fit your problem. I just want to add something on InterruptedException

Javadoc says:

InterruptedException :Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.

This means InterruptedException won't be thrown while running

operationOne();
operationTwo();
operationThree();

unless you are either sleeping, waiting for a lock or paused somewhere in these three methods.

EDIT If the provided code can not be changed as suggested by the nice and useful answers around here then I am afraid you have no way of interrupting your thread. As apposed to other languages such as C# where a thread can be aborted by calling Thread.Abort() Java does not have that possibility. See this link to know more about the exact reasons.

Upvotes: 0

Amir Raminfar
Amir Raminfar

Reputation: 34159

I recommend testing for Thread.isInterrupted(). Javadoc here. The idea here is that you are doing some work, most likely in a loop. On every iteration you should check if the interrupted flag is true and stop the work.

while(doingWork && !Thread.isInterrupted() {
  // do the work
}

Edit: To be clear, your thread won't receive an InterruptedException if the sub tasks are not blocking or worst, eat that exception. Checking for the flag is the right method but not everybody follows it.

Upvotes: 3

user949300
user949300

Reputation: 15729

First, the 2nd line of your 2nd block of code should be t.start(), not t.run(). t.run() simply calls your run method in-line.

And yes, MyRunnable.run() must check periodically, while it is running, for Thread.currentThread().isInterrupted(). Since many things you might want to do in a Runnable involve InterruptedExceptions, my advice is to bite the bullet and live with them. Periodically call a utility function

public static void checkForInterrupt() throws InterruptedException {
   if (Thread.currentThread().isInterrupted())
      throw new InterruptedException();
}

EDIT added

Since I see a comment that the poster has no control over the individual operations, his MyRunnable.run() code should look like

public void run() {
  operation1();
  checkForInterrupt();
  operation2();
  checkForInterrupt();
  operation3();
}

Upvotes: 1

Eagle
Eagle

Reputation: 163

an InterruptedThreadException is only thrown when the thread is being blocked (wait, sleep, etc.) . Otherwise, you'll have to check Thread.currentThread().isInterrupted().

Upvotes: 0

Matt
Matt

Reputation: 95

First of all, should be class in there

public class MyRunnable extends Thread {
    public void run() {
        if(!isInterrupted()){
            operationOne();
            operationTwo();
            operationThree();
        }
    }
}

Would this work better?

Upvotes: -1

Related Questions