具有竞争队列的 Java 线程池
我有一种情况,我想使用 Java 固定线程池的扩展。我有 N 组可运行对象,我想争夺资源。但是,我希望使用的线程总数保持不变。这里概述了我希望其工作的方式
- 分配一个具有 N 个线程和 M 个队列的对象;
- 将作业 n 调度到队列 m 上。
- 有一个指向第一个队列的指针 重复 一个。如果当前正在使用最大线程数等待。 b.弹出当前队列中的作业 c.将指针移过一个队列(或从最后一个队列到第一个队列)
首先,这样的东西是否已经存在?其次,如果没有,我对编写自己的线程池感到紧张,因为我知道编写自己的线程池可能很危险。谁能给我指出一些写我自己的好例子。
I have a situation where I'd like to use an extension of Java's fixed thread pools. I have N groups of runnable objects that I'd like to compete for resources. However, I'd like the total number of threads used to remain constant. The way that I would like this to work is outlined here
- Allocate an object with N threads and M queues;
- Schedule job n on queue m.
- Have a pointer to the first queue
Repeat
a. If the maximum number of threads is currently in use wait.
b. Pop off a job on the current queue
c. Move the pointer one queue over (or from the last queue to the first)
First, does something like this already exist? Second if not, I'm nervous about writing my own because I know writing my own thread pools can be dangerous. Can anyone point me to some good examples for writing my own.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您最好的选择可能是创建自己的队列实现,该队列在其他队列中循环。例如(伪代码):
class CyclicQueue {
队列队列[];
整数当前= 0;
当然
,有了这个,如果你想要阻止,你需要自己添加它。
在这种情况下,您可能需要为每个队列一个自定义队列,它可以通知父队列该值已添加。
Your best bet is probably creating your own implementation of a Queue that cycles through other queues. For example (in pseudo-code):
class CyclicQueue {
Queue queues[];
int current = 0;
}
Of course, with this, if you want blocking you'll need to add that in yourself.
In which case, you'll probably want a custom Queue for each queue which can notify the parent queue that value has been added.