Reputation: 15284
I need to get number of running threads in a ThreadPoolExecutor, as well as the Queue size.
I have a working implementation as (keep track of each futures in an arraylist and count which is not completed):
java.util.concurrent.ArrayBlockingQueue<Runnable> threadpoolQueue =
new java.util.concurrent.ArrayBlockingQueue<Runnable>(10);
ThreadPoolExecutor threadpool = new java.util.concurrent.ThreadPoolExecutor(0,
executingThreads, retryInterval, java.util.concurrent.TimeUnit.MILLISECONDS, threadpoolQueue);
ArrayList<Future> threads = new ArrayList<Future>();
threads.add(threadpool.submit(/*Runnable*/));
//Get Sizes
int threadpoolRunningThreads = 0;
for (Future obj : threads) {
if (!obj.isDone()) {
threadpoolRunningThreads++;
}
}
logger.debug("Merging all threads threadpool.size=" + threadpoolRunningThreads
+ ",threadpool.queue.size="+ threadpoolQueue.size());
This is a very awkward way of tracking threadpool, and I don't see any methods provided in threadpool that allows to get the number for running and queued threads. I wanna do something like:
threadpool.getIncompletedTaskSize();
threadpool.getQueuedTaskSize();
Can I achieve this? or at least something easier than my implementation? I am using Java 1.6.
Upvotes: 3
Views: 6191
Reputation: 721
Maybe ThreadPoolExecutor#getActiveCount()
and ThreadPoolExecutor#getQueue()#size()
?
Upvotes: 2