暂停 TimerTask 直到下一次执行
我正在使用 TimerTask 运行一些定期任务,该任务正在处理一组文件。我有一个要求,如果要处理的文件数量超过预先确定的限制,线程将暂停执行并等待下一个周期再次开始处理文件。有没有办法将 TimerTask 挂起直到下一个执行周期,或者我是否必须扩展 TimerTask 类才能实现此功能?我看到有一个 TimerTask.cancel 方法,但这将取消该线程的所有进一步执行。我不希望这种事发生。我只想让线程挂起直到下一个执行周期。由于我们的框架使用 TimerTask,所以我没有机会迁移到 Java 中的任何其他并发类,而且我必须坚持使用它。
如有任何建议、指示或提示,我们将不胜感激。
谢谢,
阿莎
I am using a TimerTask to run some periodic tasks, the task being processing a set of files. I have a requirement where if the number of files to be processed exceeds a pre-determined limit, the thread suspends execution and waits till the next cycle to start processing the files again. Is there a way to suspend the TimerTask until the next execution period or do I have to extend the TimerTask class to achieve this functionality? I saw there is a TimerTask.cancel method, but this will cancel all further executions of this thread. I don't want this to happen. I just want the thread to be suspended until the next execution period. I don't have the luxury of moving to any of the other concurrent classes in Java as our framework uses TimerTask, and I have to stick with it.
Any suggestions, pointers or tips are greatly appreciated.
thanks,
Asha
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是将任务放在 < code>BlockingQueue 并让
TimerTask
remove()
处理条目,直到队列为空或达到预先确定的限制。任何剩余的条目将在队列中等待下一次计划的执行。One approach would be to put the tasks on a
BlockingQueue
and let theTimerTask
remove()
and process entries until the queue is empty or the pre-determined limit has been reached. Any remaining entries will be in the queue waiting for the next scheduled execution.