Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a synchronized queue in Java? [closed]

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.

like image 487
Shelef Avatar asked Mar 17 '13 12:03

Shelef


People also ask

Is queue synchronized in java?

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.

Is blocking queue synchronized?

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.

Is queue in java thread-safe?

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.

Is SynchronousQueue thread-safe?

SynchronousQueue is a special blocking queue with no internal capacity. It helps in exchange data or information between threads in a thread-safe manner.


2 Answers

You can use 'BlockingQueue', below link may help you to get better idea about it

BlockingQueue, Queue Implementations

like image 178
Vikas Singh Avatar answered Oct 09 '22 00:10

Vikas Singh


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.

like image 41
bsiamionau Avatar answered Oct 09 '22 01:10

bsiamionau