Java并发执行线程任务
我有一项任务需要按计划执行。 (它基本上轮询数据库以查找更改,然后根据结果执行代码)。问题是我需要轮询任务发生,即使它已经在执行。
到目前为止,我已经尝试使用带有 scheduleAtFixedRate()
方法的 Timer/TimerTask 组合以及带有 scheduleAtFixedRate()
方法的 ScheduledThreadPoolExecutor/Thread 组合。
两者都等待当前计划任务完成,然后再运行下一个任务。我需要能够安排一个任务每 5 秒运行一次,并让它运行,即使该任务的最后一次执行尚未完成。
有什么想法吗?
I have a task that needs to be executed on a schedule. (It basically polls a database looking for a change and then executes code depending on the result). The problem is that I need the polled task to happen even when it is already executing.
So far I have tried using a Timer/TimerTask combo with the scheduleAtFixedRate()
method and the ScheduledThreadPoolExecutor/Thread combo with the scheduleAtFixedRate()
method.
Both wait for the current scheduled task to complete before running the next. I need to be able to schedule a task to run every 5 seconds and have it run even if the last execution of the task has not yet completed.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何使用一个
Timer
作为“启动”计时器,然后使用一个单独的线程池来执行:当计时器计时时,您将任务提交到线程池以立即执行。 (您可能需要调整线程池以同时运行一些最大数量的任务。)How about using one
Timer
as the "kick-off" timer, but then a separate thread pool for execution: when the timer ticks, you submit the task to the thread pool for immediate execution. (You may want to tweak the thread pool to have some maximum number of tasks running simultaneously.)