ThreadPoolExecutor超时配置

时间:2015-06-18 09:26:33

标签: java threadpoolexecutor

我想为ThreadPoolExecutor设置超时。

我知道我可以使用

future.get(THREAD_TIMEOUT_MS, TimeUnit.MILLISECONDS);

但是这段代码是封锁的。

我想要实现的是我可以创建几个Runables,例如使用4个线程池进行处理。

如果线程的处理超过例如5秒我想抛出超时异常。

这是我目前的设置:

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);
        }
    }

}

但是就像你可以看到future.get(THREAD_TIMEOUT_MS,Tim​​eUnit.MILLISECONDS);将等到线程完成。所以下一个Threads没有启动。

测试:

@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();
                    }
                }
            });
        }
    }

在此测试中,第二个线程在3秒后启动,而不是立即启动。

1 个答案:

答案 0 :(得分:0)

你可以创建两个线程池,一个用于超时,一个用于实际工作。

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);
        }
    });
}