author | shade |
Fri, 25 Mar 2016 15:35:43 +0300 | |
changeset 36936 | bfcdf736a998 |
parent 33674 | 566777f73c32 |
child 39725 | 9548f8d846e9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
5506
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent.atomic; |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
37 |
|
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
38 |
import java.util.function.LongBinaryOperator; |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
39 |
import java.util.function.LongUnaryOperator; |
2 | 40 |
|
41 |
/** |
|
42 |
* A {@code long} value that may be updated atomically. See the |
|
43 |
* {@link java.util.concurrent.atomic} package specification for |
|
44 |
* description of the properties of atomic variables. An |
|
45 |
* {@code AtomicLong} is used in applications such as atomically |
|
46 |
* incremented sequence numbers, and cannot be used as a replacement |
|
47 |
* for a {@link java.lang.Long}. However, this class does extend |
|
48 |
* {@code Number} to allow uniform access by tools and utilities that |
|
49 |
* deal with numerically-based classes. |
|
50 |
* |
|
51 |
* @since 1.5 |
|
52 |
* @author Doug Lea |
|
53 |
*/ |
|
54 |
public class AtomicLong extends Number implements java.io.Serializable { |
|
55 |
private static final long serialVersionUID = 1927816293512124184L; |
|
56 |
||
33674
566777f73c32
8140606: Update library code to use internal Unsafe
chegar
parents:
32838
diff
changeset
|
57 |
private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe(); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
58 |
private static final long VALUE; |
2 | 59 |
|
60 |
/** |
|
61 |
* Records whether the underlying JVM supports lockless |
|
62 |
* compareAndSwap for longs. While the Unsafe.compareAndSwapLong |
|
63 |
* method works in either case, some constructions should be |
|
64 |
* handled at Java level to avoid locking user-visible locks. |
|
65 |
*/ |
|
66 |
static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8(); |
|
67 |
||
68 |
/** |
|
69 |
* Returns whether underlying JVM supports lockless CompareAndSet |
|
70 |
* for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS. |
|
71 |
*/ |
|
72 |
private static native boolean VMSupportsCS8(); |
|
73 |
||
74 |
static { |
|
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
10067
diff
changeset
|
75 |
try { |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
76 |
VALUE = U.objectFieldOffset |
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
10067
diff
changeset
|
77 |
(AtomicLong.class.getDeclaredField("value")); |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
78 |
} catch (ReflectiveOperationException e) { |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
79 |
throw new Error(e); |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
80 |
} |
2 | 81 |
} |
82 |
||
83 |
private volatile long value; |
|
84 |
||
85 |
/** |
|
86 |
* Creates a new AtomicLong with the given initial value. |
|
87 |
* |
|
88 |
* @param initialValue the initial value |
|
89 |
*/ |
|
90 |
public AtomicLong(long initialValue) { |
|
91 |
value = initialValue; |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Creates a new AtomicLong with initial value {@code 0}. |
|
96 |
*/ |
|
97 |
public AtomicLong() { |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* Gets the current value. |
|
102 |
* |
|
103 |
* @return the current value |
|
104 |
*/ |
|
105 |
public final long get() { |
|
106 |
return value; |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Sets to the given value. |
|
111 |
* |
|
112 |
* @param newValue the new value |
|
113 |
*/ |
|
114 |
public final void set(long newValue) { |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
115 |
// Use putLongVolatile instead of ordinary volatile store when |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
116 |
// using compareAndSwapLong, for sake of some 32bit systems. |
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
117 |
U.putLongVolatile(this, VALUE, newValue); |
2 | 118 |
} |
119 |
||
120 |
/** |
|
121 |
* Eventually sets to the given value. |
|
122 |
* |
|
123 |
* @param newValue the new value |
|
124 |
* @since 1.6 |
|
125 |
*/ |
|
126 |
public final void lazySet(long newValue) { |
|
36936
bfcdf736a998
8152698: Remove obsolete Unsafe.putOrdered{X} methods, usages, runtime and compiler support
shade
parents:
33674
diff
changeset
|
127 |
U.putLongRelease(this, VALUE, newValue); |
2 | 128 |
} |
129 |
||
130 |
/** |
|
131 |
* Atomically sets to the given value and returns the old value. |
|
132 |
* |
|
133 |
* @param newValue the new value |
|
134 |
* @return the previous value |
|
135 |
*/ |
|
136 |
public final long getAndSet(long newValue) { |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
137 |
return U.getAndSetLong(this, VALUE, newValue); |
2 | 138 |
} |
139 |
||
140 |
/** |
|
141 |
* Atomically sets the value to the given updated value |
|
142 |
* if the current value {@code ==} the expected value. |
|
143 |
* |
|
144 |
* @param expect the expected value |
|
145 |
* @param update the new value |
|
18576 | 146 |
* @return {@code true} if successful. False return indicates that |
2 | 147 |
* the actual value was not equal to the expected value. |
148 |
*/ |
|
149 |
public final boolean compareAndSet(long expect, long update) { |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
150 |
return U.compareAndSwapLong(this, VALUE, expect, update); |
2 | 151 |
} |
152 |
||
153 |
/** |
|
154 |
* Atomically sets the value to the given updated value |
|
155 |
* if the current value {@code ==} the expected value. |
|
156 |
* |
|
18576 | 157 |
* <p><a href="package-summary.html#weakCompareAndSet">May fail |
158 |
* spuriously and does not provide ordering guarantees</a>, so is |
|
159 |
* only rarely an appropriate alternative to {@code compareAndSet}. |
|
2 | 160 |
* |
161 |
* @param expect the expected value |
|
162 |
* @param update the new value |
|
18576 | 163 |
* @return {@code true} if successful |
2 | 164 |
*/ |
165 |
public final boolean weakCompareAndSet(long expect, long update) { |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
166 |
return U.compareAndSwapLong(this, VALUE, expect, update); |
2 | 167 |
} |
168 |
||
169 |
/** |
|
170 |
* Atomically increments by one the current value. |
|
171 |
* |
|
172 |
* @return the previous value |
|
173 |
*/ |
|
174 |
public final long getAndIncrement() { |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
175 |
return U.getAndAddLong(this, VALUE, 1L); |
2 | 176 |
} |
177 |
||
178 |
/** |
|
179 |
* Atomically decrements by one the current value. |
|
180 |
* |
|
181 |
* @return the previous value |
|
182 |
*/ |
|
183 |
public final long getAndDecrement() { |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
184 |
return U.getAndAddLong(this, VALUE, -1L); |
2 | 185 |
} |
186 |
||
187 |
/** |
|
188 |
* Atomically adds the given value to the current value. |
|
189 |
* |
|
190 |
* @param delta the value to add |
|
191 |
* @return the previous value |
|
192 |
*/ |
|
193 |
public final long getAndAdd(long delta) { |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
194 |
return U.getAndAddLong(this, VALUE, delta); |
2 | 195 |
} |
196 |
||
197 |
/** |
|
198 |
* Atomically increments by one the current value. |
|
199 |
* |
|
200 |
* @return the updated value |
|
201 |
*/ |
|
202 |
public final long incrementAndGet() { |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
203 |
return U.getAndAddLong(this, VALUE, 1L) + 1L; |
2 | 204 |
} |
205 |
||
206 |
/** |
|
207 |
* Atomically decrements by one the current value. |
|
208 |
* |
|
209 |
* @return the updated value |
|
210 |
*/ |
|
211 |
public final long decrementAndGet() { |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
212 |
return U.getAndAddLong(this, VALUE, -1L) - 1L; |
2 | 213 |
} |
214 |
||
215 |
/** |
|
216 |
* Atomically adds the given value to the current value. |
|
217 |
* |
|
218 |
* @param delta the value to add |
|
219 |
* @return the updated value |
|
220 |
*/ |
|
221 |
public final long addAndGet(long delta) { |
|
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
222 |
return U.getAndAddLong(this, VALUE, delta) + delta; |
2 | 223 |
} |
224 |
||
225 |
/** |
|
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
226 |
* Atomically updates the current value with the results of |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
227 |
* applying the given function, returning the previous value. The |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
228 |
* function should be side-effect-free, since it may be re-applied |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
229 |
* when attempted updates fail due to contention among threads. |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
230 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
231 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
232 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
233 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
234 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
235 |
public final long getAndUpdate(LongUnaryOperator updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
236 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
237 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
238 |
prev = get(); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
239 |
next = updateFunction.applyAsLong(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
240 |
} while (!compareAndSet(prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
241 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
242 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
243 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
244 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
245 |
* Atomically updates the current value with the results of |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
246 |
* applying the given function, returning the updated value. The |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
247 |
* function should be side-effect-free, since it may be re-applied |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
248 |
* when attempted updates fail due to contention among threads. |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
249 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
250 |
* @param updateFunction a side-effect-free function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
251 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
252 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
253 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
254 |
public final long updateAndGet(LongUnaryOperator updateFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
255 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
256 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
257 |
prev = get(); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
258 |
next = updateFunction.applyAsLong(prev); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
259 |
} while (!compareAndSet(prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
260 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
261 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
262 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
263 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
264 |
* Atomically updates the current value with the results of |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
265 |
* applying the given function to the current and given values, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
266 |
* returning the previous value. The function should be |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
267 |
* side-effect-free, since it may be re-applied when attempted |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
268 |
* updates fail due to contention among threads. The function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
269 |
* is applied with the current value as its first argument, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
270 |
* and the given update as the second argument. |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
271 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
272 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
273 |
* @param accumulatorFunction a side-effect-free function of two arguments |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
274 |
* @return the previous value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
275 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
276 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
277 |
public final long getAndAccumulate(long x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
278 |
LongBinaryOperator accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
279 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
280 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
281 |
prev = get(); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
282 |
next = accumulatorFunction.applyAsLong(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
283 |
} while (!compareAndSet(prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
284 |
return prev; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
285 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
286 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
287 |
/** |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
288 |
* Atomically updates the current value with the results of |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
289 |
* applying the given function to the current and given values, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
290 |
* returning the updated value. The function should be |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
291 |
* side-effect-free, since it may be re-applied when attempted |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
292 |
* updates fail due to contention among threads. The function |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
293 |
* is applied with the current value as its first argument, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
294 |
* and the given update as the second argument. |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
295 |
* |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
296 |
* @param x the update value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
297 |
* @param accumulatorFunction a side-effect-free function of two arguments |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
298 |
* @return the updated value |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
299 |
* @since 1.8 |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
300 |
*/ |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
301 |
public final long accumulateAndGet(long x, |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
302 |
LongBinaryOperator accumulatorFunction) { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
303 |
long prev, next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
304 |
do { |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
305 |
prev = get(); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15267
diff
changeset
|
306 |
next = accumulatorFunction.applyAsLong(prev, x); |
15267
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
307 |
} while (!compareAndSet(prev, next)); |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
308 |
return next; |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
309 |
} |
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
310 |
|
c884f548a25f
8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents:
15020
diff
changeset
|
311 |
/** |
2 | 312 |
* Returns the String representation of the current value. |
15020
50394fa17c1b
8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents:
14325
diff
changeset
|
313 |
* @return the String representation of the current value |
2 | 314 |
*/ |
315 |
public String toString() { |
|
316 |
return Long.toString(get()); |
|
317 |
} |
|
318 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
319 |
/** |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
320 |
* Returns the value of this {@code AtomicLong} as an {@code int} |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
321 |
* after a narrowing primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
322 |
* @jls 5.1.3 Narrowing Primitive Conversions |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
323 |
*/ |
2 | 324 |
public int intValue() { |
325 |
return (int)get(); |
|
326 |
} |
|
327 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
328 |
/** |
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
10067
diff
changeset
|
329 |
* Returns the value of this {@code AtomicLong} as a {@code long}. |
32838
caeef2c79243
8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents:
25859
diff
changeset
|
330 |
* Equivalent to {@link #get()}. |
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
331 |
*/ |
2 | 332 |
public long longValue() { |
333 |
return get(); |
|
334 |
} |
|
335 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
336 |
/** |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
337 |
* Returns the value of this {@code AtomicLong} as a {@code float} |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
338 |
* after a widening primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
339 |
* @jls 5.1.2 Widening Primitive Conversions |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
340 |
*/ |
2 | 341 |
public float floatValue() { |
342 |
return (float)get(); |
|
343 |
} |
|
344 |
||
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
345 |
/** |
11134
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
10067
diff
changeset
|
346 |
* Returns the value of this {@code AtomicLong} as a {@code double} |
9ff7640994bf
7117360: Warnings in java.util.concurrent.atomic package
dl
parents:
10067
diff
changeset
|
347 |
* after a widening primitive conversion. |
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
348 |
* @jls 5.1.2 Widening Primitive Conversions |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9242
diff
changeset
|
349 |
*/ |
2 | 350 |
public double doubleValue() { |
351 |
return (double)get(); |
|
352 |
} |
|
353 |
||
354 |
} |