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 MAX_VALUE = 0x1FFFFF;
public static int MIN_VALUE = 0; public static int MIN_VALUE = 0;
/** /**
* Sets the hi and lo values. * Sets the hi and lo values.
* *
@ -51,7 +52,7 @@ public class AtomicTriInteger extends AtomicLong
*/ */
public boolean compareAndSet(long expectEncoded, int w0, int w1, int w2) public boolean compareAndSet(long expectEncoded, int w0, int w1, int w2)
{ {
return compareAndSet(expectEncoded,encode(w0, w1, w2)); return compareAndSet(expectEncoded, encode(w0, w1, w2));
} }
/** /**
@ -63,19 +64,18 @@ public class AtomicTriInteger extends AtomicLong
*/ */
public void add(int delta0, int delta1, int delta2) public void add(int delta0, int delta1, int delta2)
{ {
while(true) while (true)
{ {
long encoded = get(); long encoded = get();
long update = encode( long update = encode(
getWord0(encoded)+delta0, getWord0(encoded) + delta0,
getWord1(encoded)+delta1, getWord1(encoded) + delta1,
getWord2(encoded)+delta2); getWord2(encoded) + delta2);
if (compareAndSet(encoded,update)) if (compareAndSet(encoded, update))
return; return;
} }
} }
/** /**
* Gets word 0 value * Gets word 0 value
* *
@ -115,7 +115,7 @@ public class AtomicTriInteger extends AtomicLong
*/ */
public static int getWord0(long encoded) 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) 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) public static int getWord2(long encoded)
{ {
return (int) (encoded&0x1FFFFFL); return (int)(encoded & MAX_VALUE);
} }
/** /**
@ -150,26 +150,26 @@ public class AtomicTriInteger extends AtomicLong
*/ */
public static long encode(int w0, int w1, int w2) public static long encode(int w0, int w1, int w2)
{ {
if (w0<MIN_VALUE if (w0 < MIN_VALUE
|| w0>MAX_VALUE || w0 > MAX_VALUE
|| w1<MIN_VALUE || w1 < MIN_VALUE
|| w1>MAX_VALUE || w1 > MAX_VALUE
|| w2<MIN_VALUE || w2 < MIN_VALUE
|| w2>MAX_VALUE) || w2 > MAX_VALUE)
throw new IllegalArgumentException(String.format("Words must be 0<= word <= 0x1FFFFF: %d, %d, %d", w0, w1, w2)); 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)&0x1FFFFFL; long wl0 = ((long)w0) & MAX_VALUE;
long wl1 = ((long)w1)&0x1FFFFFL; long wl1 = ((long)w1) & MAX_VALUE;
long wl2 = ((long)w2)&0x1FFFFFL; long wl2 = ((long)w2) & MAX_VALUE;
return (wl0<<42)+(wl1<<21)+(wl2); return (wl0 << 42) + (wl1 << 21) + (wl2);
} }
@Override @Override
public String toString() public String toString()
{ {
long value = get(); long encoded = get();
int w0 = getWord0(value); int w0 = getWord0(encoded);
int w1 = getWord1(value); int w1 = getWord1(encoded);
int w2 = getWord2(value); int w2 = getWord2(encoded);
return String.format("{%d,%d,%d}",w0,w1,w2); 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 protected void doStart() throws Exception
{ {
if (_reservedThreads==0) if (_reservedThreads==0)
{
_tryExecutor = NO_TRY; _tryExecutor = NO_TRY;
}
else else
{ {
ReservedThreadExecutor reserved = new ReservedThreadExecutor(this,_reservedThreads); ReservedThreadExecutor reserved = new ReservedThreadExecutor(this,_reservedThreads);
@ -198,28 +200,22 @@ public class QueuedThreadPool extends ContainerLifeCycle implements SizedThreadP
joinThreads(System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeout) / 2); joinThreads(System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeout) / 2);
Thread.yield(); Thread.yield();
int size = _threads.size(); if (LOG.isDebugEnabled())
if (size > 0)
{ {
Thread.yield(); for (Thread unstopped : _threads)
if (LOG.isDebugEnabled())
{ {
for (Thread unstopped : _threads) StringBuilder dmp = new StringBuilder();
for (StackTraceElement element : unstopped.getStackTrace())
{ {
StringBuilder dmp = new StringBuilder(); dmp.append(System.lineSeparator()).append("\tat ").append(element);
for (StackTraceElement element : unstopped.getStackTrace())
{
dmp.append(System.lineSeparator()).append("\tat ").append(element);
}
LOG.warn("Couldn't stop {}{}", unstopped, dmp.toString());
} }
LOG.warn("Couldn't stop {}{}", unstopped, dmp.toString());
} }
else }
{ else
for (Thread unstopped : _threads) {
LOG.warn("{} Couldn't stop {}",this,unstopped); for (Thread unstopped : _threads)
} LOG.warn("{} Couldn't stop {}",this,unstopped);
} }
// Close any un-executed jobs // Close any un-executed jobs
@ -784,7 +780,6 @@ public class QueuedThreadPool extends ContainerLifeCycle implements SizedThreadP
@Override @Override
public void run() public void run()
{ {
boolean idle = false; boolean idle = false;
Runnable job = null; Runnable job = null;
@ -807,14 +802,15 @@ public class QueuedThreadPool extends ContainerLifeCycle implements SizedThreadP
_counts.add(0,0,1); // threads, starting, idle _counts.add(0,0,1); // threads, starting, idle
} }
job = idleJobPoll(); long idleTimeout = getIdleTimeout();
job = idleJobPoll(idleTimeout);
// maybe we should shrink? // maybe we should shrink?
if (job == null && getThreads() > _minThreads) if (job == null && getThreads() > _minThreads && idleTimeout > 0)
{ {
long last = _lastShrink.get(); long last = _lastShrink.get();
long now = System.nanoTime(); 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)) 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.take();
return _jobs.poll(_idleTimeout, TimeUnit.MILLISECONDS); return _jobs.poll(idleTimeout, TimeUnit.MILLISECONDS);
} }
} }
} }