Details
-
Improvement
-
Status: Open
-
Minor
-
Resolution: Unresolved
-
None
-
None
-
None
-
None
Description
Currently, The CounterGroup#counters use HashMap, so this class all methods use the synchronized, if not use HashMap, change to use ConcurrentHashMap , some methods, for example: get(String)、incrementAndGet(String)、addAndGet(String, Long)、set(String, Long)、getCounter(String), etc. can remove the synchronized, because they are thread safe.This can take a little performance improvement.
// use ConcurrentHashMap instead of HashMap
private ConcurrentHashMap<String, AtomicLong> counters;
public CounterGroup()
{ counters = new ConcurrentHashMap<>(); }// no need use synchronized
public Long get(String name)
// no need use synchronized
public Long incrementAndGet(String name)
// no need use synchronized
public AtomicLong getCounter(String name) {
if (!counters.containsKey(name))
return counters.get(name);
}
Attachments
Issue Links
- links to