author | dl |
Mon, 12 Sep 2016 13:14:49 -0700 | |
changeset 40817 | 4f5fb115676d |
parent 39725 | 9548f8d846e9 |
child 44589 | 64d9270bd24c |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
|
33 |
* Pat Fisher, Mike Judd. |
|
34 |
*/ |
|
35 |
||
36 |
import java.util.concurrent.atomic.AtomicReference; |
|
37 |
||
38 |
import junit.framework.Test; |
|
39 |
import junit.framework.TestSuite; |
|
40 |
||
41 |
public class AtomicReferenceTest extends JSR166TestCase { |
|
42 |
public static void main(String[] args) { |
|
43 |
main(suite(), args); |
|
44 |
} |
|
45 |
public static Test suite() { |
|
46 |
return new TestSuite(AtomicReferenceTest.class); |
|
47 |
} |
|
48 |
||
49 |
/** |
|
50 |
* constructor initializes to given value |
|
51 |
*/ |
|
52 |
public void testConstructor() { |
|
53 |
AtomicReference ai = new AtomicReference(one); |
|
54 |
assertSame(one, ai.get()); |
|
55 |
} |
|
56 |
||
57 |
/** |
|
58 |
* default constructed initializes to null |
|
59 |
*/ |
|
60 |
public void testConstructor2() { |
|
61 |
AtomicReference ai = new AtomicReference(); |
|
62 |
assertNull(ai.get()); |
|
63 |
} |
|
64 |
||
65 |
/** |
|
66 |
* get returns the last value set |
|
67 |
*/ |
|
68 |
public void testGetSet() { |
|
69 |
AtomicReference ai = new AtomicReference(one); |
|
70 |
assertSame(one, ai.get()); |
|
71 |
ai.set(two); |
|
72 |
assertSame(two, ai.get()); |
|
73 |
ai.set(m3); |
|
74 |
assertSame(m3, ai.get()); |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* get returns the last value lazySet in same thread |
|
79 |
*/ |
|
80 |
public void testGetLazySet() { |
|
81 |
AtomicReference ai = new AtomicReference(one); |
|
82 |
assertSame(one, ai.get()); |
|
83 |
ai.lazySet(two); |
|
84 |
assertSame(two, ai.get()); |
|
85 |
ai.lazySet(m3); |
|
86 |
assertSame(m3, ai.get()); |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* compareAndSet succeeds in changing value if equal to expected else fails |
|
91 |
*/ |
|
92 |
public void testCompareAndSet() { |
|
93 |
AtomicReference ai = new AtomicReference(one); |
|
94 |
assertTrue(ai.compareAndSet(one, two)); |
|
95 |
assertTrue(ai.compareAndSet(two, m4)); |
|
96 |
assertSame(m4, ai.get()); |
|
97 |
assertFalse(ai.compareAndSet(m5, seven)); |
|
98 |
assertSame(m4, ai.get()); |
|
99 |
assertTrue(ai.compareAndSet(m4, seven)); |
|
100 |
assertSame(seven, ai.get()); |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* compareAndSet in one thread enables another waiting for value |
|
105 |
* to succeed |
|
106 |
*/ |
|
107 |
public void testCompareAndSetInMultipleThreads() throws Exception { |
|
108 |
final AtomicReference ai = new AtomicReference(one); |
|
109 |
Thread t = new Thread(new CheckedRunnable() { |
|
110 |
public void realRun() { |
|
111 |
while (!ai.compareAndSet(two, three)) |
|
112 |
Thread.yield(); |
|
113 |
}}); |
|
114 |
||
115 |
t.start(); |
|
116 |
assertTrue(ai.compareAndSet(one, two)); |
|
117 |
t.join(LONG_DELAY_MS); |
|
118 |
assertFalse(t.isAlive()); |
|
119 |
assertSame(three, ai.get()); |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* repeated weakCompareAndSet succeeds in changing value when equal |
|
124 |
* to expected |
|
125 |
*/ |
|
126 |
public void testWeakCompareAndSet() { |
|
127 |
AtomicReference ai = new AtomicReference(one); |
|
128 |
do {} while (!ai.weakCompareAndSet(one, two)); |
|
129 |
do {} while (!ai.weakCompareAndSet(two, m4)); |
|
130 |
assertSame(m4, ai.get()); |
|
131 |
do {} while (!ai.weakCompareAndSet(m4, seven)); |
|
132 |
assertSame(seven, ai.get()); |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* getAndSet returns previous value and sets to given value |
|
137 |
*/ |
|
138 |
public void testGetAndSet() { |
|
139 |
AtomicReference ai = new AtomicReference(one); |
|
140 |
assertSame(one, ai.getAndSet(zero)); |
|
141 |
assertSame(zero, ai.getAndSet(m10)); |
|
142 |
assertSame(m10, ai.getAndSet(one)); |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* a deserialized serialized atomic holds same value |
|
147 |
*/ |
|
148 |
public void testSerialization() throws Exception { |
|
149 |
AtomicReference x = new AtomicReference(); |
|
150 |
AtomicReference y = serialClone(x); |
|
151 |
assertNotSame(x, y); |
|
152 |
x.set(one); |
|
153 |
AtomicReference z = serialClone(x); |
|
154 |
assertNotSame(y, z); |
|
155 |
assertEquals(one, x.get()); |
|
156 |
assertEquals(null, y.get()); |
|
157 |
assertEquals(one, z.get()); |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* toString returns current value. |
|
162 |
*/ |
|
163 |
public void testToString() { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
35394
diff
changeset
|
164 |
AtomicReference<Integer> ai = new AtomicReference<>(one); |
35394 | 165 |
assertEquals(one.toString(), ai.toString()); |
166 |
ai.set(two); |
|
167 |
assertEquals(two.toString(), ai.toString()); |
|
168 |
} |
|
169 |
||
170 |
} |