ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/LongAdderTable.java
(Generate patch)

Comparing jsr166/src/jsr166e/LongAdderTable.java (file contents):
Revision 1.1 by dl, Sun Jul 31 14:20:01 2011 UTC vs.
Revision 1.11 by jsr166, Sun Jan 18 20:17:33 2015 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < package jsr166e.extra;
7 > package jsr166e;
8 >
9   import jsr166e.LongAdder;
9 import java.util.concurrent.ConcurrentHashMap;
10   import java.util.Map;
11   import java.util.Set;
12   import java.io.Serializable;
13  
14   /**
15   * A keyed table of adders, that may be useful in computing frequency
16 < * counts and histograms, or may be used a form of multiset.  A {@link
17 < * LongAdder} is associated with each key. Keys are added to the table
18 < * implicitly upon any attempt to update, or may be added explicitly
19 < * using method {@link #install}.
16 > * counts and histograms, or may be used as a form of multiset.  A
17 > * {@link LongAdder} is associated with each key. Keys are added to
18 > * the table implicitly upon any attempt to update, or may be added
19 > * explicitly using method {@link #install}.
20   *
21   * <p><em>jsr166e note: This class is targeted to be placed in
22 < * java.util.concurrent.atomic<em>
22 > * java.util.concurrent.atomic.</em>
23   *
24 + * @since 1.8
25   * @author Doug Lea
26   */
27   public class LongAdderTable<K> implements Serializable {
28      /** Relies on default serialization */
29      private static final long serialVersionUID = 7249369246863182397L;
30  
30    /** Concurrency parameter for map -- we assume high contention */
31    private static final int MAP_SEGMENTS =
32        Math.max(16, Runtime.getRuntime().availableProcessors());
33
31      /** The underlying map */
32 <    private final ConcurrentHashMap<K, LongAdder> map;
32 >    private final ConcurrentHashMapV8<K, LongAdder> map;
33 >
34 >    static final class CreateAdder
35 >        implements ConcurrentHashMapV8.Fun<Object, LongAdder> {
36 >        public LongAdder apply(Object unused) { return new LongAdder(); }
37 >    }
38 >
39 >    private static final CreateAdder createAdder = new CreateAdder();
40  
41      /**
42       * Creates a new empty table.
43       */
44      public LongAdderTable() {
45 <        map = new ConcurrentHashMap<K, LongAdder>(16, 0.75f, MAP_SEGMENTS);
45 >        map = new ConcurrentHashMapV8<K, LongAdder>();
46      }
47  
48      /**
# Line 50 | Line 54 | public class LongAdderTable<K> implement
54       * @return the adder associated with the key
55       */
56      public LongAdder install(K key) {
57 <        LongAdder a = map.get(key);
54 <        if (a == null) {
55 <            LongAdder r = new LongAdder();
56 <            if ((a = map.putIfAbsent(key, r)) == null)
57 <                a = r;
58 <        }
59 <        return a;
57 >        return map.computeIfAbsent(key, createAdder);
58      }
59  
60      /**
# Line 68 | Line 66 | public class LongAdderTable<K> implement
66       * @param x the value to add
67       */
68      public void add(K key, long x) {
69 <        LongAdder a = map.get(key);
72 <        if (a == null) {
73 <            LongAdder r = new LongAdder();
74 <            if ((a = map.putIfAbsent(key, r)) == null)
75 <                a = r;
76 <        }
77 <        a.add(x);
69 >        map.computeIfAbsent(key, createAdder).add(x);
70      }
71  
72      /**
# Line 136 | Line 128 | public class LongAdderTable<K> implement
128      /**
129       * Returns the sum totalled across all keys.
130       *
131 <     * @return the sum totalled across all keys.
131 >     * @return the sum totalled across all keys
132       */
133      public long sumAll() {
134          long sum = 0L;
# Line 156 | Line 148 | public class LongAdderTable<K> implement
148      /**
149       * Totals, then resets, the sums associated with all keys.
150       *
151 <     * @return the sum totalled across all keys.
151 >     * @return the sum totalled across all keys
152       */
153      public long sumThenResetAll() {
154          long sum = 0L;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines