2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
44 |
|
assertEquals(2,ai.get()); |
45 |
|
ai.set(-3); |
46 |
|
assertEquals(-3,ai.get()); |
47 |
< |
|
47 |
> |
|
48 |
|
} |
49 |
+ |
|
50 |
+ |
/** |
51 |
+ |
* get returns the last value lazySet in same thread |
52 |
+ |
*/ |
53 |
+ |
public void testGetLazySet(){ |
54 |
+ |
AtomicLong ai = new AtomicLong(1); |
55 |
+ |
assertEquals(1,ai.get()); |
56 |
+ |
ai.lazySet(2); |
57 |
+ |
assertEquals(2,ai.get()); |
58 |
+ |
ai.lazySet(-3); |
59 |
+ |
assertEquals(-3,ai.get()); |
60 |
+ |
|
61 |
+ |
} |
62 |
+ |
|
63 |
|
/** |
64 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
65 |
|
*/ |
82 |
|
final AtomicLong ai = new AtomicLong(1); |
83 |
|
Thread t = new Thread(new Runnable() { |
84 |
|
public void run() { |
85 |
< |
while(!ai.compareAndSet(2, 3)) Thread.yield(); |
85 |
> |
while (!ai.compareAndSet(2, 3)) Thread.yield(); |
86 |
|
}}); |
87 |
|
try { |
88 |
|
t.start(); |
91 |
|
assertFalse(t.isAlive()); |
92 |
|
assertEquals(ai.get(), 3); |
93 |
|
} |
94 |
< |
catch(Exception e) { |
94 |
> |
catch (Exception e) { |
95 |
|
unexpectedException(); |
96 |
|
} |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
101 |
< |
* to expected |
101 |
> |
* to expected |
102 |
|
*/ |
103 |
|
public void testWeakCompareAndSet(){ |
104 |
|
AtomicLong ai = new AtomicLong(1); |
105 |
< |
while(!ai.weakCompareAndSet(1,2)); |
106 |
< |
while(!ai.weakCompareAndSet(2,-4)); |
105 |
> |
while (!ai.weakCompareAndSet(1,2)); |
106 |
> |
while (!ai.weakCompareAndSet(2,-4)); |
107 |
|
assertEquals(-4,ai.get()); |
108 |
< |
while(!ai.weakCompareAndSet(-4,7)); |
108 |
> |
while (!ai.weakCompareAndSet(-4,7)); |
109 |
|
assertEquals(7,ai.get()); |
110 |
|
} |
111 |
|
|
207 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
208 |
|
AtomicLong r = (AtomicLong) in.readObject(); |
209 |
|
assertEquals(l.get(), r.get()); |
210 |
< |
} catch(Exception e){ |
210 |
> |
} catch (Exception e){ |
211 |
|
unexpectedException(); |
212 |
|
} |
213 |
|
} |
214 |
|
|
215 |
|
/** |
216 |
|
* toString returns current value. |
217 |
< |
*/ |
217 |
> |
*/ |
218 |
|
public void testToString() { |
219 |
|
AtomicLong ai = new AtomicLong(); |
220 |
|
for (long i = -12; i < 6; ++i) { |
225 |
|
|
226 |
|
/** |
227 |
|
* longValue returns current value. |
228 |
< |
*/ |
228 |
> |
*/ |
229 |
|
public void testLongValue() { |
230 |
|
AtomicLong ai = new AtomicLong(); |
231 |
|
for (int i = -12; i < 6; ++i) { |
236 |
|
|
237 |
|
/** |
238 |
|
* floatValue returns current value. |
239 |
< |
*/ |
239 |
> |
*/ |
240 |
|
public void testFloatValue() { |
241 |
|
AtomicLong ai = new AtomicLong(); |
242 |
|
for (int i = -12; i < 6; ++i) { |
247 |
|
|
248 |
|
/** |
249 |
|
* doubleValue returns current value. |
250 |
< |
*/ |
250 |
> |
*/ |
251 |
|
public void testDoubleValue() { |
252 |
|
AtomicLong ai = new AtomicLong(); |
253 |
|
for (int i = -12; i < 6; ++i) { |