From 9f285d92e71cc7bf3d1109fc266594e515b232bd Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 7 Aug 2017 10:30:12 +1000 Subject: [PATCH] Issue #1637 Limit reserved threads if threadpool is of small size --- .../util/thread/ReservedThreadExecutor.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ReservedThreadExecutor.java b/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ReservedThreadExecutor.java index 95045b29386..0d62fac9954 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ReservedThreadExecutor.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ReservedThreadExecutor.java @@ -50,12 +50,29 @@ public class ReservedThreadExecutor extends AbstractLifeCycle implements Executo /** * @param executor The executor to use to obtain threads - * @param capacity The number of threads to preallocate. If less than 0 then the number of available processors is used. + * @param capacity The number of threads to preallocate. If less than 0 then capacity + * is calculated based on a heuristic from the number of available processors and + * thread pool size. */ public ReservedThreadExecutor(Executor executor,int capacity) { _executor = executor; - _queue = new ReservedThread[capacity>=0?capacity:Runtime.getRuntime().availableProcessors()]; + + if (capacity < 0) + { + if (executor instanceof ThreadPool) + { + int threads = ((ThreadPool)executor).getThreads(); + int cpus = Runtime.getRuntime().availableProcessors(); + capacity = Math.max(1,Math.min(cpus,threads/8)); + } + else + { + capacity = Runtime.getRuntime().availableProcessors(); + } + } + + _queue = new ReservedThread[capacity]; } public Executor getExecutor()