user1866818
user1866818

Reputation:

Collection in order and synchronized-Java

I just want to use a collection that gives me the items by the order I added them in the past, and also is synchronized. Of course I want to add-remove items dynamically. I am so confused by the many collections Java has. If anyone knows the answer please tell me.

Upvotes: 0

Views: 200

Answers (2)

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

What about ConcurrentLinkedDeque? http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html

It is fully synchronized and offers the required FIFO order of a queue.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500215

Well, Vector has the two properties you asked for... but to be honest, I don't find that the collection being synchronized is actually that useful. I find it's rarely useful to just make single operations synchronized - instead, you want to synchronize over a whole set of operations, such as iterating over the whole collection.

But if for whatever reason you really, really want a collection where each operation is individually synchronized, Vector will do the trick.

Or you could use ArrayList and create a synchronized wrapper using Collections.synchronizedList.

Upvotes: 5

Related Questions