Parallel Read Only HashMap

I am writing a web service that relies heavily on one large card, which is fully updated once per hour. The rest of the time, many threads read the table at the same time.

My question is: what is the most effective design for implementing such a Map?

The card can be quite large (100 - 500 MB). Read-only access, with the exception of once an hour, when the entire card is replaced.

I thought to just use the Java HashMap and possibly use reflection to set the field to final between updates if it improves performance, but I don't know how to make the JVM optimize for many simultaneous reads.

+4
source share
4 answers

Since the map does not update when used, use HashMapthat gives excellent O (1) search performance (with sacrificing thread safety).

When the time comes for an update, create a new map and replace the links.

Think of using AtomicReferenceto make the swap stream safe:

private final AtomicReference<Map<K, V>> mapRef = new AtomicReference<>();

For use:

mapRef.get().get(key);

To initialize or replace a new card:

Map<K, V> newMap = new HashMap<>();
// populate map
mapRef.set(newMap); // all threads will see this change
+15
source

In your scenario, until you have proven that the HashMap Java standard is not fast enough, I think you may need to worry about garbage collection if you stop the world here and there might be a problem.

, HashMap ( ) , , .

, , HashMaps, @Bohemian .

+2

Go to ConcurrentHashMap . This allows simultaneous read access without sacrificing performance.

+1
source

Use this.

public class Model {
  private Map<?, ?> values;
  private ReadWriteLock lock = new ReentrantReadWriteLock();

  public ? getValue(? key) {
    lock.readLock().lock();
    ? rv = values.get(key);
    lock.readLock().unlock();
    return rv;
  }

  public void update(Map<?, ?> values) {
    lock.writeLock().lock();
    rv = values;
    //OR rv.putAll(values)
    lock.writeLock().unlock();
  }
}
-1
source

Source: https://habr.com/ru/post/1525708/


All Articles