forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleAccumulator.java
343 lines (295 loc) · 13 KB
/
DoubleAccumulator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
package java.util.concurrent.atomic;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.function.DoubleBinaryOperator;
import static java.lang.Double.doubleToRawLongBits;
import static java.lang.Double.longBitsToDouble;
/**
* One or more variables that together maintain a running {@code double}
* value updated using a supplied function. When updates (method
* {@link #accumulate}) are contended across threads, the set of variables
* may grow dynamically to reduce contention. Method {@link #get}
* (or, equivalently, {@link #doubleValue}) returns the current value
* across the variables maintaining updates.
*
* <p>This class is usually preferable to alternatives when multiple
* threads update a common value that is used for purposes such as
* summary statistics that are frequently updated but less frequently
* read.
*
* <p>The supplied accumulator function should be side-effect-free,
* since it may be re-applied when attempted updates fail due to
* contention among threads. For predictable results, the accumulator
* function should be commutative and associative within the floating
* point tolerance required in usage contexts. The function is applied
* with an existing value (or identity) as one argument, and a given
* update as the other argument. For example, to maintain a running
* maximum value, you could supply {@code Double::max} along with
* {@code Double.NEGATIVE_INFINITY} as the identity. The order of
* accumulation within or across threads is not guaranteed. Thus, this
* class may not be applicable if numerical stability is required,
* especially when combining values of substantially different orders
* of magnitude.
*
* <p>Class {@link DoubleAdder} provides analogs of the functionality
* of this class for the common special case of maintaining sums. The
* call {@code new DoubleAdder()} is equivalent to {@code new
* DoubleAccumulator((x, y) -> x + y, 0.0)}.
*
* <p>This class extends {@link Number}, but does <em>not</em> define
* methods such as {@code equals}, {@code hashCode} and {@code
* compareTo} because instances are expected to be mutated, and so are
* not useful as collection keys.
*
* @author Doug Lea
* @since 1.8
*/
// 对DoubleAdder的升级操作,通过内置的function属性,支持对目标值进行更多的操作
public class DoubleAccumulator extends Striped64 implements Serializable {
private static final long serialVersionUID = 7249069246863182397L;
// 原始值
private final long identity;
// 支持对目标值进行更多操作
private final DoubleBinaryOperator function;
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Creates a new instance using the given accumulator function
* and identity element.
*
* @param accumulatorFunction a side-effect-free function of two arguments
* @param identity identity (initial value) for the accumulator function
*/
public DoubleAccumulator(DoubleBinaryOperator accumulatorFunction, double identity) {
this.function = accumulatorFunction;
base = this.identity = doubleToRawLongBits(identity);
}
/*▲ 构造器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 更新值 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Updates with the given value.
*
* @param x the value
*/
// 结合内部的function属性来更新当前值
public void accumulate(double x) {
Cell[] cs;
long b, v, r;
int m;
Cell c;
// 执行流程参考DoubleAdder中的add(double)
if((cs = cells) != null
|| ((r = doubleToRawLongBits(function.applyAsDouble(longBitsToDouble(b = base), x))) != b && !casBase(b, r))) {
boolean uncontended = true;
if(cs == null
|| (m = cs.length - 1)<0
|| (c = cs[getProbe() & m]) == null
|| !(uncontended = ((r = doubleToRawLongBits(function.applyAsDouble(longBitsToDouble(v = c.value), x))) == v) || c.cas(v, r))) {
doubleAccumulate(x, function, uncontended);
}
}
}
/*▲ 更新值 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 获取值 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns the current value. The returned value is <em>NOT</em>
* an atomic snapshot; invocation in the absence of concurrent
* updates returns an accurate result, but concurrent updates that
* occur while the value is being calculated might not be
* incorporated.
*
* @return the current value
*/
public double get() {
Cell[] cs = cells;
double result = longBitsToDouble(base);
if(cs != null) {
for(Cell c : cs)
if(c != null)
result = function.applyAsDouble(result, longBitsToDouble(c.value));
}
return result;
}
/**
* Returns the {@linkplain #get current value} as an {@code int}
* after a narrowing primitive conversion.
*/
public int intValue() {
return (int) get();
}
/**
* Returns the {@linkplain #get current value} as a {@code long}
* after a narrowing primitive conversion.
*/
public long longValue() {
return (long) get();
}
/**
* Returns the {@linkplain #get current value} as a {@code float}
* after a narrowing primitive conversion.
*/
public float floatValue() {
return (float) get();
}
/**
* Equivalent to {@link #get}.
*
* @return the current value
*/
public double doubleValue() {
return get();
}
/*▲ 获取值 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 重置 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Resets variables maintaining updates to the identity value.
* This method may be a useful alternative to creating a new
* updater, but is only effective if there are no concurrent
* updates. Because this method is intrinsically racy, it should
* only be used when it is known that no threads are concurrently
* updating.
*/
// 重置为原始值
public void reset() {
Cell[] cs = cells;
base = identity;
if(cs != null) {
for(Cell c : cs)
if(c != null)
c.reset(identity);
}
}
/**
* Equivalent in effect to {@link #get} followed by {@link
* #reset}. This method may apply for example during quiescent
* points between multithreaded computations. If there are
* updates concurrent with this method, the returned value is
* <em>not</em> guaranteed to be the final value occurring before
* the reset.
*
* @return the value before reset
*/
// 获取当前值,并重置为原始值
public double getThenReset() {
Cell[] cs = cells;
double result = longBitsToDouble(getAndSetBase(identity));
if(cs != null) {
for(Cell c : cs) {
if(c != null) {
double v = longBitsToDouble(c.getAndSet(identity));
result = function.applyAsDouble(result, v);
}
}
}
return result;
}
/*▲ 重置 ████████████████████████████████████████████████████████████████████████████████┛ */
/**
* Returns the String representation of the current value.
*
* @return the String representation of the current value
*/
public String toString() {
return Double.toString(get());
}
/**
* Returns a
* <a href="../../../../serialized-form.html#java.util.concurrent.atomic.DoubleAccumulator.SerializationProxy">
* SerializationProxy</a>
* representing the state of this instance.
*
* @return a {@link SerializationProxy}
* representing the state of this instance
*/
private Object writeReplace() {
return new SerializationProxy(get(), function, identity);
}
/**
* @param s the stream
*
* @throws java.io.InvalidObjectException always
*/
private void readObject(ObjectInputStream s) throws InvalidObjectException {
throw new InvalidObjectException("Proxy required");
}
/**
* Serialization proxy, used to avoid reference to the non-public
* Striped64 superclass in serialized forms.
*
* @serial include
*/
private static class SerializationProxy implements Serializable {
private static final long serialVersionUID = 7249069246863182397L;
/**
* The current value returned by get().
*
* @serial
*/
private final double value;
/**
* The function used for updates.
*
* @serial
*/
private final DoubleBinaryOperator function;
/**
* The identity value, represented as a long, as converted by
* {@link Double#doubleToRawLongBits}. The original identity
* can be recovered using {@link Double#longBitsToDouble}.
*
* @serial
*/
private final long identity;
SerializationProxy(double value, DoubleBinaryOperator function, long identity) {
this.value = value;
this.function = function;
this.identity = identity;
}
/**
* Returns a {@code DoubleAccumulator} object with initial state
* held by this proxy.
*
* @return a {@code DoubleAccumulator} object with initial state
* held by this proxy
*/
private Object readResolve() {
double d = longBitsToDouble(identity);
DoubleAccumulator a = new DoubleAccumulator(function, d);
a.base = doubleToRawLongBits(value);
return a;
}
}
}