Executors.newSingleThreadExecutor() - 如何查看队列中有多少任务
我在代码中使用 Executors.newSingleThreadExecutor() 。 我想监视队列中的任务数量,以检查处理器是否因消息而过载。如何获取当前未完成提交任务的数量? 我期待这样的事情:
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new Runnable() {...});
executor.submit(new Runnable() {...});
executor.submit(new Runnable() {...});
// do something and get 3
...
// do something and get 2
...
// do something and get 1
谢谢
I am using Executors.newSingleThreadExecutor() in my code.
I want to monitor number of tasks in queue to check that procesor is not overloaded with messages. How can I get a number of not completed submited tasks for curent moment?
I expect something like this:
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new Runnable() {...});
executor.submit(new Runnable() {...});
executor.submit(new Runnable() {...});
// do something and get 3
...
// do something and get 2
...
// do something and get 1
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
万一您想保证即使使用新版本的 JVM,FlorianOver 建议的代码也能工作,您可以这样做:(对于方法 Executors.newSingleThreadExecutor() 将不再返回类型的实例线程池执行器)
Just in case you want to guarantee that even with new versions of the JVM the code suggested by FlorianOver will work, you could do it this way: (For the case that the method Executors.newSingleThreadExecutor() will no longer return an instance of type ThreadPoolExecutor)
不是这样的吗?
Isn't it like this?