title: Producer Consumer with one producer and multiple consumers
body: <p>I am implementing producer-consumer where items to be consumed are of 1000 types type-0, type-1, ..., type-999.</p>
  
  <p>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)</p>
  
  <p>Condition is <em>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.</em></p>
  
  <p>Another solution is to have 1000 queues and 1000 consumers for each type-i which is not feasible</p>
  
  <p>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.</p>
  
  <p>Whats good design pattern we can use here ?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <ol>
  <li>Solution-1 : (1 Queue - 1 Consumer) per type : Best solution but lot of threads</li>
  <li>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)</li>
  </ol>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>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</p>