开发者

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

I'd like to log the number of active threads cre开发者_如何学Cated by a newCachedThreadPool call. I can't seem to find out where to get this value though.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜