如何在可变时间安排一组线程(任务)?
我必须在一天中的不同时间异步启动 10 个任务,直到第二天的某个时间。越接近第二天的时间,我就越需要重复这 10 项任务。
我的问题是我应该如何处理这个问题?我应该使用什么执行器?管理内存的最佳方法是什么?
我考虑使用 Executors.newScheduledThreadPool 来在不同的时间启动一个包含 10 个任务的线程池进程。这个问题要求我启动一组新的任务,即使前一组任务尚未完成(因此可能每次都会触发一个新的线程池)。
我还在考虑使用某种进程注册表来管理已启动的不同进程。当进程不再使用时,注册表可以停止它。
每次任务完成时,我都会想到刷新可运行对象并停止线程池。总体来说这是一个好的解决方案吗?
可能出现的问题是线程池内存饱和。也许对线程池设置时间限制?
I have to launch 10 tasks asynchronously at variable times through out the day until a certain hour the next day. The closer I get to the time the next day the more I have to repeat these 10 tasks.
My question, is how should I manage this? What executors should I use? What is the best way to manage the memory?
I thought of using an Executors.newScheduledThreadPool that could start a threadpool process with the 10 tasks at variable times. The problem requires me to launch a new set of tasks even though the previous group of tasks have not finished (so probably trigger a new threadpool each time).
I am also thinking of using sort of process registry to manage the different processes that have been launched. When a process is unused anymore than the registry can stop it.
And each time the tasks are done, I thought of flushing the runnables, and stopping the threadpool. Is that overall a good solution?
The problem that may arise, is to have the memory saturated with threadpools. Maybe put a time limit on the threadpool?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想,您需要在普通的非调度池中使用一个调度线程,并在另一个工作线程池中使用,如下所示:
无需重新创建线程池。
I guess, you need one dispatching thread inside plain non-scheduling pool and another pool for workers, something like this:
No need to recreate thread pools.