This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Concurrent Collections vs Unsynchronized Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply 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: 3028
    
    4

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Concurrent Collections vs Unsynchronized Collections
 
Similar Threads
which Collection Class is synchronied?
java.util Decisions
using collections in session object
strange error
What is synchronizedCollection(..)..