Skip to content

Commit

Permalink
#9237 make startThread set shrinkThreshold in the future to guarante…
Browse files Browse the repository at this point in the history
…e at least one idleTimeout period without shrinking

Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban committed Mar 28, 2023
1 parent 1669716 commit 47247d9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ protected void startThread()
LOG.debug("Starting {}", thread);
_threads.add(thread);
// Update the shrink threshold to prevent thrashing
_shrinkThreshold.set(NanoTime.now());
_shrinkThreshold.set(NanoTime.now() + TimeUnit.MILLISECONDS.toNanos(_idleTimeout));
thread.start();
started = true;
}
Expand Down Expand Up @@ -1092,7 +1092,7 @@ public String toString()
int idle = Math.max(0, AtomicBiInteger.getLo(count));
int queue = getQueueSize();

return String.format("%s[%s]@%x{%s,%d<=%d<=%d,i=%d,r=%d,q=%d}[%s]",
return String.format("%s[%s]@%x{%s,%d<=%d<=%d,i=%d,r=%d,t=%d,q=%d}[%s]",
getClass().getSimpleName(),
_name,
hashCode(),
Expand All @@ -1102,6 +1102,7 @@ public String toString()
getMaxThreads(),
idle,
getReservedThreads(),
NanoTime.millisUntil(_shrinkThreshold.get()),
queue,
_tryExecutor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -1114,7 +1115,8 @@ public void testShrinkCount() throws Exception
tp.setMaxThreads(maxThreads);
int idleTimeout = 1000;
tp.setIdleTimeout(idleTimeout);
tp.setMaxShrinkCount(3);
int shrinkCount = 3;
tp.setMaxShrinkCount(shrinkCount);
tp.start();

waitForThreads(tp, minThreads);
Expand All @@ -1135,7 +1137,7 @@ public void testShrinkCount() throws Exception

assertEquals(maxThreads, tp.getThreads());

Thread.sleep(idleTimeout + idleTimeout / 2);
Thread.sleep(idleTimeout * 2 + idleTimeout / 2);
assertEquals(maxThreads - tp.getMaxShrinkCount(), tp.getThreads());

Thread.sleep(idleTimeout);
Expand Down

0 comments on commit 47247d9

Please sign in to comment.