Track number of active threads yourself as ThreadPoolExecutor has a cost that is linear to
number of active threads. It also has the threadpool lock while counting.
definitely a no,no,no.
@@ -9,6 +9,8 @@ import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
import com.google.inject.Inject;
import com.yahoo.container.protect.ProcessTerminator;
@@ -141,6 +143,8 @@ public class ThreadPoolProvider extends AbstractComponent implements Provider<Ex
private final static class WorkerCompletionTimingThreadPoolExecutor extends ThreadPoolExecutor {
volatile long lastThreadReturnTimeMillis = System.currentTimeMillis();
+ AtomicLong startedCount = new AtomicLong(0);
+ AtomicLong completedCount = new AtomicLong(0);
public WorkerCompletionTimingThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
@@ -151,11 +155,23 @@ public class ThreadPoolProvider extends AbstractComponent implements Provider<Ex
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}
+ @Override
+ protected void beforeExecute(Thread t, Runnable r) {
+ super.beforeExecute(t, r);
+ startedCount.incrementAndGet();
+ }
+
@Override
protected void afterExecute(Runnable r, Throwable t) {
+ super.afterExecute(r, t);
lastThreadReturnTimeMillis = System.currentTimeMillis();
+ completedCount.decrementAndGet();
}
+ @Override
+ public int getActiveCount() {
+ return (int)(startedCount.get() - completedCount.get());
+ }
}
}
