You do not need to use synchronization with ConcurretnHashMap, except in rare cases when you need to perform several operations atomically.
To just get the size, you can call it without synchronization.
To clarify when I will use synchronization with ConcurrentHashMap ...
Say that you have an expensive property that you want to create upon request. You want to read at the same time, but also want the values ββto be created only once.
public ExpensiveObject get(String key) { return map.get(key);
Note. This requires that all records be synchronized, but reading can still be parallel.
Peter Lawrey
source share