I'm using LinkedBlockingQueue
between two different threads. One thread adds data via add
, while the other thread receives data via take
.
My question is, do I need to synchronize access to add
and take
. Is LinkedBlockingQueue
's insert and remove methods thread safe?
ArrayBlockingQueue is thread-safe. The Iterator provided in iterator() method traverses the elements in order from first (head) to last (tail). It supports an optional fairness policy for ordering waiting producer and consumer threads.
Thread safety is a computer programming concept applicable to multi-threaded code. Thread-safe code only manipulates shared data structures in a manner that ensures that all threads behave properly and fulfill their design specifications without unintended interaction.
The LinkedBlockingQueue is an optionally-bounded blocking queue based on linked nodes. It means that the LinkedBlockingQueue can be bounded, if its capacity is given, else the LinkedBlockingQueue will be unbounded. The capacity can be given as a parameter to the constructor of LinkedBlockingQueue.
Yes. From the docs:
"BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically unless specified otherwise in an implementation. So it is possible, for example, for addAll(c) to fail (throwing an exception) after adding only some of the elements in c."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With