Spring - 调度和池化不同状态的可运行对象(每个可运行对象实例具有不同的状态)

发布于 2024-09-05 10:03:24 字数 1064 浏览 4 评论 0原文

我不知道使用什么来调度和池化不同状态的可运行对象(每个可运行实例具有不同的状态)。我可以将 ScheduledExecutorFactoryBean 与 MethodInvokingRunnable 一起使用来提供参数。但看看关键的 ScheduledExecutorFactoryBean 方法,它的设计方式是所有任务都应该从头开始。

    protected void registerTasks(ScheduledExecutorTask[] tasks, ScheduledExecutorService executor) {
        for (ScheduledExecutorTask task : tasks) {
            Runnable runnable = getRunnableToSchedule(task);
            if (task.isOneTimeTask()) {
                executor.schedule(runnable, task.getDelay(), task.getTimeUnit());
            }
            else {
                if (task.isFixedRate()) {
                    executor.scheduleAtFixedRate(runnable, task.getDelay(), task.getPeriod(), task.getTimeUnit());
                }
                else {
                    executor.scheduleWithFixedDelay(runnable, task.getDelay(), task.getPeriod(), task.getTimeUnit());
                }
            }
        }
}

我想不出如何使用 ThreadPoolTask​​Scheduler 设置这个场景。

请帮我一下。谢谢


编辑:缩短版本是:如何设置任务调度程序,以 2 秒的间隔运行数百个“不同的线程实例(具有不同的状态)”。

I can't figure out what to use for scheduling and pooling runnables of different state (each Runnable instance has different state). I could use ScheduledExecutorFactoryBean together with MethodInvokingRunnable to supply arguments. But take a look at the key ScheduledExecutorFactoryBean method, it is designed in a way that all task should start at the beginning.

    protected void registerTasks(ScheduledExecutorTask[] tasks, ScheduledExecutorService executor) {
        for (ScheduledExecutorTask task : tasks) {
            Runnable runnable = getRunnableToSchedule(task);
            if (task.isOneTimeTask()) {
                executor.schedule(runnable, task.getDelay(), task.getTimeUnit());
            }
            else {
                if (task.isFixedRate()) {
                    executor.scheduleAtFixedRate(runnable, task.getDelay(), task.getPeriod(), task.getTimeUnit());
                }
                else {
                    executor.scheduleWithFixedDelay(runnable, task.getDelay(), task.getPeriod(), task.getTimeUnit());
                }
            }
        }
}

I can't think of how to setup this scenario with ThreadPoolTaskScheduler.

Please help me out here. Thank you


EDIT: shortened version is: how to setup task scheduler that would run hundreds of "different instances of Threads (with different state) with 2 seconds interval.

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

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

发布评论

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

评论(1

咆哮 2024-09-12 10:03:24

这很简单,我不知道我在想什么,我没有看到它:-)


我刚刚以编程方式用数千个任务填充了 ScheduledExecutorFactoryBean 的 ScheduledExecutorTask[] 数组,每个任务都具有递增的延迟属性和不同的可运行项。然后我就使用了factorybean...来自spring的家伙的非常方便的factorybean...

It's easy, I don't know what I was thinking that I didn't see it :-)


I have just programmatically filled the array ScheduledExecutorTask[] of ScheduledExecutorFactoryBean with thousands of tasks, each with incremented delay property and different runnable. Then I just used the factorybean...Really handy factorybean from spring guys...

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