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.7 by dl, Sun Oct 2 22:01:06 2011 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < package jsr166e.extra;
7 > package jsr166e;
8   import jsr166e.LongAdder;
9 import java.util.concurrent.ConcurrentHashMap;
9   import java.util.Map;
10   import java.util.Set;
11   import java.io.Serializable;
# Line 21 | Line 20 | import java.io.Serializable;
20   * <p><em>jsr166e note: This class is targeted to be placed in
21   * java.util.concurrent.atomic<em>
22   *
23 + * @since 1.8
24   * @author Doug Lea
25   */
26   public class LongAdderTable<K> implements Serializable {
27      /** Relies on default serialization */
28      private static final long serialVersionUID = 7249369246863182397L;
29  
30    /** Concurrency parameter for map -- we assume high contention */
31    private static final int MAP_SEGMENTS =
32        Math.max(16, Runtime.getRuntime().availableProcessors());
33
30      /** The underlying map */
31 <    private final ConcurrentHashMap<K, LongAdder> map;
31 >    private final ConcurrentHashMapV8<K, LongAdder> map;
32 >
33 >    static final class CreateAdder
34 >        implements ConcurrentHashMapV8.MappingFunction<Object, LongAdder> {
35 >        public LongAdder map(Object unused) { return new LongAdder(); }
36 >    }
37 >
38 >    private static final CreateAdder createAdder = new CreateAdder();
39  
40      /**
41       * Creates a new empty table.
42       */
43      public LongAdderTable() {
44 <        map = new ConcurrentHashMap<K, LongAdder>(16, 0.75f, MAP_SEGMENTS);
44 >        map = new ConcurrentHashMapV8<K, LongAdder>();
45      }
46  
47      /**
# Line 50 | Line 53 | public class LongAdderTable<K> implement
53       * @return the adder associated with the key
54       */
55      public LongAdder install(K key) {
56 <        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;
56 >        return map.computeIfAbsent(key, createAdder);
57      }
58  
59      /**
# Line 68 | Line 65 | public class LongAdderTable<K> implement
65       * @param x the value to add
66       */
67      public void add(K key, long x) {
68 <        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);
68 >        map.computeIfAbsent(key, createAdder).add(x);
69      }
70  
71      /**
# Line 136 | Line 127 | public class LongAdderTable<K> implement
127      /**
128       * Returns the sum totalled across all keys.
129       *
130 <     * @return the sum totalled across all keys.
130 >     * @return the sum totalled across all keys
131       */
132      public long sumAll() {
133          long sum = 0L;
# Line 156 | Line 147 | public class LongAdderTable<K> implement
147      /**
148       * Totals, then resets, the sums associated with all keys.
149       *
150 <     * @return the sum totalled across all keys.
150 >     * @return the sum totalled across all keys
151       */
152      public long sumThenResetAll() {
153          long sum = 0L;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines