• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

synchronization on a key in a collection, is this possible

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.





 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ... ...
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may use java.util.concurrent.ConcurrentHashMap class instead.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic