--- jsr166/src/jsr166e/LongAdderTable.java 2011/07/31 14:20:01 1.1 +++ jsr166/src/jsr166e/LongAdderTable.java 2012/08/13 15:52:33 1.9 @@ -4,41 +4,44 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -package jsr166e.extra; +package jsr166e; import jsr166e.LongAdder; -import java.util.concurrent.ConcurrentHashMap; import java.util.Map; import java.util.Set; import java.io.Serializable; /** * A keyed table of adders, that may be useful in computing frequency - * counts and histograms, or may be used a form of multiset. A {@link - * LongAdder} is associated with each key. Keys are added to the table - * implicitly upon any attempt to update, or may be added explicitly - * using method {@link #install}. + * counts and histograms, or may be used as a form of multiset. A + * {@link LongAdder} is associated with each key. Keys are added to + * the table implicitly upon any attempt to update, or may be added + * explicitly using method {@link #install}. * *

jsr166e note: This class is targeted to be placed in * java.util.concurrent.atomic * + * @since 1.8 * @author Doug Lea */ public class LongAdderTable implements Serializable { /** Relies on default serialization */ private static final long serialVersionUID = 7249369246863182397L; - /** Concurrency parameter for map -- we assume high contention */ - private static final int MAP_SEGMENTS = - Math.max(16, Runtime.getRuntime().availableProcessors()); - /** The underlying map */ - private final ConcurrentHashMap map; + private final ConcurrentHashMapV8 map; + + static final class CreateAdder + implements ConcurrentHashMapV8.Fun { + public LongAdder apply(Object unused) { return new LongAdder(); } + } + + private static final CreateAdder createAdder = new CreateAdder(); /** * Creates a new empty table. */ public LongAdderTable() { - map = new ConcurrentHashMap(16, 0.75f, MAP_SEGMENTS); + map = new ConcurrentHashMapV8(); } /** @@ -50,13 +53,7 @@ public class LongAdderTable implement * @return the adder associated with the key */ public LongAdder install(K key) { - LongAdder a = map.get(key); - if (a == null) { - LongAdder r = new LongAdder(); - if ((a = map.putIfAbsent(key, r)) == null) - a = r; - } - return a; + return map.computeIfAbsent(key, createAdder); } /** @@ -68,13 +65,7 @@ public class LongAdderTable implement * @param x the value to add */ public void add(K key, long x) { - LongAdder a = map.get(key); - if (a == null) { - LongAdder r = new LongAdder(); - if ((a = map.putIfAbsent(key, r)) == null) - a = r; - } - a.add(x); + map.computeIfAbsent(key, createAdder).add(x); } /** @@ -136,7 +127,7 @@ public class LongAdderTable implement /** * Returns the sum totalled across all keys. * - * @return the sum totalled across all keys. + * @return the sum totalled across all keys */ public long sumAll() { long sum = 0L; @@ -156,7 +147,7 @@ public class LongAdderTable implement /** * Totals, then resets, the sums associated with all keys. * - * @return the sum totalled across all keys. + * @return the sum totalled across all keys */ public long sumThenResetAll() { long sum = 0L;