common-utils/src/main/java/org/warp/commonutils/concurrency/executor/BoundedExecutorService.java

113 lines
3.6 KiB
Java
Raw Normal View History

2020-06-12 18:36:36 +02:00
package org.warp.commonutils.concurrency.executor;
import java.time.Duration;
2020-08-24 23:48:05 +02:00
import java.util.concurrent.BlockingQueue;
2020-06-12 18:36:36 +02:00
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
2020-06-12 18:36:36 +02:00
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
2020-06-12 18:36:36 +02:00
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import org.jetbrains.annotations.Nullable;
2020-08-24 23:48:05 +02:00
public class BoundedExecutorService {
private static final int MAX_BLOCKING_QUEUE_SIZE = 50000;
2020-08-24 23:48:05 +02:00
private BoundedExecutorService() {
}
2020-06-12 18:36:36 +02:00
2020-07-17 15:33:09 +02:00
@Deprecated
2020-08-24 23:48:05 +02:00
public static ExecutorService createUnbounded(
2020-07-17 15:33:09 +02:00
int corePoolSize,
2020-08-16 22:52:46 +02:00
int maxPoolSize,
2020-07-17 15:33:09 +02:00
long keepAliveTime,
TimeUnit unit,
@Nullable BiConsumer<Boolean, Integer> queueSizeStatus) {
2020-08-16 22:52:46 +02:00
return create(0, corePoolSize, maxPoolSize, keepAliveTime, unit, Executors.defaultThreadFactory(), queueSizeStatus);
2020-07-17 15:33:09 +02:00
}
2020-08-24 23:48:05 +02:00
public static ExecutorService createUnbounded(
2020-07-17 15:33:09 +02:00
int corePoolSize,
2020-08-16 22:52:46 +02:00
int maxPoolSize,
2020-07-17 15:33:09 +02:00
long keepAliveTime,
TimeUnit unit,
ThreadFactory threadFactory,
@Nullable BiConsumer<Boolean, Integer> queueSizeStatus) {
return createCustom(0, corePoolSize, maxPoolSize, keepAliveTime, unit, threadFactory, Duration.ofDays(100000), queueSizeStatus, new LinkedBlockingQueue<>(MAX_BLOCKING_QUEUE_SIZE));
2020-08-24 23:48:05 +02:00
}
public static ExecutorService createUnbounded(
int corePoolSize,
int maxPoolSize,
long keepAliveTime,
TimeUnit unit,
ThreadFactory threadFactory,
@Nullable BiConsumer<Boolean, Integer> queueSizeStatus,
BlockingQueue<Runnable> queue) {
return createCustom(0, corePoolSize, maxPoolSize, keepAliveTime, unit, threadFactory, Duration.ofDays(100000), queueSizeStatus, queue);
2020-07-17 15:33:09 +02:00
}
2020-06-12 18:36:36 +02:00
@Deprecated
2020-08-24 23:48:05 +02:00
public static ExecutorService create(
2020-08-16 22:52:46 +02:00
int maxQueueSize,
2020-06-12 18:36:36 +02:00
int corePoolSize,
2020-08-16 22:52:46 +02:00
int maxPoolSize,
2020-06-12 18:36:36 +02:00
long keepAliveTime,
TimeUnit unit,
@Nullable BiConsumer<Boolean, Integer> queueSizeStatus) {
2020-08-16 22:52:46 +02:00
return create(maxQueueSize, corePoolSize, maxPoolSize, keepAliveTime, unit, Executors.defaultThreadFactory(), queueSizeStatus);
2020-06-12 18:36:36 +02:00
}
2020-08-24 23:48:05 +02:00
public static ExecutorService create(
2020-08-16 22:52:46 +02:00
int maxQueueSize,
2020-06-12 18:36:36 +02:00
int corePoolSize,
2020-08-16 22:52:46 +02:00
int maxPoolSize,
2020-06-12 18:36:36 +02:00
long keepAliveTime,
TimeUnit unit,
ThreadFactory threadFactory,
@Nullable BiConsumer<Boolean, Integer> queueSizeStatus) {
return createCustom(maxQueueSize, corePoolSize, maxPoolSize, keepAliveTime, unit, threadFactory, Duration.ofDays(100000), queueSizeStatus, new LinkedBlockingQueue<>(MAX_BLOCKING_QUEUE_SIZE));
2020-06-12 18:36:36 +02:00
}
2020-08-24 23:48:05 +02:00
public static ExecutorService create(
int maxQueueSize,
int corePoolSize,
int maxPoolSize,
long keepAliveTime,
TimeUnit unit,
ThreadFactory threadFactory,
@Nullable BiConsumer<Boolean, Integer> queueSizeStatus,
BlockingQueue<Runnable> queue) {
return createCustom(maxQueueSize, corePoolSize, maxPoolSize, keepAliveTime, unit, threadFactory, Duration.ofDays(100000), queueSizeStatus, queue);
}
public static ExecutorService createCustom(
2020-08-16 22:52:46 +02:00
int maxQueueSize,
int corePoolSize,
2020-08-16 22:52:46 +02:00
int maxPoolSize,
long keepAliveTime,
TimeUnit unit,
ThreadFactory threadFactory,
Duration queueItemTtl,
2020-08-24 23:48:05 +02:00
@Nullable BiConsumer<Boolean, Integer> queueSizeStatus,
BlockingQueue<Runnable> queue) {
2020-08-16 22:52:46 +02:00
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,
maxPoolSize,
keepAliveTime,
unit,
queue,
threadFactory
);
threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return new BlockingOnFullQueueExecutorServiceDecorator(threadPoolExecutor,
maxQueueSize,
queueItemTtl,
queue::size,
queueSizeStatus
);
}
2020-06-12 18:36:36 +02:00
}