Added maximum queue size to avoid outofmemory

This commit is contained in:
Andrea Cavalli 2020-08-27 00:10:07 +02:00
parent 816e9db448
commit 894351e633

View File

@ -13,6 +13,8 @@ import org.jetbrains.annotations.Nullable;
public class BoundedExecutorService {
private static final int MAX_BLOCKING_QUEUE_SIZE = 50000;
private BoundedExecutorService() {
}
@ -34,7 +36,7 @@ public class BoundedExecutorService {
TimeUnit unit,
ThreadFactory threadFactory,
@Nullable BiConsumer<Boolean, Integer> queueSizeStatus) {
return createCustom(0, corePoolSize, maxPoolSize, keepAliveTime, unit, threadFactory, Duration.ofDays(100000), queueSizeStatus, new LinkedBlockingQueue<>());
return createCustom(0, corePoolSize, maxPoolSize, keepAliveTime, unit, threadFactory, Duration.ofDays(100000), queueSizeStatus, new LinkedBlockingQueue<>(MAX_BLOCKING_QUEUE_SIZE));
}
public static ExecutorService createUnbounded(
@ -67,7 +69,7 @@ public class BoundedExecutorService {
TimeUnit unit,
ThreadFactory threadFactory,
@Nullable BiConsumer<Boolean, Integer> queueSizeStatus) {
return createCustom(maxQueueSize, corePoolSize, maxPoolSize, keepAliveTime, unit, threadFactory, Duration.ofDays(100000), queueSizeStatus, new LinkedBlockingQueue<>());
return createCustom(maxQueueSize, corePoolSize, maxPoolSize, keepAliveTime, unit, threadFactory, Duration.ofDays(100000), queueSizeStatus, new LinkedBlockingQueue<>(MAX_BLOCKING_QUEUE_SIZE));
}
public static ExecutorService create(
@ -99,6 +101,7 @@ public class BoundedExecutorService {
queue,
threadFactory
);
threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return new BlockingOnFullQueueExecutorServiceDecorator(threadPoolExecutor,
maxQueueSize,
queueItemTtl,