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:
parent
2b227f121e
commit
b70d22fee8
|
@ -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.
|
||||
*
|
||||
|
@ -51,7 +52,7 @@ public class AtomicTriInteger extends AtomicLong
|
|||
*/
|
||||
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)
|
||||
{
|
||||
while(true)
|
||||
while (true)
|
||||
{
|
||||
long encoded = get();
|
||||
long update = encode(
|
||||
getWord0(encoded)+delta0,
|
||||
getWord1(encoded)+delta1,
|
||||
getWord2(encoded)+delta2);
|
||||
if (compareAndSet(encoded,update))
|
||||
getWord0(encoded) + delta0,
|
||||
getWord1(encoded) + delta1,
|
||||
getWord2(encoded) + delta2);
|
||||
if (compareAndSet(encoded, update))
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -150,26 +150,26 @@ public class AtomicTriInteger extends AtomicLong
|
|||
*/
|
||||
public static long encode(int w0, int w1, int w2)
|
||||
{
|
||||
if (w0<MIN_VALUE
|
||||
|| w0>MAX_VALUE
|
||||
|| w1<MIN_VALUE
|
||||
|| 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;
|
||||
return (wl0<<42)+(wl1<<21)+(wl2);
|
||||
if (w0 < MIN_VALUE
|
||||
|| w0 > MAX_VALUE
|
||||
|| w1 < MIN_VALUE
|
||||
|| w1 > MAX_VALUE
|
||||
|| w2 < MIN_VALUE
|
||||
|| w2 > MAX_VALUE)
|
||||
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);
|
||||
return String.format("{%d,%d,%d}",w0,w1,w2);
|
||||
long encoded = get();
|
||||
int w0 = getWord0(encoded);
|
||||
int w1 = getWord1(encoded);
|
||||
int w2 = getWord2(encoded);
|
||||
return String.format("{%d,%d,%d}", w0, w1, w2);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,28 +200,22 @@ public class QueuedThreadPool extends ContainerLifeCycle implements SizedThreadP
|
|||
joinThreads(System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeout) / 2);
|
||||
|
||||
Thread.yield();
|
||||
int size = _threads.size();
|
||||
if (size > 0)
|
||||
if (LOG.isDebugEnabled())
|
||||
{
|
||||
Thread.yield();
|
||||
|
||||
if (LOG.isDebugEnabled())
|
||||
for (Thread unstopped : _threads)
|
||||
{
|
||||
for (Thread unstopped : _threads)
|
||||
StringBuilder dmp = new StringBuilder();
|
||||
for (StackTraceElement element : unstopped.getStackTrace())
|
||||
{
|
||||
StringBuilder dmp = new StringBuilder();
|
||||
for (StackTraceElement element : unstopped.getStackTrace())
|
||||
{
|
||||
dmp.append(System.lineSeparator()).append("\tat ").append(element);
|
||||
}
|
||||
LOG.warn("Couldn't stop {}{}", unstopped, dmp.toString());
|
||||
dmp.append(System.lineSeparator()).append("\tat ").append(element);
|
||||
}
|
||||
LOG.warn("Couldn't stop {}{}", unstopped, dmp.toString());
|
||||
}
|
||||
else
|
||||
{
|
||||
for (Thread unstopped : _threads)
|
||||
LOG.warn("{} Couldn't stop {}",this,unstopped);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (Thread unstopped : _threads)
|
||||
LOG.warn("{} Couldn't stop {}",this,unstopped);
|
||||
}
|
||||
|
||||
// Close any un-executed jobs
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue