我正在实施一项任务处理服务,我想管理服务质量,为某些类型的任务提供更高的优先级。有四种类型的任务,因此我将使用四个队列,每种类型的任务一个。
- 创建四个处理线程(每个队列一个)并为它们分配不同的优先级是否方便?
- 或者我应该让处理线程主要处理更高优先级的队列?
- 还有其他方法吗?
I would suggest having a single thread that is responsible for grabbing tasks.
There are many, many possible strategies. One is simply to have 4 queues, and try to cycle between them. Another is to stick tasks into a priority queue (typically implemented with a heap data structure), but if you do that then be aware that all of the higher priority tasks will be taken before any lower priority tasks. A third is to use a priority queue based on age so you can take the oldest request first - then make high priority requests artificially old. (I could suggest the age of the oldest thing in the queue plus a constant term.)
One general point to keep in mind. If you assign sufficient capacity, your queues will likely remain reasonably short. If your capacity is insufficient, then queues will grow without bound and in the long run you can think of your queueing problem as one of triage rather than prioritization. But if possible, it often works well to try to increase capacity instead of being clever in prioritization.