Using Distributed ConcurrentHashMap

Posted: March 21st, 2016 | Author: | Filed under: Distributed Caching

Before, if you wanted to make atomic modifications to the distributed hash table provided by Cacheonix, you could use the distributed locks, such as in the example below:

      Cacheonix cacheonix = Cacheonix.getInstance();
      ReadWriteLock readWriteLock = cacheonix.getCluster().getReadWriteLock();
      Map map = cacheonix.getCache(mapName);
      Lock lock = readWriteLock.writeLock();
      try {
         if (!map.containsKey(key)) {
            return map.put(key, value);
         } else {
            return map.get(key);
      } finally {

That approach worked but it required multiple network round trips to get things done. The latest Cacheonix release adds a distributed implementation of ConcurrentHashMap. Now you can modify distributed hash maps atomically, with minimal latency, by using the new JDK-compatible distributed ConcurrentMap API:

     Cacheonix cacheonix = Cacheonix.getInstance();
     java.util.concurrent.ConcurrentMap concurrentMap = cacheonix.getCache(mapName);
     return concurrentMap.putIfAbsent(key, value);

Distributed ConcurrentMap by Cacheonix supports all four methods of ConcurrentMap:

  • V putIfAbsent(K key, V value);
  • boolean remove(Object key, Object value);
  • boolean replace(K key, V oldValue, V newValue);
  • V replace(K key, V value);

Hope this helps.


Slava Imeshev
Cacheonix Open Source Java Cache

(No) Comments: Post a response