clintropolis commented on code in PR #18176:
URL: https://github.com/apache/druid/pull/18176#discussion_r2260939205
##########
processing/src/main/java/org/apache/druid/java/util/common/concurrent/Execs.java:
##########
@@ -173,6 +175,97 @@ public void rejectedExecution(Runnable r,
ThreadPoolExecutor executor)
);
}
+ public static ExecutorService newBlockingCached(
+ final String nameFormat,
+ int minThreads,
+ int maxThreads,
+ long keepAliveTime,
+ TimeUnit keepAliveTimeUnit,
+ final Integer priority
+ )
+ {
+ final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
+ if (minThreads == maxThreads) {
+ return new ThreadPoolExecutor(
+ minThreads,
+ maxThreads,
+ keepAliveTime,
+ keepAliveTimeUnit,
+ queue,
+ makeThreadFactory(nameFormat, priority),
+ (r, executor) -> {
+ if (executor.isShutdown()) {
+ throw new RejectedExecutionException("Executor is shutdown,
rejecting task");
+ }
+ try {
+ executor.getQueue().put(r);
+ }
+ catch (InterruptedException e) {
+ throw new RejectedExecutionException("Got Interrupted while
adding to the Queue", e);
+ }
+ }
+ );
+ }
+ return new ThreadPoolExecutor(
+ minThreads,
+ maxThreads,
+ keepAliveTime,
+ keepAliveTimeUnit,
+ queue,
+ makeThreadFactory(nameFormat, priority),
+ (r, executor) -> {
+ if (executor.isShutdown()) {
+ throw new RejectedExecutionException("Executor is shutdown,
rejecting task");
+ }
+ try {
+ executor.getQueue().put(r);
+ }
+ catch (InterruptedException e) {
+ throw new RejectedExecutionException("Got Interrupted while adding
to the Queue", e);
+ }
+ }
+ )
+ {
+ private int running = 0;
+
+ @Override
+ public void execute(Runnable command)
+ {
+ synchronized (this) {
+ running++;
+ growIfNeeded();
Review Comment:
I thought this too initially 😅 , but when I dug into it, these pools
actually only grow when the queue feeding them is full, preferring to wait in a
queue over the overhead of making new threads (see
https://docs.oracle.com/en/java/javase/21/docs//api/java.base/java/util/concurrent/ThreadPoolExecutor.html).
So a pool with an unbounded queue will only ever have as many threads as the
minimum count.
TBH I don't actually know if we need a pool that can grow and shrink, a
fixed size pool would probably be fine. ideally I think I would do this all
with virtual threads in java 21 - I did some tests and they works pretty well
and then we wouldn't need to define a thread pool here at all.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]