Java BlockingQueue with multiple consumer threads unfair

244 views Asked by At

I'm creating a resource pool using a (java) LinkedBlockingQueue, where

  • the resource elements are equivalent, belong to a pool where their ordering is indifferent.
  • the consumers are competing threads grabbing one resource at a time, with a "pull" operation, working with the resource, and then giving it back to the pool, with an "add" operation.
  • While a particular resource is being used by a consumer thread it must not be available to other consumer threads.

The problem is: LinkedBlockingQueue does not make a FIFO of waiting consumers, and the level of service is not uniform.

Any ideas on the topic ? Thanks in advance.

1

There are 1 answers

1
Tregoreg On

I understand that your situation may require this type of design, but repeatedly taking resources from a queue and putting them back seems slightly unusual to me.

Couldn't you simple have a fixed pool of workers (each bound to its resources) and use the LinkedBlockingQueue for distributing jobs/work instead?