pondermatic
pondermatic

Reputation: 6583

When using newCachedThreadPool how do I get the current number of threads

I'd like to log the number of active threads created by a newCachedThreadPool call. I can't seem to find out where to get this value though.

Upvotes: 2

Views: 1782

Answers (2)

Matthew T. Staebler
Matthew T. Staebler

Reputation: 4996

I am not sure whether you are interested in the number of threads that exist or whether you are interested in the number of tasks that are active. If you are interested in the tasks, then you can store a Future for each task that has been added. When you want to know how many tasks are active, you can simply count the number of Future objects that respond false to isDone. The ones that respond true can obviously be removed at that point.

Upvotes: 0

Guillaume
Guillaume

Reputation: 14656

You can pass a ThreadFactory to newCachedThreadPool Let it implement some logging when creating a new Thread, since newCachedThreadPool reuses threads you don't have to worry about threads end.

OR

Cast the executor into a ThreadPoolExecutor and call its getPoolSize() method

Upvotes: 4

Related Questions