6 |
|
|
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; |
26 |
|
/** Relies on default serialization */ |
27 |
|
private static final long serialVersionUID = 7249369246863182397L; |
28 |
|
|
30 |
– |
/** Concurrency parameter for map -- we assume high contention */ |
31 |
– |
private static final int MAP_SEGMENTS = |
32 |
– |
Math.max(16, Runtime.getRuntime().availableProcessors()); |
33 |
– |
|
29 |
|
/** The underlying map */ |
30 |
< |
private final ConcurrentHashMap<K, LongAdder> map; |
30 |
> |
private final ConcurrentHashMapV8<K, LongAdder> map; |
31 |
> |
|
32 |
> |
static final class CreateAdder |
33 |
> |
implements ConcurrentHashMapV8.MappingFunction<Object, LongAdder> { |
34 |
> |
public LongAdder map(Object unused) { return new LongAdder(); } |
35 |
> |
} |
36 |
> |
|
37 |
> |
private static final CreateAdder createAdder = new CreateAdder(); |
38 |
|
|
39 |
|
/** |
40 |
|
* Creates a new empty table. |
41 |
|
*/ |
42 |
|
public LongAdderTable() { |
43 |
< |
map = new ConcurrentHashMap<K, LongAdder>(16, 0.75f, MAP_SEGMENTS); |
43 |
> |
map = new ConcurrentHashMapV8<K, LongAdder>(); |
44 |
|
} |
45 |
|
|
46 |
|
/** |
71 |
|
*/ |
72 |
|
public void add(K key, long x) { |
73 |
|
LongAdder a = map.get(key); |
74 |
< |
if (a == null) { |
75 |
< |
LongAdder r = new LongAdder(); |
74 |
< |
if ((a = map.putIfAbsent(key, r)) == null) |
75 |
< |
a = r; |
76 |
< |
} |
74 |
> |
if (a == null) |
75 |
> |
a = map.computeIfAbsent(key, createAdder); |
76 |
|
a.add(x); |
77 |
|
} |
78 |
|
|