[concurrency-interest] Access to pool size
Tim Peierls
tim at peierls.net
Sun Aug 21 10:44:31 EDT 2005
Claude Hussenet wrote:
> Is there a way to have access to the pool size of an
> ExecutorService created through the factory methods
> of the Executors class?
>
> It looks like that I would have to instantiate
> directly
> ThreadPoolExecutor if I want to monitor the pool size
> of an ExecutorService.
>
> Please confirm...
It's true: the Executors factory methods aren't specified to return a
ThreadPoolExecutor, even though they do in practice.
If all you want is to retrieve the count of active threads in the pool,
however, you don't have to use a TPE constructor:
int getActiveThreadCount(ExecutorService exec) {
if (exec instanceof ThreadPoolExecutor) {
return ((ThreadPoolExecutor) exec).getActiveCount();
else // probably obtained with newSingleThreadExecutor
return 1;
}
That else-clause is a bit of a gamble; you might prefer to return some
distinguished value to indicate "unknown".
--tim
More information about the Concurrency-interest
mailing list