动态线程池管理

发布于 2024-12-09 16:45:42 字数 241 浏览 1 评论 0原文

我在线程池中以最小阈值执行三种类型的任务 A、B、C:

  • (A) 70 %
  • (B) 20 %
  • (C) 10 %

池大小:100 个线程。

如何确保在任何给定时间内进行以下分配:

  1. 无空闲线程:例如,如果仅存在类型 C 的任务,则池中将 100% C 的任务
  2. 无饥饿:B 和 C 任务最终将得到服务

I have three types of Tasks A,B,C to be executed with minimum thresholds in a Thread Pool:

  • (A) 70 %
  • (B) 20 %
  • (C) 10 %

Pool Size: 100 Threads.

How to ensure in any given time the following distribution:

  1. No Idle Threads: If ,for example, only type C tasks exists, The pool will be 100% C's
  2. No starvation: B and C Tasks will eventually be served

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

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

发布评论

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

评论(1

妄司 2024-12-16 16:45:42

您应该尝试以下策略:

You should try the following strategy:

  • maintain a PriorityBlockingQueue with all items inserted into the queue having a priority field which is the basis for comparison. Let tasks of type C have priority 0, B have 10, and A have 20 (lower values == higher priority)
  • Maintain a separate 'tracking' queue of active B items in the PriorityBlockingQueue. A separate Timer instance attempts to remove all B items from the PriorityBlockingQueue, when successful it decrements the priority for B and reinserts it into the PriorityBlockingQueue - this ensures no starvation for items of type B. Unsuccessful attempts mean the item has already been processed and that the item should be removed from the tracking queue.
  • Threads in the thread pool attempt to pop an element of the queue before working on it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文