9

Concurrent Hashmap could solve synchronization issue which is seen in hashmap. So adding and removing would be fast if we are using synchronize key work with hashmap. What about checking hashmap size, if mulitple threads checking concurrentHashMap size? do we still need synchronzation key word: something as follows:

public static synchronized getSize(){
     return aConcurrentHashmap.size();
}  
assylias
  • 321,522
  • 82
  • 660
  • 783
user84592
  • 4,750
  • 11
  • 55
  • 91

5 Answers5

11

concurentHashMap.size() will return the size known at the moment of the call, but it might be a stale value when you use that number because another thread has added / removed items in the meantime.

However the whole purpose of ConcurrentMaps is that you don't need to synchronize it as it is a thread safe collection.

assylias
  • 321,522
  • 82
  • 660
  • 783
2

You don't need to use synchronized with ConcurretnHashMap except in very rare occasions where you need to perform multiple operations atomically.

To just get the size, you can call it without synchronization.


To clarify when I would use synchronization with ConcurrentHashMap...

Say you have an expensive object you want to create on demand. You want concurrent reads, but also want to ensure that values are only created once.

public ExpensiveObject get(String key) {
    return map.get(key); // can work concurrently.
}

public void put(String key, ExepensiveBuilder builder) {
    // cannot use putIfAbsent because it needs the object before checking.
    synchronized(map) {
        if (!map.containsKey(key))
            map.put(key, builder.create());
    }
}

Note: This requires that all writes are synchronized, but reads can still be concurrent.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

You can simply call aConcurrentHashmap.size(). However, you have to bear in mind that by the time you get the answer it might already be obsolete. This would happen if another thread where to concurrently modify the map.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

The designers of ConcurrentHashMap thought of giving weightage to individual operations like : get(), put() and remove() over methods which operate over complete HashMap like isEmpty() or size(). This is done because the changes of these methods getting called (in general) are less than the other individual methods.

A synchronization for size() is not needed here. We can get the size by calling concurentHashMap.size() method. This method may return stale values as other thread might modify the map in the meanwhile. But, this is explicitely assumed to be broken as these operations are deprioritized.

Tom
  • 4,257
  • 6
  • 33
  • 49
Subodh Karwa
  • 2,495
  • 1
  • 15
  • 13
0

ConcorrentHashMap is fail-safe. it won't give any concurrent modification exceptions. it works good for multi threaded operations. The whole implementation of ConcurrentHashMap is same as HashMap but the while retrieving the elements , HashMap locks whole map restricting doing further modifications which gives concurrent modification exception.' But in ConcurrentHashMap, the locking happens at bucket level so the chance of giving concurrent modification exception is not present.

So to answer you question here, checking size of ConcurrentHashMap doesn't help because , it keeps chaining based on the operations or modification code that you write on the map. It has size method which is same from the HashMap.

Phani Kumar
  • 61
  • 2
  • 10