wood burning stoves*
The moose likes Performance and the fly likes HashMap - in-memory caching - multi-threaded environment ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "HashMap - in-memory caching - multi-threaded environment ?" Watch "HashMap - in-memory caching - multi-threaded environment ?" New topic
Author

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

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1368
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

Joined: Jun 26, 2002
Posts: 852
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


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Poobhathy Kannan
Ranch Hand

Joined: May 26, 2004
Posts: 94
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.


http://learnertobeginner.blogspot.com/
Frank Pavageau
Greenhorn

Joined: Jul 20, 2010
Posts: 10
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

Joined: Apr 08, 2004
Posts: 1368
Can i use eCache or Oracle coherent caching for storing database values ?
Frank Pavageau
Greenhorn

Joined: Jul 20, 2010
Posts: 10
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.
 
Consider Paul's rocket mass heater.
 
subject: HashMap - in-memory caching - multi-threaded environment ?
 
Similar Threads
When to use synchronized
Map inerface in a project
Comparision of Hashtable, Synchronized HashMap and ConcurrentHashMap
Restricting multiple logins
Sort Arraylist by passing runtime parameter