Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java -BlockingQueue -- Multiple producers, single consumer

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.

like image 599
Sheriff Avatar asked Dec 11 '12 19:12

Sheriff


People also ask

Can a producer have more than one consumer?

Re: Multiple producer one consumerYes, you can.

What is producer-consumer problem in multithreading?

In computing, the producer-consumer problem (also known as the bounded-buffer problem) is a classic example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, which share a common, fixed-size buffer used as a queue.

What is Producer-consumer relationship in Java?

Producer and Consumer are two separate processes. Both processes share a common buffer or queue. The producer continuously produces certain data and pushes it onto the buffer, whereas the consumer consumes those data from the buffer.

What are the different ways to solve producer-consumer problem in Java?

We can solve the producer-consumer problem using semaphores, Semaphore is a thread synchronization construct. Semaphore is used to control the access to a shared resource with the help of a variable/counter. The other way to solve this is using a thread. A thread is a sequential flow of control in a program.


1 Answers

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.

like image 62
NPE Avatar answered Oct 02 '22 04:10

NPE