5

Quick clarification please

I know BlockingQueues are threadsafe.

Does that mean that I can pass a single reference to a blocking queue to all producers who can drop Events in willy-nilly to be consumed by a single consumer, and nothing gets disrupted?

Otherwise having to produce up to 20 BlockingQueues that may or may not have regular updates and reading them with any efficiency seems an insurmountable task.

Sheriff
  • 495
  • 4
  • 16
  • Possible duplicate of [Multiple blocking queues, single consumer](https://stackoverflow.com/questions/9588233/multiple-blocking-queues-single-consumer) – Amir Fo Oct 01 '19 at 14:56

2 Answers2

5

Does that mean that I can pass a single reference to a blocking queue to all producers who can drop Events in willy-nilly to be consumed by a single consumer, and nothing gets disrupted?

In a word, yes. This is safe. To quote the documentation:

BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

If it's threadsafe that means that you only need one instance of that queue which can be accessed by all threads. The concurrent data structure manages those accesses. This also means that no synchronization from your side is needed.

Zhedar
  • 3,480
  • 1
  • 21
  • 44
  • Thank you very much. Just wanted to be safe before I committed to this route. In the past I've always had producer/consumer dyads, but I'm trying to centralize now. – Sheriff Dec 11 '12 at 19:52
  • 1
    The concurrent collection seem to be a save way to go for the future as they're also quite performant ;) – Zhedar Dec 11 '12 at 19:55