有没有一种简单的方法来判断 Java 执行器在给定时刻正在运行哪些任务?
我确信我可以将一些东西组合在一起,这将使我能够解决这个问题,但我希望有一个我所缺少的开箱即用的解决方案。我阅读了文档,但没有看到任何内容。
我的特定应用程序使用由 DelayQueue
支持的 ThreadPoolExecutor
,尽管我不确定这是否重要。
谢谢!
I'm sure I could hack something together which would enable me to figure this out, but I'm hoping there's an out-of-the-box solution I'm just missing. I read the docs but I didn't see anything.
My specific app is using a ThreadPoolExecutor
backed by a DelayQueue
, although I'm not sure that matters.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种简单的方法:用代码包装您的任务,该代码将调用回调信号通知新任务的开始,并传递您想要的所有数据:
One simple approach: wrap your tasks with code which will call callback signalling start of new task, passing along all data you will want it to:
我认为没有可靠的方法可以做到这一点。如果您想要计数,可以使用可用的方法(getTaskCount()/getCompletedTaskCount())来帮助。如果您需要实际任务,那么据我所知,您需要手动跟踪任务,通过覆盖 beforeExecute() 和 afterExecute() 是一种方法。
I don't think there is a reliable way to do this. If you want counts there are methods available (getTaskCount()/getCompletedTaskCount()) which help. If you need the actual tasks then AFAIK you need manually to keep track of the tasks, by overriding beforeExecute() and afterExecute() is one way.
您实际上需要在运行时知道,还是只是为了调试?
如果您只是进行调试,请考虑查看 JVisualVM 应用程序(安装在 JDK 的“bin”目录中)。它允许您动态检查所有正在运行的线程,因此它应该能够为您提供一些见解。
(JVisualVM 还可以执行 CPU/内存分析、执行和分配热点,并实时显示垃圾收集活动。)
Do you actually need to know at runtime, or just for debugging?
If you're just debugging, consider taking a look at the JVisualVM application (installed in the 'bin' directory of the JDK). It lets you dynamically inspect all running threads, so it should be able to provide you with some insight.
(The JVisualVM can also perform CPU/Memory profiling, with execution & allocation hotspots, and shows garbage collection activity in realtime.)