Question

I'm trying to model a situation in Java in which many producers (at least 2) access the same LinkedBlockingQueue at a fixed rate. They produce, put, and then start over again. I was wondering whether this could eventually lead to race conditions between those producers which try to gain write access on the queue at the same time. Are java.util.concurrent.BlockingQueue's implementations already set up to handle such an issue, or should I manually create mutexes in order to avoid this kind of problems?

Thank you for your attention.

Was it helpful?

Solution

java's blocking queues are thread-safe for single operations such as take and put but are not for multiple operations of put or take operations such as addAll which is not being performed atomically.

so in your case the answer is no, you should not handle the thread-safety yourself unless you would like the producers to produce multiple products and put them all in one operation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top