1 |
|
/* |
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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import java.util.concurrent.atomic.*; |
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; |
11 |
|
|
12 |
|
public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { |
13 |
|
volatile Integer x = null; |
21 |
|
return new TestSuite(AtomicReferenceFieldUpdaterTest.class); |
22 |
|
} |
23 |
|
|
24 |
+ |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) { |
25 |
+ |
return AtomicReferenceFieldUpdater.newUpdater |
26 |
+ |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName); |
27 |
+ |
} |
28 |
+ |
|
29 |
|
/** |
30 |
|
* Construction with non-existent field throws RuntimeException |
31 |
|
*/ |
32 |
|
public void testConstructor() { |
33 |
|
try { |
34 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
31 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
32 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "y"); |
34 |
> |
updaterFor("y"); |
35 |
|
shouldThrow(); |
36 |
< |
} |
35 |
< |
catch (RuntimeException rt) {} |
36 |
> |
} catch (RuntimeException success) {} |
37 |
|
} |
38 |
|
|
38 |
– |
|
39 |
|
/** |
40 |
|
* construction with field not of given type throws RuntimeException |
41 |
|
*/ |
42 |
|
public void testConstructor2() { |
43 |
|
try { |
44 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
45 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
46 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "z"); |
44 |
> |
updaterFor("z"); |
45 |
|
shouldThrow(); |
46 |
< |
} |
49 |
< |
catch (RuntimeException rt) {} |
46 |
> |
} catch (RuntimeException success) {} |
47 |
|
} |
48 |
|
|
49 |
|
/** |
51 |
|
*/ |
52 |
|
public void testConstructor3() { |
53 |
|
try { |
54 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
58 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
59 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "w"); |
54 |
> |
updaterFor("w"); |
55 |
|
shouldThrow(); |
56 |
< |
} |
62 |
< |
catch (RuntimeException rt) {} |
56 |
> |
} catch (RuntimeException success) {} |
57 |
|
} |
58 |
|
|
59 |
|
/** |
60 |
< |
* get returns the last value set or assigned |
60 |
> |
* get returns the last value set or assigned |
61 |
|
*/ |
62 |
|
public void testGetSet() { |
63 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
64 |
< |
try { |
71 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
72 |
< |
} catch (RuntimeException ok) { |
73 |
< |
return; |
74 |
< |
} |
64 |
> |
a = updaterFor("x"); |
65 |
|
x = one; |
66 |
< |
assertEquals(one,a.get(this)); |
67 |
< |
a.set(this,two); |
68 |
< |
assertEquals(two,a.get(this)); |
69 |
< |
a.set(this,m3); |
70 |
< |
assertEquals(m3,a.get(this)); |
66 |
> |
assertSame(one, a.get(this)); |
67 |
> |
a.set(this, two); |
68 |
> |
assertSame(two, a.get(this)); |
69 |
> |
a.set(this, m3); |
70 |
> |
assertSame(m3, a.get(this)); |
71 |
|
} |
72 |
|
|
73 |
|
/** |
74 |
< |
* get returns the last value lazySet by same thread |
74 |
> |
* get returns the last value lazySet by same thread |
75 |
|
*/ |
76 |
|
public void testGetLazySet() { |
77 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
78 |
< |
try { |
79 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
80 |
< |
} catch (RuntimeException ok) { |
81 |
< |
return; |
82 |
< |
} |
83 |
< |
x = one; |
84 |
< |
assertEquals(one,a.get(this)); |
95 |
< |
a.lazySet(this,two); |
96 |
< |
assertEquals(two,a.get(this)); |
97 |
< |
a.lazySet(this,m3); |
98 |
< |
assertEquals(m3,a.get(this)); |
78 |
> |
a = updaterFor("x"); |
79 |
> |
x = one; |
80 |
> |
assertSame(one, a.get(this)); |
81 |
> |
a.lazySet(this, two); |
82 |
> |
assertSame(two, a.get(this)); |
83 |
> |
a.lazySet(this, m3); |
84 |
> |
assertSame(m3, a.get(this)); |
85 |
|
} |
86 |
|
|
87 |
|
/** |
89 |
|
*/ |
90 |
|
public void testCompareAndSet() { |
91 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
92 |
< |
try { |
93 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
94 |
< |
} catch (RuntimeException ok) { |
95 |
< |
return; |
96 |
< |
} |
97 |
< |
x = one; |
98 |
< |
assertTrue(a.compareAndSet(this,one,two)); |
99 |
< |
assertTrue(a.compareAndSet(this,two,m4)); |
100 |
< |
assertEquals(m4,a.get(this)); |
115 |
< |
assertFalse(a.compareAndSet(this,m5,seven)); |
116 |
< |
assertFalse((seven == a.get(this))); |
117 |
< |
assertTrue(a.compareAndSet(this,m4,seven)); |
118 |
< |
assertEquals(seven,a.get(this)); |
92 |
> |
a = updaterFor("x"); |
93 |
> |
x = one; |
94 |
> |
assertTrue(a.compareAndSet(this, one, two)); |
95 |
> |
assertTrue(a.compareAndSet(this, two, m4)); |
96 |
> |
assertSame(m4, a.get(this)); |
97 |
> |
assertFalse(a.compareAndSet(this, m5, seven)); |
98 |
> |
assertFalse(seven == a.get(this)); |
99 |
> |
assertTrue(a.compareAndSet(this, m4, seven)); |
100 |
> |
assertSame(seven, a.get(this)); |
101 |
|
} |
102 |
|
|
103 |
|
/** |
107 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
108 |
|
x = one; |
109 |
|
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
110 |
< |
try { |
111 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
112 |
< |
} catch (RuntimeException ok) { |
113 |
< |
return; |
114 |
< |
} |
115 |
< |
|
116 |
< |
Thread t = new Thread(new Runnable() { |
135 |
< |
public void run() { |
136 |
< |
while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield(); |
137 |
< |
}}); |
110 |
> |
a = updaterFor("x"); |
111 |
> |
|
112 |
> |
Thread t = new Thread(new CheckedRunnable() { |
113 |
> |
public void realRun() { |
114 |
> |
while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) |
115 |
> |
Thread.yield(); |
116 |
> |
}}); |
117 |
|
|
118 |
|
t.start(); |
119 |
|
assertTrue(a.compareAndSet(this, one, two)); |
120 |
|
t.join(LONG_DELAY_MS); |
121 |
|
assertFalse(t.isAlive()); |
122 |
< |
assertEquals(a.get(this), three); |
122 |
> |
assertSame(three, a.get(this)); |
123 |
|
} |
124 |
|
|
125 |
|
/** |
128 |
|
*/ |
129 |
|
public void testWeakCompareAndSet() { |
130 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
131 |
< |
try { |
132 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
133 |
< |
} catch (RuntimeException ok) { |
134 |
< |
return; |
135 |
< |
} |
136 |
< |
x = one; |
137 |
< |
while (!a.weakCompareAndSet(this,one,two)); |
159 |
< |
while (!a.weakCompareAndSet(this,two,m4)); |
160 |
< |
assertEquals(m4,a.get(this)); |
161 |
< |
while (!a.weakCompareAndSet(this,m4,seven)); |
162 |
< |
assertEquals(seven,a.get(this)); |
131 |
> |
a = updaterFor("x"); |
132 |
> |
x = one; |
133 |
> |
while (!a.weakCompareAndSet(this, one, two)); |
134 |
> |
while (!a.weakCompareAndSet(this, two, m4)); |
135 |
> |
assertSame(m4, a.get(this)); |
136 |
> |
while (!a.weakCompareAndSet(this, m4, seven)); |
137 |
> |
assertSame(seven, a.get(this)); |
138 |
|
} |
139 |
|
|
140 |
|
/** |
142 |
|
*/ |
143 |
|
public void testGetAndSet() { |
144 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
145 |
< |
try { |
146 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
147 |
< |
} catch (RuntimeException ok) { |
148 |
< |
return; |
149 |
< |
} |
175 |
< |
x = one; |
176 |
< |
assertEquals(one,a.getAndSet(this, zero)); |
177 |
< |
assertEquals(zero,a.getAndSet(this,m10)); |
178 |
< |
assertEquals(m10,a.getAndSet(this,1)); |
145 |
> |
a = updaterFor("x"); |
146 |
> |
x = one; |
147 |
> |
assertSame(one, a.getAndSet(this, zero)); |
148 |
> |
assertSame(zero, a.getAndSet(this, m10)); |
149 |
> |
assertSame(m10, a.getAndSet(this, 1)); |
150 |
|
} |
151 |
|
|
152 |
|
} |