interrupting a thread after fixed time, does it have to throw InterruptedException?

Go To StackoverFlow.com

5

I want to interrupt a thread after a fixed amount of time. Someone else asked the same question, and the top-voted answer (https://stackoverflow.com/a/2275596/1310503) gave the solution below, which I have slightly shortened.

import java.util.Arrays;
import java.util.concurrent.*;

public class Test {
    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.invokeAll(Arrays.asList(new Task()), 2, TimeUnit.SECONDS);
        executor.shutdown();
    }
}

class Task implements Callable<String> {
    public String call() throws Exception {
        try {
            System.out.println("Started..");
            Thread.sleep(4000); // Just to demo a long running task of 4 seconds.
            System.out.println("Finished!");
        } catch (InterruptedException e) {
            System.out.println("Terminated!");
        }
        return null;
    }
}

They added:

the sleep() is not required. It is just used for SSCCE/demonstration purposes. Just do your long running task right there in place of sleep().

But if you replace Thread.sleep(4000); with for (int i = 0; i < 5E8; i++) {} then it doesn't compile, because the empty loop doesn't throw an InterruptedException. And for the thread to be interruptible, it needs to throw an InterruptedException.

Is there any way of making the above code work with a general long-running task instead of sleep()?

2012-04-05 15:34
by user1310503


2

If you want you action to be interruptable (i.e. it should be possible to interrupt it before it's completed) you need to either use other interruptable action (Thread.sleep, InputStream.read, read for more info) or manually check thread interruption status in your cycle condition using Thread.isInterrupted.

2012-04-05 15:40
by Andrei LED
I think you are right. I wanted to be able to interrupt the action/task at any point, and without writing special code inside the action/task to check whether it is interrupted, but I guess this is probably impossible - user1310503 2012-04-10 15:31


2

You've misunderstood.

"...for the thread to be interruptible, it needs to throw an InterruptedException" is simply not true. That catch block is there only because the Thread.sleep() method throws InterruptedException. If you're not using sleep (or any other code which can throw InterruptedException), then you don't need the catch block.

2012-04-05 15:38
by dty
If you do that, the program doesn't work - the thread never gets interrupted. I think what I wrote is true - user1310503 2012-04-10 15:24
I know what I wrote is true :-) I suspect the reason you can't interrupt the thread is because you've put the CPU into a busy spin, and it needs to make some kind of system call before the InterruptedException is thrown - dty 2012-04-10 18:29
I don't understand what you mean by "before the InterruptedException is thrown". We are talking about some code that doesn't throw an InterruptedException, aren't we? If what you wrote is true, I would be very grateful if you could give some code that (a) can be used as the body of Task.call(), (b) does not involve an InterruptedException, and (c) is correctly interrupted by Test.main() - user1310503 2012-04-11 10:55
It will, necessarily, have to involve InterruptedException. But it doesn't have to re-throw it or declare it as thrown or anything like that. If you were, for example, to write some output to a file inside your loop instead of simply doing a busy-spin, then you'll probably find you can interrupt it much better. When you interrupt a thread it doesn't take effect until you make or return from a system call, or you are blocked inside certain system calls (read, sleep, etc.) You can't interrupt a thread that is purely consuming CPU as your busy-spin loop is doing - dty 2012-04-11 13:01


2

You could check the interrupted status of the thread, for example:

public static void main(String[] args) throws Exception {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.invokeAll(Arrays.asList(new Task()), 2, TimeUnit.SECONDS);
    executor.shutdown();
}

static class Task implements Callable<String> {

    public String call() throws Exception {
        System.out.println("Started..");
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            if (Thread.currentThread().isInterrupted()) {
                System.out.println("Interrupted!");
                return null;
            }
        }
        System.out.println("Finished!");
        return null;
    }
}
2012-04-05 15:42
by assylias
But what I want to do is interrupt a general task at any time whatever it is doing. I don't want to have to insert special code in the Task to check whether it is interrupted, and I don't want it only to be interruptible at one point in each iteration - for example, one particular iteration might take a long time - user1310503 2012-04-10 15:29
This is not how Java deals with interruption - interruptions are a collaboratie process, whereby the interrupting code code suggests that a thread should interrupt, but each interrupted thread can implement that request as it wishes, including by doing nothing and continuing normal execution (obviously not recommended). There is a Thread.stop() method (see here) but it is deprecated and you should not use it - assylias 2012-04-10 15:35
In other words, in Java, if you want a piece of code to be interruptible, you need to code it to be aware of interruptions and handle them - assylias 2012-04-10 15:37
Thank you, that all makes sens - user1310503 2012-04-11 10:57
If one of the answers helped you solve your issue, you should accept it to mark the issue as closed - assylias 2012-04-11 11:00


-1

None of the code will throw InterruptedException if you replace the sleep. You should remove the try-catch for InterruptedException:

public String call() {
    System.out.println("Started..");
    for (int i = 0; i < 5E8; i++) {}
    System.out.println("Finished!");
    return null;
} 
2012-04-05 15:48
by evanwong
If you do that, the program doesn't work - the thread never gets stopped - user1310503 2012-04-10 15:24
Ads