Uploaded image for project: 'Flume'
  1. Flume
  2. FLUME-3229

CounterGroup#counters use ConcurrentHashMap instead of HashMap

    XMLWordPrintableJSON

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)

      { 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);
      }

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              ykgarfield yangkun
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated: