• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concurrent Collections vs Unsynchronized Collections

 
Matt Elliott
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic