Is there a synchronized Queue
class in Java? I'm looking for something like Vector
(which is synchronized) vs ArrayList
(which is not), but instead of implementing the List
interface, I'm looking for it to implement Queue
.
Note that there is no Collections.synchronizedQueue
method to wrap an unsynchronized queue and make it synchronized.
Synchronous queues are similar to rendezvous channels used in CSP and Ada. They are well suited for handoff designs, in which an object running in one thread must sync up with an object running in another thread in order to hand it some information, event, or task.
No, you do not need to synchronize access to the object properties, or even use volatile on the member variables. All actions performed by a thread before it queues an object on a BlockingQueue "happen-before" the object is dequeued. That means that any changes made by the first thread are visible to the second.
Yes, that would be thread-safe, provided you never forget to do it. Encapsulating the queues in a thread-safe object, or using a thread-safe queue from the beginning would be much less fragile. So, indeed, this is good.
SynchronousQueue is a special blocking queue with no internal capacity. It helps in exchange data or information between threads in a thread-safe manner.
You can use 'BlockingQueue', below link may help you to get better idea about it
BlockingQueue, Queue Implementations
Look at ArrayBlockingQueue
and another BlockingQueue
implementations.
From documentation:
A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.
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