22 |
|
* <em>NOT</em> an atomic snapshot (concurrent updates may occur while |
23 |
|
* the sum is being calculated), and so cannot be used alone for |
24 |
|
* fine-grained synchronization control. |
25 |
< |
* |
25 |
> |
* |
26 |
|
* <p> This class may be applicable when many threads frequently |
27 |
|
* update a common sum that is used for purposes such as collecting |
28 |
|
* statistics. In this case, performance may be significantly faster |
31 |
|
* only one thread can ever update the sum, performance may be |
32 |
|
* significantly slower than just updating a local variable. |
33 |
|
* |
34 |
< |
* @author Doug Lea |
34 |
> |
* @author Doug Lea |
35 |
|
*/ |
36 |
|
public class StripedAdder implements Serializable { |
37 |
|
private static final long serialVersionUID = 7249069246863182397L; |
52 |
|
* candidate Adder. |
53 |
|
*/ |
54 |
|
|
55 |
< |
/** |
55 |
> |
/** |
56 |
|
* Number of processors, to place a cap on table growth. |
57 |
|
*/ |
58 |
|
static final int NCPU = Runtime.getRuntime().availableProcessors(); |
66 |
|
Adder(long x) { super(x); } |
67 |
|
} |
68 |
|
|
69 |
< |
/** |
69 |
> |
/** |
70 |
|
* Holder for the thread-local hash code. |
71 |
|
*/ |
72 |
|
static final class HashCode { |
79 |
|
*/ |
80 |
|
static final class ThreadHashCode extends ThreadLocal<HashCode> { |
81 |
|
static final Random rng = new Random(); |
82 |
< |
public HashCode initialValue() { |
82 |
> |
public HashCode initialValue() { |
83 |
|
int h = rng.nextInt(); |
84 |
|
return new HashCode((h == 0) ? 1 : h); // ensure nonzero |
85 |
|
} |
107 |
|
/** |
108 |
|
* Marsaglia XorShift for rehashing on collisions |
109 |
|
*/ |
110 |
< |
private static int xorShift(int r) { |
110 |
> |
private static int xorShift(int r) { |
111 |
|
r ^= r << 13; |
112 |
|
r ^= r >>> 17; |
113 |
|
return r ^ (r << 5); |
172 |
|
* Returns an estimate of the current sum. The result is |
173 |
|
* calculated by summing multiple variables, so may not be |
174 |
|
* accurate if updates occur concurrently with this method. |
175 |
< |
* |
175 |
> |
* |
176 |
|
* @return the estimated sum |
177 |
|
*/ |
178 |
|
public long sum() { |