Issue #3550 - QueuedThreadPool cleanup.

Improved code formatting.
Removed unnecessary code in doStop().
Now explicitly checking whether idleTimeout > 0 before shrinking.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2019-05-22 18:17:44 +02:00
parent 2b227f121e
commit b70d22fee8
2 changed files with 46 additions and 50 deletions

View File

@ -27,6 +27,7 @@ public class AtomicTriInteger extends AtomicLong
{
public static int MAX_VALUE = 0x1FFFFF;
public static int MIN_VALUE = 0;
/**
* Sets the hi and lo values.
*
@ -75,7 +76,6 @@ public class AtomicTriInteger extends AtomicLong
}
}
/**
* Gets word 0 value
*
@ -115,7 +115,7 @@ public class AtomicTriInteger extends AtomicLong
*/
public static int getWord0(long encoded)
{
return (int) ((encoded>>42)&0x1FFFFFL);
return (int)((encoded >> 42) & MAX_VALUE);
}
/**
@ -126,7 +126,7 @@ public class AtomicTriInteger extends AtomicLong
*/
public static int getWord1(long encoded)
{
return (int) ((encoded>>21)&0x1FFFFFL);
return (int)((encoded >> 21) & MAX_VALUE);
}
/**
@ -137,7 +137,7 @@ public class AtomicTriInteger extends AtomicLong
*/
public static int getWord2(long encoded)
{
return (int) (encoded&0x1FFFFFL);
return (int)(encoded & MAX_VALUE);
}
/**
@ -156,20 +156,20 @@ public class AtomicTriInteger extends AtomicLong
|| w1 > MAX_VALUE
|| w2 < MIN_VALUE
|| w2 > MAX_VALUE)
throw new IllegalArgumentException(String.format("Words must be 0<= word <= 0x1FFFFF: %d, %d, %d", w0, w1, w2));
long wl0 = ((long)w0)&0x1FFFFFL;
long wl1 = ((long)w1)&0x1FFFFFL;
long wl2 = ((long)w2)&0x1FFFFFL;
throw new IllegalArgumentException(String.format("Words must be %d <= word <= %d: %d, %d, %d", MIN_VALUE, MAX_VALUE, w0, w1, w2));
long wl0 = ((long)w0) & MAX_VALUE;
long wl1 = ((long)w1) & MAX_VALUE;
long wl2 = ((long)w2) & MAX_VALUE;
return (wl0 << 42) + (wl1 << 21) + (wl2);
}
@Override
public String toString()
{
long value = get();
int w0 = getWord0(value);
int w1 = getWord1(value);
int w2 = getWord2(value);
long encoded = get();
int w0 = getWord0(encoded);
int w1 = getWord1(encoded);
int w2 = getWord2(encoded);
return String.format("{%d,%d,%d}", w0, w1, w2);
}
}

View File

@ -143,7 +143,9 @@ public class QueuedThreadPool extends ContainerLifeCycle implements SizedThreadP
protected void doStart() throws Exception
{
if (_reservedThreads==0)
{
_tryExecutor = NO_TRY;
}
else
{
ReservedThreadExecutor reserved = new ReservedThreadExecutor(this,_reservedThreads);
@ -198,11 +200,6 @@ public class QueuedThreadPool extends ContainerLifeCycle implements SizedThreadP
joinThreads(System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeout) / 2);
Thread.yield();
int size = _threads.size();
if (size > 0)
{
Thread.yield();
if (LOG.isDebugEnabled())
{
for (Thread unstopped : _threads)
@ -220,7 +217,6 @@ public class QueuedThreadPool extends ContainerLifeCycle implements SizedThreadP
for (Thread unstopped : _threads)
LOG.warn("{} Couldn't stop {}",this,unstopped);
}
}
// Close any un-executed jobs
while (!_jobs.isEmpty())
@ -784,7 +780,6 @@ public class QueuedThreadPool extends ContainerLifeCycle implements SizedThreadP
@Override
public void run()
{
boolean idle = false;
Runnable job = null;
@ -807,14 +802,15 @@ public class QueuedThreadPool extends ContainerLifeCycle implements SizedThreadP
_counts.add(0,0,1); // threads, starting, idle
}
job = idleJobPoll();
long idleTimeout = getIdleTimeout();
job = idleJobPoll(idleTimeout);
// maybe we should shrink?
if (job == null && getThreads() > _minThreads)
if (job == null && getThreads() > _minThreads && idleTimeout > 0)
{
long last = _lastShrink.get();
long now = System.nanoTime();
if (last == 0 || (now - last) > TimeUnit.MILLISECONDS.toNanos(_idleTimeout))
if (last == 0 || (now - last) > TimeUnit.MILLISECONDS.toNanos(idleTimeout))
{
if (_lastShrink.compareAndSet(last, now))
{
@ -872,11 +868,11 @@ public class QueuedThreadPool extends ContainerLifeCycle implements SizedThreadP
}
}
private Runnable idleJobPoll() throws InterruptedException
private Runnable idleJobPoll(long idleTimeout) throws InterruptedException
{
if (_idleTimeout <= 0)
if (idleTimeout <= 0)
return _jobs.take();
return _jobs.poll(_idleTimeout, TimeUnit.MILLISECONDS);
return _jobs.poll(idleTimeout, TimeUnit.MILLISECONDS);
}
}
}