Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap - in-memory caching - multi-threaded environment ?

 
kri shan
Ranch Hand
Posts: 1473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am planning to use HashMap my in-memory caching in my application. Our application server is clustered. Whether this HashMap will work in multi-threaded environment ? If not, can i change my implemnetation to HashTable for multi-threading support. How is the performance using HashMap/HashTable in a clusteered environment ?
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are talking about 2 separate issues here. Being thread-safe is when you code needs to be changed by more than one process simultaneously in one JVM i.e. in a multi-threaded environment. Having your objects safe in a clustered environment is a different matter. I haven't looked at this in a while, so I don't know if built-in jvm support exists now or not. I would suggest not rolling your own and looking for some form of open source or commercial object cache that will do what you want. I googled 'distributed object cache'. This should get you started.

http://java-source.net/open-source/cache-solutions
http://jakarta.apache.org/jcs/
http://www.manageability.org/blog/stuff/distributed-cache-java
 
Poobhathy Kannan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kri Shan, By stating in-memory caching, do you mean having a static instance variable of HashMap? if so, you might need to synchronize the part accessing/updating this Map. Or you can try using http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/util/Collections.html#synchronizedMap(java.util.Map)

HashTable is synchronized and can be used in multi-thread environment but it is kind of getting old.

 
Frank Pavageau
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could just use java.util.concurrent.ConcurrentHashMap and be done with it (for the concurrent part, not the clustered part). Any modern clustered Map will probably implement ConcurrentMap anyway, so you'll just have to change the implementation.
 
kri shan
Ranch Hand
Posts: 1473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can i use eCache or Oracle coherent caching for storing database values ?
 
Frank Pavageau
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ehcache is commonly used to cache the results of database queries: for example, it's frequently used as the second level cache for Hibernate. It's easily plugable, has lots of configuration options (region sizes, overflow to disk, time to live, time to idle, eviction strategy, etc.), can be extended (self-populating caches) and clustered with Terracotta.

As far as I know, Oracle Coherence can be used too, but in that case I hope you wouldn't use it only as a local cache, since Ehcache easily provides the same service for free.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic