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 java.util.concurrent.atomic.*; |
14 |
|
volatile int x = 0; |
15 |
|
int w; |
16 |
|
long z; |
17 |
< |
public static void main(String[] args){ |
17 |
> |
public static void main(String[] args) { |
18 |
|
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
25 |
|
* Construction with non-existent field throws RuntimeException |
26 |
|
*/ |
27 |
|
public void testConstructor() { |
28 |
< |
try{ |
29 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
28 |
> |
try { |
29 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
30 |
|
a = AtomicIntegerFieldUpdater.newUpdater |
31 |
|
(AtomicIntegerFieldUpdaterTest.class, "y"); |
32 |
|
shouldThrow(); |
38 |
|
* construction with field not of given type throws RuntimeException |
39 |
|
*/ |
40 |
|
public void testConstructor2() { |
41 |
< |
try{ |
42 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
41 |
> |
try { |
42 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
43 |
|
a = AtomicIntegerFieldUpdater.newUpdater |
44 |
|
(AtomicIntegerFieldUpdaterTest.class, "z"); |
45 |
|
shouldThrow(); |
51 |
|
* construction with non-volatile field throws RuntimeException |
52 |
|
*/ |
53 |
|
public void testConstructor3() { |
54 |
< |
try{ |
55 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
54 |
> |
try { |
55 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
56 |
|
a = AtomicIntegerFieldUpdater.newUpdater |
57 |
|
(AtomicIntegerFieldUpdaterTest.class, "w"); |
58 |
|
shouldThrow(); |
76 |
|
assertEquals(2,a.get(this)); |
77 |
|
a.set(this,-3); |
78 |
|
assertEquals(-3,a.get(this)); |
79 |
< |
|
79 |
> |
|
80 |
|
} |
81 |
|
|
82 |
|
/** |
95 |
|
assertEquals(2,a.get(this)); |
96 |
|
a.lazySet(this,-3); |
97 |
|
assertEquals(-3,a.get(this)); |
98 |
< |
|
98 |
> |
|
99 |
|
} |
100 |
|
|
101 |
|
/** |
123 |
|
* compareAndSet in one thread enables another waiting for value |
124 |
|
* to succeed |
125 |
|
*/ |
126 |
< |
public void testCompareAndSetInMultipleThreads() { |
126 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
127 |
|
x = 1; |
128 |
|
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a; |
129 |
|
try { |
132 |
|
return; |
133 |
|
} |
134 |
|
|
135 |
< |
Thread t = new Thread(new Runnable() { |
136 |
< |
public void run() { |
137 |
< |
while(!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield(); |
138 |
< |
}}); |
139 |
< |
try { |
140 |
< |
t.start(); |
141 |
< |
assertTrue(a.compareAndSet(this, 1, 2)); |
142 |
< |
t.join(LONG_DELAY_MS); |
143 |
< |
assertFalse(t.isAlive()); |
144 |
< |
assertEquals(a.get(this), 3); |
145 |
< |
} |
146 |
< |
catch(Exception e) { |
147 |
< |
unexpectedException(); |
148 |
< |
} |
135 |
> |
Thread t = new Thread(new CheckedRunnable() { |
136 |
> |
public void realRun() { |
137 |
> |
while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) |
138 |
> |
Thread.yield(); |
139 |
> |
}}); |
140 |
> |
|
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 |
|
|
148 |
|
/** |
149 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
150 |
< |
* to expected |
150 |
> |
* to expected |
151 |
|
*/ |
152 |
|
public void testWeakCompareAndSet() { |
153 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
157 |
|
return; |
158 |
|
} |
159 |
|
x = 1; |
160 |
< |
while(!a.weakCompareAndSet(this,1,2)); |
161 |
< |
while(!a.weakCompareAndSet(this,2,-4)); |
160 |
> |
while (!a.weakCompareAndSet(this,1,2)); |
161 |
> |
while (!a.weakCompareAndSet(this,2,-4)); |
162 |
|
assertEquals(-4,a.get(this)); |
163 |
< |
while(!a.weakCompareAndSet(this,-4,7)); |
163 |
> |
while (!a.weakCompareAndSet(this,-4,7)); |
164 |
|
assertEquals(7,a.get(this)); |
165 |
|
} |
166 |
|
|