File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronization on a key in a collection, is this possible

 
wayne forrest
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need some help please, or suggestions on how to solve this problem.

I have data stored in a HashMap as below:

key1 ObjectA.
key2 ObjectB.
key3 ObjectC.

There is one producer per key and for example 100 users per key.

Currently I am synchronizing on the entire collection, and I guess this is not optimal, especially since Object(A/B/C) is going to change
very often, up to 5 times a second. And the clients really want the data as fast as possible.

Is it possible to sync on the KEY? is there perhaps a better collection to use?

Or what is the best way to solve this one without moving away from servlets?


thanks.





 
Jim Hoglund
Ranch Hand
Posts: 525
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your data structure choice depends on size and dynamics (add, find, remove, etc.)
that you don't describe. As for synchronization, all users of a referenced item must be
protected from untimely changes to that item. So if user37 reads or updates the object
referenced by Key1, user37 must synchronize on that object, not the entire data structure.
In addition, users that use or change the data structure must synchronize on it.

Jim ... ...
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may use java.util.concurrent.ConcurrentHashMap class instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic