How to timeout a thread How to timeout a thread multithreading multithreading

How to timeout a thread


Indeed rather use ExecutorService instead of Timer, here's an SSCCE:

package com.stackoverflow.q2275443;import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;public class Test {    public static void main(String[] args) throws Exception {        ExecutorService executor = Executors.newSingleThreadExecutor();        Future<String> future = executor.submit(new Task());        try {            System.out.println("Started..");            System.out.println(future.get(3, TimeUnit.SECONDS));            System.out.println("Finished!");        } catch (TimeoutException e) {            future.cancel(true);            System.out.println("Terminated!");        }        executor.shutdownNow();    }}class Task implements Callable<String> {    @Override    public String call() throws Exception {        Thread.sleep(4000); // Just to demo a long running task of 4 seconds.        return "Ready!";    }}

Play a bit with the timeout argument in Future#get() method, e.g. increase it to 5 and you'll see that the thread finishes. You can intercept the timeout in the catch (TimeoutException e) block.

Update: to clarify a conceptual misunderstanding, 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(). Inside your long running task, you should be checking if the thread is not interrupted as follows:

while (!Thread.interrupted()) {    // Do your long running task here.}


There isn't a 100% reliable way to do this for any old task. The task has to be written with this ability in mind.

Core Java libraries like ExecutorService cancel asynchronous tasks with interrupt() calls on the worker thread. So, for example, if the task contains some sort of loop, you should be checking its interrupt status on each iteration. If the task is doing I/O operations, they should be interruptible too—and setting that up can be tricky. In any case, keep in mind that code has to actively check for interrupts; setting an interrupt doesn't necessarily do anything.

Of course, if your task is some simple loop, you can just check the current time at each iteration and give up when a specified timeout has elapsed. A worker thread isn't needed in that case.


Consider using an instance of ExecutorService. Both invokeAll() and invokeAny() methods are available with a timeout parameter.

The current thread will block until the method completes (not sure if this is desirable) either because the task(s) completed normally or the timeout was reached. You can inspect the returned Future(s) to determine what happened.