15 |
|
int z; |
16 |
|
long w; |
17 |
|
|
18 |
< |
public static void main(String[] args){ |
18 |
> |
public static void main(String[] args) { |
19 |
|
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
21 |
|
public static Test suite() { |
25 |
|
/** |
26 |
|
* Construction with non-existent field throws RuntimeException |
27 |
|
*/ |
28 |
< |
public void testConstructor(){ |
28 |
> |
public void testConstructor() { |
29 |
|
try { |
30 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
31 |
|
a = AtomicLongFieldUpdater.newUpdater |
38 |
|
/** |
39 |
|
* construction with field not of given type throws RuntimeException |
40 |
|
*/ |
41 |
< |
public void testConstructor2(){ |
41 |
> |
public void testConstructor2() { |
42 |
|
try { |
43 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
44 |
|
a = AtomicLongFieldUpdater.newUpdater |
51 |
|
/** |
52 |
|
* construction with non-volatile field throws RuntimeException |
53 |
|
*/ |
54 |
< |
public void testConstructor3(){ |
54 |
> |
public void testConstructor3() { |
55 |
|
try { |
56 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
57 |
|
a = AtomicLongFieldUpdater.newUpdater |
65 |
|
/** |
66 |
|
* get returns the last value set or assigned |
67 |
|
*/ |
68 |
< |
public void testGetSet(){ |
68 |
> |
public void testGetSet() { |
69 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
70 |
|
try { |
71 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
83 |
|
/** |
84 |
|
* get returns the last value lazySet by same thread |
85 |
|
*/ |
86 |
< |
public void testGetLazySet(){ |
86 |
> |
public void testGetLazySet() { |
87 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
88 |
|
try { |
89 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
102 |
|
/** |
103 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
104 |
|
*/ |
105 |
< |
public void testCompareAndSet(){ |
105 |
> |
public void testCompareAndSet() { |
106 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
107 |
|
try { |
108 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
124 |
|
* compareAndSet in one thread enables another waiting for value |
125 |
|
* to succeed |
126 |
|
*/ |
127 |
< |
public void testCompareAndSetInMultipleThreads() { |
127 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
128 |
|
x = 1; |
129 |
|
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a; |
130 |
|
try { |
133 |
|
return; |
134 |
|
} |
135 |
|
|
136 |
< |
Thread t = new Thread(new Runnable() { |
137 |
< |
public void run() { |
138 |
< |
while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3)) Thread.yield(); |
139 |
< |
}}); |
140 |
< |
try { |
141 |
< |
t.start(); |
142 |
< |
assertTrue(a.compareAndSet(this, 1, 2)); |
143 |
< |
t.join(LONG_DELAY_MS); |
144 |
< |
assertFalse(t.isAlive()); |
145 |
< |
assertEquals(a.get(this), 3); |
146 |
< |
} |
147 |
< |
catch (Exception e) { |
148 |
< |
unexpectedException(); |
149 |
< |
} |
136 |
> |
Thread t = new Thread(new CheckedRunnable() { |
137 |
> |
public void realRun() { |
138 |
> |
while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3)) |
139 |
> |
Thread.yield(); |
140 |
> |
}}); |
141 |
> |
|
142 |
> |
t.start(); |
143 |
> |
assertTrue(a.compareAndSet(this, 1, 2)); |
144 |
> |
t.join(LONG_DELAY_MS); |
145 |
> |
assertFalse(t.isAlive()); |
146 |
> |
assertEquals(a.get(this), 3); |
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
151 |
|
* to expected |
152 |
|
*/ |
153 |
< |
public void testWeakCompareAndSet(){ |
153 |
> |
public void testWeakCompareAndSet() { |
154 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
155 |
|
try { |
156 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
168 |
|
/** |
169 |
|
* getAndSet returns previous value and sets to given value |
170 |
|
*/ |
171 |
< |
public void testGetAndSet(){ |
171 |
> |
public void testGetAndSet() { |
172 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
173 |
|
try { |
174 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
184 |
|
/** |
185 |
|
* getAndAdd returns previous value and adds given value |
186 |
|
*/ |
187 |
< |
public void testGetAndAdd(){ |
187 |
> |
public void testGetAndAdd() { |
188 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
189 |
|
try { |
190 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
201 |
|
/** |
202 |
|
* getAndDecrement returns previous value and decrements |
203 |
|
*/ |
204 |
< |
public void testGetAndDecrement(){ |
204 |
> |
public void testGetAndDecrement() { |
205 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
206 |
|
try { |
207 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
217 |
|
/** |
218 |
|
* getAndIncrement returns previous value and increments |
219 |
|
*/ |
220 |
< |
public void testGetAndIncrement(){ |
220 |
> |
public void testGetAndIncrement() { |
221 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
222 |
|
try { |
223 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
237 |
|
/** |
238 |
|
* addAndGet adds given value to current, and returns current value |
239 |
|
*/ |
240 |
< |
public void testAddAndGet(){ |
240 |
> |
public void testAddAndGet() { |
241 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
242 |
|
try { |
243 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
254 |
|
/** |
255 |
|
* decrementAndGet decrements and returns current value |
256 |
|
*/ |
257 |
< |
public void testDecrementAndGet(){ |
257 |
> |
public void testDecrementAndGet() { |
258 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
259 |
|
try { |
260 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
271 |
|
/** |
272 |
|
* incrementAndGet increments and returns current value |
273 |
|
*/ |
274 |
< |
public void testIncrementAndGet(){ |
274 |
> |
public void testIncrementAndGet() { |
275 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
276 |
|
try { |
277 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |