Skip to content

Commit bfa6958

Browse files
authored
Limit ReservedThread creation (#13837)
Updated the `ReservedThreadExecutor` default value for `maxThreads` to be `0`, meaning the capacity. In this way, by default, the reserved threads that get created are at most 2 times the capacity, rather than an unlimited number. Signed-off-by: Oleksandr Krutko <alexander.krutko@gmail.com>
1 parent 785a963 commit bfa6958

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ReservedThreadExecutor.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public ReservedThreadExecutor(Executor executor, int capacity)
7474
*/
7575
public ReservedThreadExecutor(Executor executor, int capacity, int minSize)
7676
{
77-
this(executor, capacity, minSize, -1);
77+
this(executor, capacity, minSize, 0);
7878
}
7979

8080
/**
@@ -83,7 +83,7 @@ public ReservedThreadExecutor(Executor executor, int capacity, int minSize)
8383
* is calculated based on a heuristic from the number of available processors and
8484
* thread pool type.
8585
* @param minSize The minimum number of reserve Threads that the algorithm tries to maintain, or -1 for a heuristic value.
86-
* @param maxPending The maximum number of reserved Threads to start, or -1 for no limit.
86+
* @param maxPending The maximum number of reserved Threads to start, 0 (default) for thread pool capacity or -1 for no limit.
8787
*/
8888
public ReservedThreadExecutor(Executor executor, int capacity, int minSize, int maxPending)
8989
{
@@ -92,9 +92,14 @@ public ReservedThreadExecutor(Executor executor, int capacity, int minSize, int
9292
_minSize = minSize < 0 ? Math.min(1, _threads.capacity()) : minSize;
9393
if (_minSize > _threads.capacity())
9494
throw new IllegalArgumentException("minSize larger than capacity");
95-
_maxPending = maxPending;
96-
if (_maxPending == 0)
97-
throw new IllegalArgumentException("maxPending cannot be 0");
95+
96+
if (maxPending < 0)
97+
_maxPending = -1;
98+
else if (maxPending == 0)
99+
_maxPending = _threads.capacity();
100+
else
101+
_maxPending = maxPending;
102+
98103
if (LOG.isDebugEnabled())
99104
LOG.debug("{}", this);
100105
installBean(_executor);
@@ -227,7 +232,7 @@ public boolean tryExecute(Runnable task)
227232

228233
private void startReservedThread()
229234
{
230-
if (_maxPending > 0 && _pending.incrementAndGet() >= _maxPending)
235+
if (_maxPending > 0 && _pending.incrementAndGet() > _maxPending)
231236
{
232237
_pending.decrementAndGet();
233238
return;

0 commit comments

Comments
 (0)