Skip to content

Commit

Permalink
add QTP shrinkInterval to complement idleTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
magibney committed Jan 31, 2023
1 parent beee5ff commit 54524e3
Showing 1 changed file with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public class QueuedThreadPool extends ContainerLifeCycle implements ThreadFactor
private final ThreadFactory _threadFactory;
private String _name = "qtp" + hashCode();
private int _idleTimeout;
private int _shrinkInterval = -1;
private int _maxThreads;
private int _minThreads;
private int _reservedThreads = -1;
Expand Down Expand Up @@ -369,6 +370,30 @@ public void setIdleTimeout(int idleTimeout)
reserved.setIdleTimeout(idleTimeout, TimeUnit.MILLISECONDS);
}

/**
* @return the minimum interval at which the thread count will be reduced due to idleTimeout
*/
@ManagedAttribute("minimum interval between thread removals ms")
public int getShrinkInterval()
{
return _shrinkInterval < 0 ? _idleTimeout : _shrinkInterval;
}

/**
* <p>Set the minimum interval (ms) between thread removals.</p>
* <p>Controls the rate at which idle threads will be removed from the pool.</p>
*
* @param shrinkInterval the minimum interval between thread removals in ms
*/
public void setShrinkInterval(int shrinkInterval)
{
_shrinkInterval = shrinkInterval;
// TODO: do we need to pass shrinkInterval along to reserved pool?
// ReservedThreadExecutor reserved = getBean(ReservedThreadExecutor.class);
// if (reserved != null)
// reserved.setShrinkInterval(shrinkInterval, TimeUnit.MILLISECONDS);
}

/**
* @return the maximum number of threads
*/
Expand Down Expand Up @@ -1025,6 +1050,7 @@ public void run()
try
{
Runnable job = null;
long idleBaseline = NanoTime.now();
while (true)
{
// If we had a job,
Expand All @@ -1049,11 +1075,13 @@ else if (_counts.getHi() == Integer.MIN_VALUE)
{
// No job immediately available maybe we should shrink?
long idleTimeout = getIdleTimeout();
long shrinkInterval = getShrinkInterval();
if (idleTimeout > 0 && getThreads() > _minThreads)
{
long last = _lastShrink.get();
long last;
long now = NanoTime.now();
if (NanoTime.millisElapsed(last, now) > idleTimeout && _lastShrink.compareAndSet(last, now))
if (NanoTime.millisElapsed(idleBaseline, now) > idleTimeout &&
NanoTime.millisElapsed(last = _lastShrink.get(), now) > shrinkInterval && _lastShrink.compareAndSet(last, now))
{
if (LOG.isDebugEnabled())
LOG.debug("shrinking {}", QueuedThreadPool.this);
Expand All @@ -1076,6 +1104,7 @@ else if (_counts.getHi() == Integer.MIN_VALUE)
if (LOG.isDebugEnabled())
LOG.debug("run {} in {}", job, QueuedThreadPool.this);
runJob(job);
idleBaseline = NanoTime.now();
if (LOG.isDebugEnabled())
LOG.debug("ran {} in {}", job, QueuedThreadPool.this);
}
Expand Down

0 comments on commit 54524e3

Please sign in to comment.