yangkun created FLUME-3229:
------------------------------

             Summary: CounterGroup#counters use ConcurrentHashMap instead of 
HashMap
                 Key: FLUME-3229
                 URL: https://issues.apache.org/jira/browse/FLUME-3229
             Project: Flume
          Issue Type: Improvement
            Reporter: yangkun


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) {
  return getCounter(name).get();
}

// no need use synchronized
public Long incrementAndGet(String name) {
  return getCounter(name).incrementAndGet();
}

// no need use synchronized
public AtomicLong getCounter(String name) {
  if (!counters.containsKey(name)) {
    // use putIfAbsent method 
    counters.putIfAbsent(name, new AtomicLong());
  }

  return counters.get(name);
}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@flume.apache.org
For additional commands, e-mail: issues-h...@flume.apache.org

Reply via email to