Wiki

Clone wiki

atlassian-util-concurrent / CopyOnWrite_Maps

The CopyOnWriteMap and CopyOnWriteSortedMap are fully functional ConcurrentMap implementations that are specifically designed for read-mostly applications. For instance, configuration data that is loaded at startup is ideal.

These thread-safe maps delegate to the fastest possible single-threaded Map implementations (eg. HashMap) and have better multi-threaded read performance than ConcurrentHashMap and the like.

You can specify whether the views (entrySet(), keySet(), values()) are writable or not.

Creation is either via the static factory methods:

ConcurrentMap<Long, String> config = CopyOnWriteMap.newHashMap();

or via the Builder:

ConcurrentMap<Long, String> config = CopyOnWriteMap.<String, Long> builder().addAll(initialValues).liveViews().newHashMap();

Notes

Note that null tolerance semantics are defined by the underlying Map implementation (so the HashMap version accepts null keys and values, whereas most map implementations don't).

Note as well that not all Map implementations are suitable candidates to underly a CopyOnWrite container, specifically implementations that modify on read (for instance LinkedHashMap with access ordering or WeakHashMap would cause undefined behaviour.

Source

src/main/java/com/atlassian/util/concurrent/CopyOnWriteMap.java src/main/java/com/atlassian/util/concurrent/CopyOnWriteSortedMap.java

Updated