I am implementing producer-consumer where items to be consumed are of 1000 types type-0, type-1, ..., type-999.
Producer inserts item of type-i in queue. Consumer uses that to update state machine for type-i(one state machine per type-i) based on item(type-i)removed from queue.There are multiple consumers that are reading from same queue (and one producer)
Condition is if some consumer is processing item of type-i then other consumers cannot process item of same type so if other consumers pick up next items of same type type-i, all consumers get blocked which is not good solution.
Another solution is to have 1000 queues and 1000 consumers for each type-i which is not feasible
Another solution is to have 10 queues and 10 consumers(one for each queue) and assign 100 types to Q-1, 100 types to Q2 etc. But problem is if consumer-j takes lots of time to process item-i all other items in queue will face delay though other consumer-k might be idle.
Whats good design pattern we can use here ?
UPDATE:
Solution-1 : (1 Queue - 1 Consumer) per type : Best solution but lot of threads
Solution-2: (1 Queue - m Consumers) per n Types : Problem is we might reach state where all consumers are waiting on processing type-i as first consumer still working on type-i item holding lock(type-i)
UPDATE 2:
Here ordering of items is IMP and cannot be altered. You can assume one state machine per type-i and consumer modifies state as per new item(of type-i) received. Now u can see why item ordering is important