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()
?
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.
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.
InterruptedException
is thrown - dty 2012-04-10 18:29
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
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
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;
}
}
Thread.stop()
method (see here) but it is deprecated and you should not use it - assylias 2012-04-10 15:35
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;
}