ThreadPoolExecutor timeout configuration

4.5k views Asked by At

I want to setup a timeout for my ThreadPoolExecutor.

I know that i can use

future.get(THREAD_TIMEOUT_MS, TimeUnit.MILLISECONDS);

But this code is blocking.

What i want to achieve is that i can create several Runables which are processed with a pool of 4 threads for example.

If the processing of the thread takes more than e.g. 5 seconds i want to throw a timeout exception.

This is my current setup:

public class ServerExecutorService {

    public static int QUEUE_KEEP_ALIVE_TIME_SECONDS = 5;
    public static int MAXIMUM_POOL_SIZE = 12;
    public static int CORE_POOL_SIZE = 12;
    public static Logger LOG = LoggerFactory.getLogger(ServerExecutorService .class);
    public static int THREAD_TIMEOUT_MS = 5000;
    private LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(2);
    private RejectedHandler rejectedHandler = new RejectedHandler();


    private ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, QUEUE_KEEP_ALIVE_TIME_SECONDS, TimeUnit.SECONDS, linkedBlockingQueue);

    public ServerExecutorService () {
        executor.setRejectedExecutionHandler(this.rejectedHandler);
    }

    public void setRejectedHandler(RejectedHandler rejectedHandler) {
        executor.setRejectedExecutionHandler(rejectedHandler);
    }


    public void execute(Runnable runnable){
//        executor.execute(runnable);
//        Future<?> future = executor.submit(runnable);
        Future<?> future = executor.submit(runnable);

        try {
            future.get(THREAD_TIMEOUT_MS, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            System.out.println("Thread processing timeout.");
            LOG.warn("Thread processing timeout.", e);
        } catch (Exception e) {
            System.out.println("Thread processing error within ServerExecutorService ");
            LOG.error("Thread processing error within ServerExecutorService ", e);
        }
    }

}

But like you can see the future.get(THREAD_TIMEOUT_MS, TimeUnit.MILLISECONDS); will wait until the Thread has finished. So the next Threads are not started.

Test:

@Test
    public void testThreadPoolExhausted() {
        serverExecutorService.setRejectedHandler(rejectedHandler);
        for (int i = 0; i < 4; i++) {
            final int finalI = i;
            serverExecutorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("do something" + finalI);
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

In this test the second thread is started after 3 seconds and not immediatly.

1

There are 1 answers

0
roby On

You could create two threadpools, one for timeouts and one for the actual work.

ExecutorService timeoutService = Executors.newCachedThreadPool();
ExecutorService workerService = Executors.newCachedThreadPool();

public Future<?> submit(final Runnable runnable){
    return timeoutService.submit(() -> {
        try {
            Future<?> future = workerService.submit(runnable);
            future.get(100, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
}