如何在可变时间安排一组线程(任务)?

发布于 2024-12-11 03:38:01 字数 383 浏览 2 评论 0原文

我必须在一天中的不同时间异步启动 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 技术交流群。

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

发布评论

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

评论(1

想念有你 2024-12-18 03:38:01

我想,您需要在普通的非调度池中使用一个调度线程,并在另一个工作线程池中使用,如下所示:

    ExecutorService ex = Executors.newFixedThreadPool(1);
    final ExecutorService workersPool = Executors.newCachedThreadPool();

    ex.submit(new Runnable() {
        public void run() {
            try {
                do { 
                    // determine if it's time to start workers
                    if (timeToStartWorkers()) {
                        workersPool.submit(new Worker(...));
                        workersPool.submit(new Worker(...));
                        ...
                    }
                    // sleep till next time
                    Thread.sleep(timeTillNextCheck);
                }
            } catch (InterruptedException e) {
                // handle exception
            }
        }
    });

无需重新创建线程池。

I guess, you need one dispatching thread inside plain non-scheduling pool and another pool for workers, something like this:

    ExecutorService ex = Executors.newFixedThreadPool(1);
    final ExecutorService workersPool = Executors.newCachedThreadPool();

    ex.submit(new Runnable() {
        public void run() {
            try {
                do { 
                    // determine if it's time to start workers
                    if (timeToStartWorkers()) {
                        workersPool.submit(new Worker(...));
                        workersPool.submit(new Worker(...));
                        ...
                    }
                    // sleep till next time
                    Thread.sleep(timeTillNextCheck);
                }
            } catch (InterruptedException e) {
                // handle exception
            }
        }
    });

No need to recreate thread pools.

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