TimerService:有没有办法防止同时运行 2 个计时器实例?

发布于 2024-12-22 23:23:26 字数 204 浏览 5 评论 0原文

我使用 EJB3.1 TimerService 在 Java 中调度计时器。

有没有一种通用的方法来防止同时运行 2 个计时器实例? 假设我的计时器每 5 分钟触发一次。如果由于某种原因计时器在 5:00 开始运行并在 5:07 结束运行,我想下次在 5:12 运行(或在 5:10,跳过 5:05 的超时)。 有没有一种简单/内置的方法可以做到这一点?

谢谢

I use EJB3.1 TimerService to schedule timers in Java.

Is there a common way to prevent running 2 simultaneous timer instances?
Lets say my timer fires every 5 minutes. If for some reason timer started running at 5:00 and finished running at 5:07, I want to run next time at 5:12 (or at 5:10, just skip the timeout at 5:05).
Is there an easy/built-in way to do it?

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2024-12-29 23:23:26

只需让流程在完成后自行重新安排,而不是立即将它们全部安排好。

或者,您可以使用共享信号量来阻止一项作业,同时另一项作业正在运行。如果某项作业很少会停滞整整 5 分钟,那么这是一个简单的修复方法。如果这种情况并不罕见,那么作业将会堆积起来并变得不同步,因为即使已经存在被阻止的作业,计时器也会继续启动已知的作业。

Just have the process reschedule itself on completion rather than scheduling them all up at once.

Alternatively, you could have a shared semaphore that blocks one job while the other job is running. If it's rare for a job to stall the full 5 minutes, this is a simple fix. If it's not rare, then the jobs will stack up and get out of sync, as the timer will continue to start knew jobs even if there are blocked ones already.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文