1

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.

artejera
  • 1,346
  • 1
  • 11
  • 19
  • 1
    Have you ruled out using an off-the-shelf resource pooling library like [Apache commons-pool](https://commons.apache.org/proper/commons-pool/)? – dnault May 01 '20 at 18:12
  • 1
    See [Is there any unbounded fair blocking queue in java?](https://stackoverflow.com/questions/4046838/is-there-any-unbounded-fair-blocking-queue-in-java) – dnault May 01 '20 at 18:14

1 Answers1

1

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?

Tregoreg
  • 18,872
  • 15
  • 48
  • 69
  • Well I think that this design applies if it is relatively expensive to create a new resource, or not desirable to create more than N resources for any number of consumers. – artejera May 01 '20 at 18:13