aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Concurrent Collections vs Unsynchronized Collections" Watch "Concurrent Collections vs Unsynchronized Collections" New topic
Author

Concurrent Collections vs Unsynchronized Collections

Matt Elliott
Greenhorn

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
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
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.
 
 
subject: Concurrent Collections vs Unsynchronized Collections