Is there a synchronized queue in Java? [closed]
Asked Answered
C

3

13

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.

Celio answered 17/3, 2013 at 12:40 Comment(3)
ConcurrentLinkedDequeTrisa
If you look through the docs of the classes that extend docs.oracle.com/javase/7/docs/api/java/util/AbstractQueue.html you'll probably find what you're looking for. And docs.oracle.com/javase/7/docs/api/java/util/concurrent/… #2695926Minimalist
This seems like a very valid question, since Queue seems to be different from other Java collections in this respect. I am going to request that it is re-opened, having edited to clarify the question a little.Or
C
6

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.

Cutanddried answered 17/3, 2013 at 12:42 Comment(5)
This is a blocking queue, which is not what was requested.Or
@Or I believe OP wanted to find thread-safe implementation, in this case my answer is correctCutanddried
I was concerned about the blocking behaviour. If the reader/writer blocks, then this is very different behaviour than in a standard non-blocking queue. But reading the API, I think this is optional and you can read/write non-blocking as well. So you are correct.Or
It is possible to avoid blocking, but ArrayBlockingQueue has a fixed capacity.Emcee
This answer is missing the point: BlockingQueue implementations are thread-safe. sourceHardboard
S
1

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

BlockingQueue, Queue Implementations

Sienkiewicz answered 17/3, 2013 at 12:45 Comment(0)
R
-2

How about SynchronousQueue? Refer: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/SynchronousQueue.html

Reginareginald answered 17/3, 2013 at 12:43 Comment(1)
that's a very specialized implementation, not really general use.Certes

© 2022 - 2024 — McMap. All rights reserved.