wood burning stoves*
The moose likes Threads and Synchronization and the fly likes Concurrent Collections vs Unsynchronized Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Concurrent Collections vs Unsynchronized Collections" Watch "Concurrent Collections vs Unsynchronized Collections" New topic

Concurrent Collections vs Unsynchronized Collections

Matt Elliott

Joined: Feb 21, 2009
Posts: 14
If i'm only doing read operations to a collection using multiple threads,
is it more efficient to use the concurrent collection or the standard unsynchronized ones?
Assume the data structure was created and filled using only a single thread.
Cristian Vrabie
Ranch Hand

Joined: Jul 09, 2008
Posts: 71
If you guarantee that no write operation occurs after you start reading (basically the collection and (important) the elements of the collection are immutable), you should definitely use the non synchronized collections which are much faster.
Steve Luke

Joined: Jan 28, 2003
Posts: 4164

And just to help make doubly extra sure (and to notify any other coders who may work on your application) that the collection should not be modified once filled you should probably wrap the collection in an 'unmodifiable' version. See The Collections class for methods.

Cristian Vrabie
Ranch Hand

Joined: Jul 09, 2008
Posts: 71
Steve is right. All developers should be aware of the thread safeness of different classes. In order to achieve this you could use some standard annotations like @ThreadSafe, @Immutable, @GuardedBy. If you can find a good way of making these annotations visible in JavaDocs would be even better.
I agree. Here's the link: http://aspose.com/file-tools
subject: Concurrent Collections vs Unsynchronized Collections
Similar Threads
which Collection Class is synchronied?
What is synchronizedCollection(..)..
java.util Decisions
strange error
using collections in session object