1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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. |
7 |
|
*/ |
8 |
|
|
9 |
|
import java.util.concurrent.atomic.*; |
10 |
|
import junit.framework.*; |
11 |
|
import java.util.*; |
12 |
|
|
13 |
< |
public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{ |
13 |
> |
public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { |
14 |
|
volatile Integer x = null; |
15 |
|
Object z; |
16 |
|
Integer w; |
17 |
|
|
18 |
< |
public static void main(String[] args){ |
18 |
> |
public static void main(String[] args) { |
19 |
|
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
20 |
– |
|
21 |
– |
|
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(AtomicReferenceFieldUpdaterTest.class); |
23 |
|
} |
24 |
|
|
25 |
|
/** |
26 |
< |
* |
26 |
> |
* Construction with non-existent field throws RuntimeException |
27 |
|
*/ |
28 |
< |
public void testConstructor(){ |
29 |
< |
try{ |
28 |
> |
public void testConstructor() { |
29 |
> |
try { |
30 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
31 |
|
a = AtomicReferenceFieldUpdater.newUpdater |
32 |
< |
(getClass(), Integer.class, "y"); |
32 |
> |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "y"); |
33 |
|
shouldThrow(); |
34 |
< |
} |
36 |
< |
catch (RuntimeException rt) {} |
34 |
> |
} catch (RuntimeException success) {} |
35 |
|
} |
36 |
|
|
37 |
|
|
38 |
|
/** |
39 |
< |
* |
39 |
> |
* construction with field not of given type throws RuntimeException |
40 |
|
*/ |
41 |
< |
public void testConstructor2(){ |
42 |
< |
try{ |
41 |
> |
public void testConstructor2() { |
42 |
> |
try { |
43 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
44 |
|
a = AtomicReferenceFieldUpdater.newUpdater |
45 |
< |
(getClass(), Integer.class, "z"); |
45 |
> |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "z"); |
46 |
|
shouldThrow(); |
47 |
< |
} |
50 |
< |
catch (RuntimeException rt) {} |
47 |
> |
} catch (RuntimeException success) {} |
48 |
|
} |
49 |
|
|
50 |
|
/** |
51 |
< |
* |
51 |
> |
* Constructor with non-volatile field throws exception |
52 |
|
*/ |
53 |
< |
public void testConstructor3(){ |
54 |
< |
try{ |
53 |
> |
public void testConstructor3() { |
54 |
> |
try { |
55 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
56 |
|
a = AtomicReferenceFieldUpdater.newUpdater |
57 |
< |
(getClass(), Integer.class, "w"); |
57 |
> |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "w"); |
58 |
|
shouldThrow(); |
59 |
< |
} |
63 |
< |
catch (RuntimeException rt) {} |
59 |
> |
} catch (RuntimeException success) {} |
60 |
|
} |
61 |
|
|
62 |
|
/** |
63 |
< |
* |
63 |
> |
* get returns the last value set or assigned |
64 |
|
*/ |
65 |
< |
public void testGetSet(){ |
66 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
65 |
> |
public void testGetSet() { |
66 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
67 |
> |
try { |
68 |
> |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
69 |
> |
} catch (RuntimeException ok) { |
70 |
> |
return; |
71 |
> |
} |
72 |
|
x = one; |
73 |
< |
assertEquals(one,a.get(this)); |
74 |
< |
a.set(this,two); |
75 |
< |
assertEquals(two,a.get(this)); |
76 |
< |
a.set(this,-3); |
77 |
< |
assertEquals(-3,a.get(this)); |
77 |
< |
|
73 |
> |
assertEquals(one,a.get(this)); |
74 |
> |
a.set(this,two); |
75 |
> |
assertEquals(two,a.get(this)); |
76 |
> |
a.set(this,m3); |
77 |
> |
assertEquals(m3,a.get(this)); |
78 |
|
} |
79 |
+ |
|
80 |
|
/** |
81 |
< |
* |
82 |
< |
*/ |
83 |
< |
public void testCompareAndSet(){ |
84 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
81 |
> |
* get returns the last value lazySet by same thread |
82 |
> |
*/ |
83 |
> |
public void testGetLazySet() { |
84 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
85 |
> |
try { |
86 |
> |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
87 |
> |
} catch (RuntimeException ok) { |
88 |
> |
return; |
89 |
> |
} |
90 |
|
x = one; |
91 |
< |
assertTrue(a.compareAndSet(this,one,two)); |
92 |
< |
assertTrue(a.compareAndSet(this,two,m4)); |
93 |
< |
assertEquals(m4,a.get(this)); |
94 |
< |
assertFalse(a.compareAndSet(this,m5,seven)); |
95 |
< |
assertFalse((seven == a.get(this))); |
90 |
< |
assertTrue(a.compareAndSet(this,m4,seven)); |
91 |
< |
assertEquals(seven,a.get(this)); |
91 |
> |
assertEquals(one,a.get(this)); |
92 |
> |
a.lazySet(this,two); |
93 |
> |
assertEquals(two,a.get(this)); |
94 |
> |
a.lazySet(this,m3); |
95 |
> |
assertEquals(m3,a.get(this)); |
96 |
|
} |
97 |
|
|
98 |
|
/** |
99 |
< |
* |
100 |
< |
*/ |
101 |
< |
public void testWeakCompareAndSet(){ |
102 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
99 |
> |
* compareAndSet succeeds in changing value if equal to expected else fails |
100 |
> |
*/ |
101 |
> |
public void testCompareAndSet() { |
102 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
103 |
> |
try { |
104 |
> |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
105 |
> |
} catch (RuntimeException ok) { |
106 |
> |
return; |
107 |
> |
} |
108 |
|
x = one; |
109 |
< |
while(!a.weakCompareAndSet(this,one,two)); |
110 |
< |
while(!a.weakCompareAndSet(this,two,m4)); |
111 |
< |
assertEquals(m4,a.get(this)); |
112 |
< |
while(!a.weakCompareAndSet(this,m4,seven)); |
113 |
< |
assertEquals(seven,a.get(this)); |
109 |
> |
assertTrue(a.compareAndSet(this,one,two)); |
110 |
> |
assertTrue(a.compareAndSet(this,two,m4)); |
111 |
> |
assertEquals(m4,a.get(this)); |
112 |
> |
assertFalse(a.compareAndSet(this,m5,seven)); |
113 |
> |
assertFalse((seven == a.get(this))); |
114 |
> |
assertTrue(a.compareAndSet(this,m4,seven)); |
115 |
> |
assertEquals(seven,a.get(this)); |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
< |
* |
119 |
> |
* compareAndSet in one thread enables another waiting for value |
120 |
> |
* to succeed |
121 |
|
*/ |
122 |
< |
public void testGetAndSet(){ |
123 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
122 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
123 |
> |
x = one; |
124 |
> |
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
125 |
> |
try { |
126 |
> |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
127 |
> |
} catch (RuntimeException ok) { |
128 |
> |
return; |
129 |
> |
} |
130 |
> |
|
131 |
> |
Thread t = new Thread(new CheckedRunnable() { |
132 |
> |
public void realRun() { |
133 |
> |
while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) |
134 |
> |
Thread.yield(); |
135 |
> |
}}); |
136 |
> |
|
137 |
> |
t.start(); |
138 |
> |
assertTrue(a.compareAndSet(this, one, two)); |
139 |
> |
t.join(LONG_DELAY_MS); |
140 |
> |
assertFalse(t.isAlive()); |
141 |
> |
assertEquals(a.get(this), three); |
142 |
> |
} |
143 |
> |
|
144 |
> |
/** |
145 |
> |
* repeated weakCompareAndSet succeeds in changing value when equal |
146 |
> |
* to expected |
147 |
> |
*/ |
148 |
> |
public void testWeakCompareAndSet() { |
149 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
150 |
> |
try { |
151 |
> |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
152 |
> |
} catch (RuntimeException ok) { |
153 |
> |
return; |
154 |
> |
} |
155 |
> |
x = one; |
156 |
> |
while (!a.weakCompareAndSet(this,one,two)); |
157 |
> |
while (!a.weakCompareAndSet(this,two,m4)); |
158 |
> |
assertEquals(m4,a.get(this)); |
159 |
> |
while (!a.weakCompareAndSet(this,m4,seven)); |
160 |
> |
assertEquals(seven,a.get(this)); |
161 |
> |
} |
162 |
> |
|
163 |
> |
/** |
164 |
> |
* getAndSet returns previous value and sets to given value |
165 |
> |
*/ |
166 |
> |
public void testGetAndSet() { |
167 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
168 |
> |
try { |
169 |
> |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
170 |
> |
} catch (RuntimeException ok) { |
171 |
> |
return; |
172 |
> |
} |
173 |
|
x = one; |
174 |
< |
assertEquals(one,a.getAndSet(this, zero)); |
175 |
< |
assertEquals(zero,a.getAndSet(this,m10)); |
176 |
< |
assertEquals(m10,a.getAndSet(this,1)); |
174 |
> |
assertEquals(one,a.getAndSet(this, zero)); |
175 |
> |
assertEquals(zero,a.getAndSet(this,m10)); |
176 |
> |
assertEquals(m10,a.getAndSet(this,1)); |
177 |
|
} |
178 |
|
|
179 |
|
} |