1 |
dl |
1.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 |
5 |
jsr166 |
1.22 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
|
10 |
|
|
import junit.framework.*; |
11 |
|
|
import java.util.*; |
12 |
|
|
import java.util.concurrent.*; |
13 |
jsr166 |
1.30 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
dl |
1.1 |
import java.util.concurrent.locks.*; |
15 |
|
|
import java.io.*; |
16 |
|
|
|
17 |
|
|
public class AbstractQueuedSynchronizerTest extends JSR166TestCase { |
18 |
|
|
public static void main(String[] args) { |
19 |
|
|
junit.textui.TestRunner.run (suite()); |
20 |
|
|
} |
21 |
|
|
public static Test suite() { |
22 |
|
|
return new TestSuite(AbstractQueuedSynchronizerTest.class); |
23 |
|
|
} |
24 |
|
|
|
25 |
|
|
/** |
26 |
dl |
1.12 |
* A simple mutex class, adapted from the |
27 |
|
|
* AbstractQueuedSynchronizer javadoc. Exclusive acquire tests |
28 |
|
|
* exercise this as a sample user extension. Other |
29 |
|
|
* methods/features of AbstractQueuedSynchronizerTest are tested |
30 |
|
|
* via other test classes, including those for ReentrantLock, |
31 |
|
|
* ReentrantReadWriteLock, and Semaphore |
32 |
|
|
*/ |
33 |
|
|
static class Mutex extends AbstractQueuedSynchronizer { |
34 |
dl |
1.16 |
public boolean isHeldExclusively() { return getState() == 1; } |
35 |
jsr166 |
1.22 |
|
36 |
dl |
1.16 |
public boolean tryAcquire(int acquires) { |
37 |
jsr166 |
1.22 |
assertTrue(acquires == 1); |
38 |
dl |
1.12 |
return compareAndSetState(0, 1); |
39 |
|
|
} |
40 |
jsr166 |
1.22 |
|
41 |
dl |
1.16 |
public boolean tryRelease(int releases) { |
42 |
|
|
if (getState() == 0) throw new IllegalMonitorStateException(); |
43 |
dl |
1.12 |
setState(0); |
44 |
|
|
return true; |
45 |
|
|
} |
46 |
jsr166 |
1.22 |
|
47 |
dl |
1.15 |
public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); } |
48 |
dl |
1.12 |
|
49 |
|
|
} |
50 |
|
|
|
51 |
jsr166 |
1.22 |
|
52 |
dl |
1.12 |
/** |
53 |
|
|
* A simple latch class, to test shared mode. |
54 |
|
|
*/ |
55 |
jsr166 |
1.22 |
static class BooleanLatch extends AbstractQueuedSynchronizer { |
56 |
dl |
1.12 |
public boolean isSignalled() { return getState() != 0; } |
57 |
|
|
|
58 |
|
|
public int tryAcquireShared(int ignore) { |
59 |
|
|
return isSignalled()? 1 : -1; |
60 |
dl |
1.1 |
} |
61 |
jsr166 |
1.22 |
|
62 |
dl |
1.12 |
public boolean tryReleaseShared(int ignore) { |
63 |
|
|
setState(1); |
64 |
|
|
return true; |
65 |
dl |
1.1 |
} |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
/** |
69 |
jsr166 |
1.28 |
* A runnable calling acquireInterruptibly that does not expect to |
70 |
|
|
* be interrupted. |
71 |
dl |
1.1 |
*/ |
72 |
jsr166 |
1.28 |
class InterruptibleSyncRunnable extends CheckedRunnable { |
73 |
dl |
1.17 |
final Mutex sync; |
74 |
|
|
InterruptibleSyncRunnable(Mutex l) { sync = l; } |
75 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
76 |
|
|
sync.acquireInterruptibly(1); |
77 |
dl |
1.1 |
} |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
|
81 |
|
|
/** |
82 |
dl |
1.16 |
* A runnable calling acquireInterruptibly that expects to be |
83 |
jsr166 |
1.28 |
* interrupted. |
84 |
dl |
1.1 |
*/ |
85 |
jsr166 |
1.28 |
class InterruptedSyncRunnable extends CheckedInterruptedRunnable { |
86 |
dl |
1.17 |
final Mutex sync; |
87 |
|
|
InterruptedSyncRunnable(Mutex l) { sync = l; } |
88 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
89 |
|
|
sync.acquireInterruptibly(1); |
90 |
dl |
1.1 |
} |
91 |
|
|
} |
92 |
dl |
1.16 |
|
93 |
|
|
/** |
94 |
|
|
* isHeldExclusively is false upon construction |
95 |
|
|
*/ |
96 |
jsr166 |
1.22 |
public void testIsHeldExclusively() { |
97 |
jsr166 |
1.29 |
Mutex rl = new Mutex(); |
98 |
dl |
1.16 |
assertFalse(rl.isHeldExclusively()); |
99 |
|
|
} |
100 |
jsr166 |
1.22 |
|
101 |
dl |
1.1 |
/** |
102 |
dl |
1.17 |
* acquiring released sync succeeds |
103 |
dl |
1.1 |
*/ |
104 |
jsr166 |
1.22 |
public void testAcquire() { |
105 |
jsr166 |
1.29 |
Mutex rl = new Mutex(); |
106 |
dl |
1.16 |
rl.acquire(1); |
107 |
|
|
assertTrue(rl.isHeldExclusively()); |
108 |
|
|
rl.release(1); |
109 |
dl |
1.17 |
assertFalse(rl.isHeldExclusively()); |
110 |
dl |
1.1 |
} |
111 |
|
|
|
112 |
|
|
/** |
113 |
dl |
1.17 |
* tryAcquire on an released sync succeeds |
114 |
dl |
1.1 |
*/ |
115 |
jsr166 |
1.22 |
public void testTryAcquire() { |
116 |
jsr166 |
1.29 |
Mutex rl = new Mutex(); |
117 |
dl |
1.16 |
assertTrue(rl.tryAcquire(1)); |
118 |
|
|
assertTrue(rl.isHeldExclusively()); |
119 |
|
|
rl.release(1); |
120 |
dl |
1.1 |
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* hasQueuedThreads reports whether there are waiting threads |
124 |
|
|
*/ |
125 |
jsr166 |
1.26 |
public void testhasQueuedThreads() throws InterruptedException { |
126 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
127 |
dl |
1.17 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
128 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
129 |
jsr166 |
1.26 |
assertFalse(sync.hasQueuedThreads()); |
130 |
|
|
sync.acquire(1); |
131 |
|
|
t1.start(); |
132 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
133 |
|
|
assertTrue(sync.hasQueuedThreads()); |
134 |
|
|
t2.start(); |
135 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
136 |
|
|
assertTrue(sync.hasQueuedThreads()); |
137 |
|
|
t1.interrupt(); |
138 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
139 |
|
|
assertTrue(sync.hasQueuedThreads()); |
140 |
|
|
sync.release(1); |
141 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
142 |
|
|
assertFalse(sync.hasQueuedThreads()); |
143 |
|
|
t1.join(); |
144 |
|
|
t2.join(); |
145 |
jsr166 |
1.22 |
} |
146 |
dl |
1.10 |
|
147 |
|
|
/** |
148 |
dl |
1.11 |
* isQueued(null) throws NPE |
149 |
|
|
*/ |
150 |
jsr166 |
1.22 |
public void testIsQueuedNPE() { |
151 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
152 |
dl |
1.11 |
try { |
153 |
dl |
1.17 |
sync.isQueued(null); |
154 |
dl |
1.11 |
shouldThrow(); |
155 |
jsr166 |
1.26 |
} catch (NullPointerException success) {} |
156 |
dl |
1.11 |
} |
157 |
|
|
|
158 |
|
|
/** |
159 |
dl |
1.10 |
* isQueued reports whether a thread is queued. |
160 |
|
|
*/ |
161 |
jsr166 |
1.26 |
public void testIsQueued() throws InterruptedException { |
162 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
163 |
dl |
1.17 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
164 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
165 |
jsr166 |
1.26 |
assertFalse(sync.isQueued(t1)); |
166 |
|
|
assertFalse(sync.isQueued(t2)); |
167 |
|
|
sync.acquire(1); |
168 |
|
|
t1.start(); |
169 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
170 |
|
|
assertTrue(sync.isQueued(t1)); |
171 |
|
|
t2.start(); |
172 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
173 |
|
|
assertTrue(sync.isQueued(t1)); |
174 |
|
|
assertTrue(sync.isQueued(t2)); |
175 |
|
|
t1.interrupt(); |
176 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
177 |
|
|
assertFalse(sync.isQueued(t1)); |
178 |
|
|
assertTrue(sync.isQueued(t2)); |
179 |
|
|
sync.release(1); |
180 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
181 |
|
|
assertFalse(sync.isQueued(t1)); |
182 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
183 |
|
|
assertFalse(sync.isQueued(t2)); |
184 |
|
|
t1.join(); |
185 |
|
|
t2.join(); |
186 |
jsr166 |
1.22 |
} |
187 |
dl |
1.8 |
|
188 |
dl |
1.12 |
/** |
189 |
dl |
1.17 |
* getFirstQueuedThread returns first waiting thread or null if none |
190 |
dl |
1.12 |
*/ |
191 |
jsr166 |
1.26 |
public void testGetFirstQueuedThread() throws InterruptedException { |
192 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
193 |
dl |
1.17 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
194 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
195 |
jsr166 |
1.26 |
assertNull(sync.getFirstQueuedThread()); |
196 |
|
|
sync.acquire(1); |
197 |
|
|
t1.start(); |
198 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
199 |
|
|
assertEquals(t1, sync.getFirstQueuedThread()); |
200 |
|
|
t2.start(); |
201 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
202 |
|
|
assertEquals(t1, sync.getFirstQueuedThread()); |
203 |
|
|
t1.interrupt(); |
204 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
205 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
206 |
|
|
assertEquals(t2, sync.getFirstQueuedThread()); |
207 |
|
|
sync.release(1); |
208 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
209 |
|
|
assertNull(sync.getFirstQueuedThread()); |
210 |
|
|
t1.join(); |
211 |
|
|
t2.join(); |
212 |
jsr166 |
1.22 |
} |
213 |
dl |
1.12 |
|
214 |
dl |
1.8 |
|
215 |
|
|
/** |
216 |
dl |
1.12 |
* hasContended reports false if no thread has ever blocked, else true |
217 |
dl |
1.8 |
*/ |
218 |
jsr166 |
1.26 |
public void testHasContended() throws InterruptedException { |
219 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
220 |
dl |
1.17 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
221 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
222 |
jsr166 |
1.26 |
assertFalse(sync.hasContended()); |
223 |
|
|
sync.acquire(1); |
224 |
|
|
t1.start(); |
225 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
226 |
|
|
assertTrue(sync.hasContended()); |
227 |
|
|
t2.start(); |
228 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
229 |
|
|
assertTrue(sync.hasContended()); |
230 |
|
|
t1.interrupt(); |
231 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
232 |
|
|
assertTrue(sync.hasContended()); |
233 |
|
|
sync.release(1); |
234 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
235 |
|
|
assertTrue(sync.hasContended()); |
236 |
|
|
t1.join(); |
237 |
|
|
t2.join(); |
238 |
jsr166 |
1.22 |
} |
239 |
dl |
1.17 |
|
240 |
|
|
/** |
241 |
|
|
* getQueuedThreads includes waiting threads |
242 |
|
|
*/ |
243 |
jsr166 |
1.26 |
public void testGetQueuedThreads() throws InterruptedException { |
244 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
245 |
dl |
1.17 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
246 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
247 |
jsr166 |
1.26 |
assertTrue(sync.getQueuedThreads().isEmpty()); |
248 |
|
|
sync.acquire(1); |
249 |
|
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
250 |
|
|
t1.start(); |
251 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
252 |
|
|
assertTrue(sync.getQueuedThreads().contains(t1)); |
253 |
|
|
t2.start(); |
254 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
255 |
|
|
assertTrue(sync.getQueuedThreads().contains(t1)); |
256 |
|
|
assertTrue(sync.getQueuedThreads().contains(t2)); |
257 |
|
|
t1.interrupt(); |
258 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
259 |
|
|
assertFalse(sync.getQueuedThreads().contains(t1)); |
260 |
|
|
assertTrue(sync.getQueuedThreads().contains(t2)); |
261 |
|
|
sync.release(1); |
262 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
263 |
|
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
264 |
|
|
t1.join(); |
265 |
|
|
t2.join(); |
266 |
jsr166 |
1.22 |
} |
267 |
dl |
1.17 |
|
268 |
|
|
/** |
269 |
|
|
* getExclusiveQueuedThreads includes waiting threads |
270 |
|
|
*/ |
271 |
jsr166 |
1.26 |
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
272 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
273 |
dl |
1.17 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
274 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
275 |
jsr166 |
1.26 |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
276 |
|
|
sync.acquire(1); |
277 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
278 |
|
|
t1.start(); |
279 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
280 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
281 |
|
|
t2.start(); |
282 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
283 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
284 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
285 |
|
|
t1.interrupt(); |
286 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
287 |
|
|
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
288 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
289 |
|
|
sync.release(1); |
290 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
291 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
292 |
|
|
t1.join(); |
293 |
|
|
t2.join(); |
294 |
jsr166 |
1.22 |
} |
295 |
dl |
1.17 |
|
296 |
|
|
/** |
297 |
|
|
* getSharedQueuedThreads does not include exclusively waiting threads |
298 |
|
|
*/ |
299 |
jsr166 |
1.26 |
public void testGetSharedQueuedThreads() throws InterruptedException { |
300 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
301 |
dl |
1.17 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
302 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
303 |
jsr166 |
1.26 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
304 |
|
|
sync.acquire(1); |
305 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
306 |
|
|
t1.start(); |
307 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
308 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
309 |
|
|
t2.start(); |
310 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
311 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
312 |
|
|
t1.interrupt(); |
313 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
314 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
315 |
|
|
sync.release(1); |
316 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
317 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
318 |
|
|
t1.join(); |
319 |
|
|
t2.join(); |
320 |
jsr166 |
1.22 |
} |
321 |
dl |
1.1 |
|
322 |
|
|
/** |
323 |
dl |
1.16 |
* tryAcquireNanos is interruptible. |
324 |
dl |
1.1 |
*/ |
325 |
jsr166 |
1.27 |
public void testInterruptedException2() throws InterruptedException { |
326 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
327 |
|
|
sync.acquire(1); |
328 |
|
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
329 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
330 |
jsr166 |
1.33 |
sync.tryAcquireNanos(1, MILLISECONDS.toNanos(MEDIUM_DELAY_MS)); |
331 |
jsr166 |
1.28 |
}}); |
332 |
jsr166 |
1.26 |
|
333 |
|
|
t.start(); |
334 |
jsr166 |
1.32 |
Thread.sleep(SHORT_DELAY_MS); |
335 |
jsr166 |
1.26 |
t.interrupt(); |
336 |
jsr166 |
1.27 |
t.join(); |
337 |
dl |
1.1 |
} |
338 |
|
|
|
339 |
|
|
|
340 |
|
|
/** |
341 |
dl |
1.17 |
* TryAcquire on exclusively held sync fails |
342 |
dl |
1.1 |
*/ |
343 |
jsr166 |
1.26 |
public void testTryAcquireWhenSynced() throws InterruptedException { |
344 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
345 |
|
|
sync.acquire(1); |
346 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
347 |
jsr166 |
1.28 |
public void realRun() { |
348 |
|
|
threadAssertFalse(sync.tryAcquire(1)); |
349 |
|
|
}}); |
350 |
jsr166 |
1.26 |
|
351 |
|
|
t.start(); |
352 |
|
|
t.join(); |
353 |
|
|
sync.release(1); |
354 |
jsr166 |
1.22 |
} |
355 |
dl |
1.1 |
|
356 |
|
|
/** |
357 |
dl |
1.17 |
* tryAcquireNanos on an exclusively held sync times out |
358 |
dl |
1.1 |
*/ |
359 |
jsr166 |
1.26 |
public void testAcquireNanos_Timeout() throws InterruptedException { |
360 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
361 |
|
|
sync.acquire(1); |
362 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
363 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
364 |
jsr166 |
1.33 |
long nanos = MILLISECONDS.toNanos(SHORT_DELAY_MS); |
365 |
|
|
assertFalse(sync.tryAcquireNanos(1, nanos)); |
366 |
jsr166 |
1.28 |
}}); |
367 |
jsr166 |
1.26 |
|
368 |
|
|
t.start(); |
369 |
|
|
t.join(); |
370 |
|
|
sync.release(1); |
371 |
jsr166 |
1.22 |
} |
372 |
|
|
|
373 |
|
|
|
374 |
dl |
1.1 |
/** |
375 |
dl |
1.13 |
* getState is true when acquired and false when not |
376 |
dl |
1.1 |
*/ |
377 |
jsr166 |
1.26 |
public void testGetState() throws InterruptedException { |
378 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
379 |
|
|
sync.acquire(1); |
380 |
|
|
assertTrue(sync.isHeldExclusively()); |
381 |
|
|
sync.release(1); |
382 |
|
|
assertFalse(sync.isHeldExclusively()); |
383 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
384 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
385 |
|
|
sync.acquire(1); |
386 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
387 |
|
|
sync.release(1); |
388 |
|
|
}}); |
389 |
jsr166 |
1.26 |
|
390 |
|
|
t.start(); |
391 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
392 |
|
|
assertTrue(sync.isHeldExclusively()); |
393 |
|
|
t.join(); |
394 |
|
|
assertFalse(sync.isHeldExclusively()); |
395 |
dl |
1.1 |
} |
396 |
|
|
|
397 |
|
|
|
398 |
|
|
/** |
399 |
dl |
1.16 |
* acquireInterruptibly is interruptible. |
400 |
dl |
1.1 |
*/ |
401 |
jsr166 |
1.26 |
public void testAcquireInterruptibly1() throws InterruptedException { |
402 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
403 |
|
|
sync.acquire(1); |
404 |
|
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
405 |
jsr166 |
1.26 |
|
406 |
|
|
t.start(); |
407 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
408 |
|
|
t.interrupt(); |
409 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
410 |
|
|
sync.release(1); |
411 |
|
|
t.join(); |
412 |
jsr166 |
1.22 |
} |
413 |
dl |
1.1 |
|
414 |
|
|
/** |
415 |
dl |
1.16 |
* acquireInterruptibly succeeds when released, else is interruptible |
416 |
dl |
1.1 |
*/ |
417 |
jsr166 |
1.26 |
public void testAcquireInterruptibly2() throws InterruptedException { |
418 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
419 |
jsr166 |
1.26 |
sync.acquireInterruptibly(1); |
420 |
jsr166 |
1.29 |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
421 |
jsr166 |
1.26 |
t.start(); |
422 |
jsr166 |
1.32 |
Thread.sleep(SHORT_DELAY_MS); |
423 |
jsr166 |
1.26 |
t.interrupt(); |
424 |
|
|
assertTrue(sync.isHeldExclusively()); |
425 |
|
|
t.join(); |
426 |
dl |
1.1 |
} |
427 |
|
|
|
428 |
|
|
/** |
429 |
dl |
1.17 |
* owns is true for a condition created by sync else false |
430 |
dl |
1.12 |
*/ |
431 |
|
|
public void testOwns() { |
432 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
433 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
434 |
|
|
final Mutex sync2 = new Mutex(); |
435 |
|
|
assertTrue(sync.owns(c)); |
436 |
|
|
assertFalse(sync2.owns(c)); |
437 |
dl |
1.12 |
} |
438 |
|
|
|
439 |
|
|
/** |
440 |
dl |
1.17 |
* Calling await without holding sync throws IllegalMonitorStateException |
441 |
dl |
1.1 |
*/ |
442 |
jsr166 |
1.26 |
public void testAwait_IllegalMonitor() throws InterruptedException { |
443 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
444 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
445 |
dl |
1.1 |
try { |
446 |
|
|
c.await(); |
447 |
|
|
shouldThrow(); |
448 |
jsr166 |
1.28 |
} catch (IllegalMonitorStateException success) {} |
449 |
dl |
1.1 |
} |
450 |
|
|
|
451 |
|
|
/** |
452 |
dl |
1.17 |
* Calling signal without holding sync throws IllegalMonitorStateException |
453 |
dl |
1.1 |
*/ |
454 |
|
|
public void testSignal_IllegalMonitor() { |
455 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
456 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
457 |
dl |
1.1 |
try { |
458 |
|
|
c.signal(); |
459 |
|
|
shouldThrow(); |
460 |
jsr166 |
1.28 |
} catch (IllegalMonitorStateException success) {} |
461 |
dl |
1.1 |
} |
462 |
|
|
|
463 |
|
|
/** |
464 |
|
|
* awaitNanos without a signal times out |
465 |
|
|
*/ |
466 |
jsr166 |
1.26 |
public void testAwaitNanos_Timeout() throws InterruptedException { |
467 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
468 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
469 |
jsr166 |
1.26 |
sync.acquire(1); |
470 |
|
|
long t = c.awaitNanos(100); |
471 |
|
|
assertTrue(t <= 0); |
472 |
|
|
sync.release(1); |
473 |
dl |
1.1 |
} |
474 |
|
|
|
475 |
|
|
/** |
476 |
dl |
1.17 |
* Timed await without a signal times out |
477 |
dl |
1.1 |
*/ |
478 |
jsr166 |
1.26 |
public void testAwait_Timeout() throws InterruptedException { |
479 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
480 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
481 |
jsr166 |
1.26 |
sync.acquire(1); |
482 |
jsr166 |
1.30 |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
483 |
jsr166 |
1.26 |
sync.release(1); |
484 |
dl |
1.1 |
} |
485 |
|
|
|
486 |
|
|
/** |
487 |
|
|
* awaitUntil without a signal times out |
488 |
|
|
*/ |
489 |
jsr166 |
1.26 |
public void testAwaitUntil_Timeout() throws InterruptedException { |
490 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
491 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
492 |
jsr166 |
1.26 |
sync.acquire(1); |
493 |
|
|
java.util.Date d = new java.util.Date(); |
494 |
|
|
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
495 |
|
|
sync.release(1); |
496 |
dl |
1.1 |
} |
497 |
|
|
|
498 |
|
|
/** |
499 |
|
|
* await returns when signalled |
500 |
|
|
*/ |
501 |
jsr166 |
1.26 |
public void testAwait() throws InterruptedException { |
502 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
503 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
504 |
jsr166 |
1.29 |
Thread t = new Thread(new CheckedRunnable() { |
505 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
506 |
|
|
sync.acquire(1); |
507 |
|
|
c.await(); |
508 |
|
|
sync.release(1); |
509 |
|
|
}}); |
510 |
dl |
1.15 |
|
511 |
jsr166 |
1.26 |
t.start(); |
512 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
513 |
|
|
sync.acquire(1); |
514 |
|
|
c.signal(); |
515 |
|
|
sync.release(1); |
516 |
|
|
t.join(SHORT_DELAY_MS); |
517 |
|
|
assertFalse(t.isAlive()); |
518 |
dl |
1.15 |
} |
519 |
|
|
|
520 |
|
|
|
521 |
|
|
|
522 |
|
|
/** |
523 |
|
|
* hasWaiters throws NPE if null |
524 |
|
|
*/ |
525 |
|
|
public void testHasWaitersNPE() { |
526 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
527 |
dl |
1.15 |
try { |
528 |
dl |
1.17 |
sync.hasWaiters(null); |
529 |
dl |
1.15 |
shouldThrow(); |
530 |
jsr166 |
1.26 |
} catch (NullPointerException success) {} |
531 |
dl |
1.15 |
} |
532 |
|
|
|
533 |
|
|
/** |
534 |
|
|
* getWaitQueueLength throws NPE if null |
535 |
|
|
*/ |
536 |
|
|
public void testGetWaitQueueLengthNPE() { |
537 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
538 |
dl |
1.15 |
try { |
539 |
dl |
1.17 |
sync.getWaitQueueLength(null); |
540 |
dl |
1.15 |
shouldThrow(); |
541 |
jsr166 |
1.26 |
} catch (NullPointerException success) {} |
542 |
dl |
1.15 |
} |
543 |
|
|
|
544 |
|
|
|
545 |
|
|
/** |
546 |
|
|
* getWaitingThreads throws NPE if null |
547 |
|
|
*/ |
548 |
|
|
public void testGetWaitingThreadsNPE() { |
549 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
550 |
dl |
1.15 |
try { |
551 |
dl |
1.17 |
sync.getWaitingThreads(null); |
552 |
dl |
1.15 |
shouldThrow(); |
553 |
jsr166 |
1.26 |
} catch (NullPointerException success) {} |
554 |
dl |
1.15 |
} |
555 |
|
|
|
556 |
|
|
|
557 |
|
|
/** |
558 |
|
|
* hasWaiters throws IAE if not owned |
559 |
|
|
*/ |
560 |
|
|
public void testHasWaitersIAE() { |
561 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
562 |
jsr166 |
1.25 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
563 |
jsr166 |
1.29 |
final Mutex sync2 = new Mutex(); |
564 |
dl |
1.15 |
try { |
565 |
dl |
1.17 |
sync2.hasWaiters(c); |
566 |
dl |
1.15 |
shouldThrow(); |
567 |
jsr166 |
1.26 |
} catch (IllegalArgumentException success) {} |
568 |
dl |
1.15 |
} |
569 |
|
|
|
570 |
|
|
/** |
571 |
dl |
1.17 |
* hasWaiters throws IMSE if not synced |
572 |
dl |
1.15 |
*/ |
573 |
|
|
public void testHasWaitersIMSE() { |
574 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
575 |
jsr166 |
1.25 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
576 |
dl |
1.15 |
try { |
577 |
dl |
1.17 |
sync.hasWaiters(c); |
578 |
dl |
1.15 |
shouldThrow(); |
579 |
jsr166 |
1.26 |
} catch (IllegalMonitorStateException success) {} |
580 |
dl |
1.15 |
} |
581 |
|
|
|
582 |
|
|
|
583 |
|
|
/** |
584 |
|
|
* getWaitQueueLength throws IAE if not owned |
585 |
|
|
*/ |
586 |
|
|
public void testGetWaitQueueLengthIAE() { |
587 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
588 |
jsr166 |
1.25 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
589 |
jsr166 |
1.29 |
final Mutex sync2 = new Mutex(); |
590 |
dl |
1.15 |
try { |
591 |
dl |
1.17 |
sync2.getWaitQueueLength(c); |
592 |
dl |
1.15 |
shouldThrow(); |
593 |
jsr166 |
1.26 |
} catch (IllegalArgumentException success) {} |
594 |
dl |
1.15 |
} |
595 |
|
|
|
596 |
|
|
/** |
597 |
dl |
1.17 |
* getWaitQueueLength throws IMSE if not synced |
598 |
dl |
1.15 |
*/ |
599 |
|
|
public void testGetWaitQueueLengthIMSE() { |
600 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
601 |
jsr166 |
1.25 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
602 |
dl |
1.15 |
try { |
603 |
dl |
1.17 |
sync.getWaitQueueLength(c); |
604 |
dl |
1.15 |
shouldThrow(); |
605 |
jsr166 |
1.26 |
} catch (IllegalMonitorStateException success) {} |
606 |
dl |
1.15 |
} |
607 |
|
|
|
608 |
|
|
|
609 |
|
|
/** |
610 |
|
|
* getWaitingThreads throws IAE if not owned |
611 |
|
|
*/ |
612 |
|
|
public void testGetWaitingThreadsIAE() { |
613 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
614 |
jsr166 |
1.25 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
615 |
jsr166 |
1.29 |
final Mutex sync2 = new Mutex(); |
616 |
dl |
1.15 |
try { |
617 |
dl |
1.17 |
sync2.getWaitingThreads(c); |
618 |
dl |
1.15 |
shouldThrow(); |
619 |
jsr166 |
1.26 |
} catch (IllegalArgumentException success) {} |
620 |
dl |
1.15 |
} |
621 |
|
|
|
622 |
|
|
/** |
623 |
dl |
1.17 |
* getWaitingThreads throws IMSE if not synced |
624 |
dl |
1.15 |
*/ |
625 |
|
|
public void testGetWaitingThreadsIMSE() { |
626 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
627 |
jsr166 |
1.25 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
628 |
dl |
1.15 |
try { |
629 |
dl |
1.17 |
sync.getWaitingThreads(c); |
630 |
dl |
1.15 |
shouldThrow(); |
631 |
jsr166 |
1.26 |
} catch (IllegalMonitorStateException success) {} |
632 |
dl |
1.15 |
} |
633 |
|
|
|
634 |
|
|
|
635 |
|
|
|
636 |
|
|
/** |
637 |
|
|
* hasWaiters returns true when a thread is waiting, else false |
638 |
|
|
*/ |
639 |
jsr166 |
1.26 |
public void testHasWaiters() throws InterruptedException { |
640 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
641 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
642 |
jsr166 |
1.29 |
Thread t = new Thread(new CheckedRunnable() { |
643 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
644 |
|
|
sync.acquire(1); |
645 |
|
|
threadAssertFalse(sync.hasWaiters(c)); |
646 |
|
|
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
647 |
|
|
c.await(); |
648 |
|
|
sync.release(1); |
649 |
|
|
}}); |
650 |
dl |
1.15 |
|
651 |
jsr166 |
1.26 |
t.start(); |
652 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
653 |
|
|
sync.acquire(1); |
654 |
|
|
assertTrue(sync.hasWaiters(c)); |
655 |
|
|
assertEquals(1, sync.getWaitQueueLength(c)); |
656 |
|
|
c.signal(); |
657 |
|
|
sync.release(1); |
658 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
659 |
|
|
sync.acquire(1); |
660 |
|
|
assertFalse(sync.hasWaiters(c)); |
661 |
|
|
assertEquals(0, sync.getWaitQueueLength(c)); |
662 |
|
|
sync.release(1); |
663 |
|
|
t.join(SHORT_DELAY_MS); |
664 |
|
|
assertFalse(t.isAlive()); |
665 |
dl |
1.15 |
} |
666 |
|
|
|
667 |
|
|
/** |
668 |
|
|
* getWaitQueueLength returns number of waiting threads |
669 |
|
|
*/ |
670 |
jsr166 |
1.26 |
public void testGetWaitQueueLength() throws InterruptedException { |
671 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
672 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
673 |
jsr166 |
1.29 |
Thread t1 = new Thread(new CheckedRunnable() { |
674 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
675 |
|
|
sync.acquire(1); |
676 |
|
|
threadAssertFalse(sync.hasWaiters(c)); |
677 |
|
|
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
678 |
|
|
c.await(); |
679 |
|
|
sync.release(1); |
680 |
|
|
}}); |
681 |
|
|
|
682 |
jsr166 |
1.29 |
Thread t2 = new Thread(new CheckedRunnable() { |
683 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
684 |
|
|
sync.acquire(1); |
685 |
|
|
threadAssertTrue(sync.hasWaiters(c)); |
686 |
|
|
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
687 |
|
|
c.await(); |
688 |
|
|
sync.release(1); |
689 |
|
|
}}); |
690 |
dl |
1.15 |
|
691 |
jsr166 |
1.26 |
t1.start(); |
692 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
693 |
|
|
t2.start(); |
694 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
695 |
|
|
sync.acquire(1); |
696 |
|
|
assertTrue(sync.hasWaiters(c)); |
697 |
|
|
assertEquals(2, sync.getWaitQueueLength(c)); |
698 |
|
|
c.signalAll(); |
699 |
|
|
sync.release(1); |
700 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
701 |
|
|
sync.acquire(1); |
702 |
|
|
assertFalse(sync.hasWaiters(c)); |
703 |
|
|
assertEquals(0, sync.getWaitQueueLength(c)); |
704 |
|
|
sync.release(1); |
705 |
|
|
t1.join(SHORT_DELAY_MS); |
706 |
|
|
t2.join(SHORT_DELAY_MS); |
707 |
|
|
assertFalse(t1.isAlive()); |
708 |
|
|
assertFalse(t2.isAlive()); |
709 |
dl |
1.15 |
} |
710 |
|
|
|
711 |
|
|
/** |
712 |
|
|
* getWaitingThreads returns only and all waiting threads |
713 |
|
|
*/ |
714 |
jsr166 |
1.26 |
public void testGetWaitingThreads() throws InterruptedException { |
715 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
716 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
717 |
jsr166 |
1.29 |
Thread t1 = new Thread(new CheckedRunnable() { |
718 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
719 |
|
|
sync.acquire(1); |
720 |
|
|
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
721 |
|
|
c.await(); |
722 |
|
|
sync.release(1); |
723 |
|
|
}}); |
724 |
|
|
|
725 |
jsr166 |
1.29 |
Thread t2 = new Thread(new CheckedRunnable() { |
726 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
727 |
|
|
sync.acquire(1); |
728 |
|
|
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
729 |
|
|
c.await(); |
730 |
|
|
sync.release(1); |
731 |
|
|
}}); |
732 |
dl |
1.1 |
|
733 |
jsr166 |
1.26 |
sync.acquire(1); |
734 |
|
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
735 |
|
|
sync.release(1); |
736 |
|
|
t1.start(); |
737 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
738 |
|
|
t2.start(); |
739 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
740 |
|
|
sync.acquire(1); |
741 |
|
|
assertTrue(sync.hasWaiters(c)); |
742 |
|
|
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
743 |
|
|
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
744 |
|
|
c.signalAll(); |
745 |
|
|
sync.release(1); |
746 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
747 |
|
|
sync.acquire(1); |
748 |
|
|
assertFalse(sync.hasWaiters(c)); |
749 |
|
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
750 |
|
|
sync.release(1); |
751 |
|
|
t1.join(SHORT_DELAY_MS); |
752 |
|
|
t2.join(SHORT_DELAY_MS); |
753 |
|
|
assertFalse(t1.isAlive()); |
754 |
|
|
assertFalse(t2.isAlive()); |
755 |
dl |
1.15 |
} |
756 |
|
|
|
757 |
|
|
|
758 |
|
|
|
759 |
|
|
/** |
760 |
|
|
* awaitUninterruptibly doesn't abort on interrupt |
761 |
|
|
*/ |
762 |
jsr166 |
1.26 |
public void testAwaitUninterruptibly() throws InterruptedException { |
763 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
764 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
765 |
jsr166 |
1.29 |
Thread t = new Thread(new CheckedRunnable() { |
766 |
jsr166 |
1.28 |
public void realRun() { |
767 |
|
|
sync.acquire(1); |
768 |
|
|
c.awaitUninterruptibly(); |
769 |
|
|
sync.release(1); |
770 |
|
|
}}); |
771 |
dl |
1.15 |
|
772 |
jsr166 |
1.26 |
t.start(); |
773 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
774 |
|
|
t.interrupt(); |
775 |
|
|
sync.acquire(1); |
776 |
|
|
c.signal(); |
777 |
|
|
sync.release(1); |
778 |
|
|
t.join(SHORT_DELAY_MS); |
779 |
|
|
assertFalse(t.isAlive()); |
780 |
dl |
1.15 |
} |
781 |
|
|
|
782 |
|
|
/** |
783 |
|
|
* await is interruptible |
784 |
|
|
*/ |
785 |
jsr166 |
1.26 |
public void testAwait_Interrupt() throws InterruptedException { |
786 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
787 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
788 |
jsr166 |
1.29 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
789 |
|
|
public void realRun() throws InterruptedException { |
790 |
jsr166 |
1.28 |
sync.acquire(1); |
791 |
|
|
c.await(); |
792 |
|
|
}}); |
793 |
dl |
1.15 |
|
794 |
jsr166 |
1.26 |
t.start(); |
795 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
796 |
|
|
t.interrupt(); |
797 |
|
|
t.join(SHORT_DELAY_MS); |
798 |
|
|
assertFalse(t.isAlive()); |
799 |
dl |
1.15 |
} |
800 |
|
|
|
801 |
|
|
/** |
802 |
|
|
* awaitNanos is interruptible |
803 |
|
|
*/ |
804 |
jsr166 |
1.26 |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
805 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
806 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
807 |
jsr166 |
1.29 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
808 |
|
|
public void realRun() throws InterruptedException { |
809 |
jsr166 |
1.28 |
sync.acquire(1); |
810 |
jsr166 |
1.33 |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
811 |
jsr166 |
1.28 |
}}); |
812 |
dl |
1.15 |
|
813 |
jsr166 |
1.26 |
t.start(); |
814 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
815 |
|
|
t.interrupt(); |
816 |
|
|
t.join(SHORT_DELAY_MS); |
817 |
|
|
assertFalse(t.isAlive()); |
818 |
dl |
1.15 |
} |
819 |
|
|
|
820 |
|
|
/** |
821 |
|
|
* awaitUntil is interruptible |
822 |
|
|
*/ |
823 |
jsr166 |
1.26 |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
824 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
825 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
826 |
jsr166 |
1.29 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
827 |
|
|
public void realRun() throws InterruptedException { |
828 |
jsr166 |
1.28 |
sync.acquire(1); |
829 |
|
|
java.util.Date d = new java.util.Date(); |
830 |
|
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
831 |
|
|
}}); |
832 |
dl |
1.15 |
|
833 |
jsr166 |
1.26 |
t.start(); |
834 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
835 |
|
|
t.interrupt(); |
836 |
|
|
t.join(SHORT_DELAY_MS); |
837 |
|
|
assertFalse(t.isAlive()); |
838 |
dl |
1.12 |
} |
839 |
|
|
|
840 |
dl |
1.14 |
/** |
841 |
dl |
1.15 |
* signalAll wakes up all threads |
842 |
|
|
*/ |
843 |
jsr166 |
1.26 |
public void testSignalAll() throws InterruptedException { |
844 |
jsr166 |
1.29 |
final Mutex sync = new Mutex(); |
845 |
dl |
1.17 |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
846 |
jsr166 |
1.29 |
Thread t1 = new Thread(new CheckedRunnable() { |
847 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
848 |
|
|
sync.acquire(1); |
849 |
|
|
c.await(); |
850 |
|
|
sync.release(1); |
851 |
|
|
}}); |
852 |
|
|
|
853 |
jsr166 |
1.29 |
Thread t2 = new Thread(new CheckedRunnable() { |
854 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
855 |
|
|
sync.acquire(1); |
856 |
|
|
c.await(); |
857 |
|
|
sync.release(1); |
858 |
|
|
}}); |
859 |
dl |
1.15 |
|
860 |
jsr166 |
1.26 |
t1.start(); |
861 |
|
|
t2.start(); |
862 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
863 |
|
|
sync.acquire(1); |
864 |
|
|
c.signalAll(); |
865 |
|
|
sync.release(1); |
866 |
|
|
t1.join(SHORT_DELAY_MS); |
867 |
|
|
t2.join(SHORT_DELAY_MS); |
868 |
|
|
assertFalse(t1.isAlive()); |
869 |
|
|
assertFalse(t2.isAlive()); |
870 |
dl |
1.15 |
} |
871 |
|
|
|
872 |
|
|
|
873 |
|
|
/** |
874 |
dl |
1.14 |
* toString indicates current state |
875 |
|
|
*/ |
876 |
|
|
public void testToString() { |
877 |
dl |
1.17 |
Mutex sync = new Mutex(); |
878 |
|
|
String us = sync.toString(); |
879 |
dl |
1.14 |
assertTrue(us.indexOf("State = 0") >= 0); |
880 |
dl |
1.17 |
sync.acquire(1); |
881 |
|
|
String ls = sync.toString(); |
882 |
dl |
1.14 |
assertTrue(ls.indexOf("State = 1") >= 0); |
883 |
|
|
} |
884 |
|
|
|
885 |
|
|
/** |
886 |
|
|
* A serialized AQS deserializes with current state |
887 |
|
|
*/ |
888 |
jsr166 |
1.26 |
public void testSerialization() throws Exception { |
889 |
dl |
1.14 |
Mutex l = new Mutex(); |
890 |
dl |
1.16 |
l.acquire(1); |
891 |
|
|
assertTrue(l.isHeldExclusively()); |
892 |
dl |
1.14 |
|
893 |
jsr166 |
1.26 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
894 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
895 |
|
|
out.writeObject(l); |
896 |
|
|
out.close(); |
897 |
|
|
|
898 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
899 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
900 |
|
|
Mutex r = (Mutex) in.readObject(); |
901 |
|
|
assertTrue(r.isHeldExclusively()); |
902 |
dl |
1.14 |
} |
903 |
|
|
|
904 |
dl |
1.12 |
|
905 |
|
|
/** |
906 |
dl |
1.15 |
* tryReleaseShared setting state changes getState |
907 |
dl |
1.12 |
*/ |
908 |
dl |
1.15 |
public void testGetStateWithReleaseShared() { |
909 |
jsr166 |
1.29 |
final BooleanLatch l = new BooleanLatch(); |
910 |
|
|
assertFalse(l.isSignalled()); |
911 |
|
|
l.releaseShared(0); |
912 |
|
|
assertTrue(l.isSignalled()); |
913 |
dl |
1.12 |
} |
914 |
|
|
|
915 |
|
|
/** |
916 |
dl |
1.17 |
* releaseShared has no effect when already signalled |
917 |
dl |
1.12 |
*/ |
918 |
dl |
1.13 |
public void testReleaseShared() { |
919 |
jsr166 |
1.29 |
final BooleanLatch l = new BooleanLatch(); |
920 |
|
|
assertFalse(l.isSignalled()); |
921 |
|
|
l.releaseShared(0); |
922 |
|
|
assertTrue(l.isSignalled()); |
923 |
|
|
l.releaseShared(0); |
924 |
|
|
assertTrue(l.isSignalled()); |
925 |
dl |
1.12 |
} |
926 |
|
|
|
927 |
|
|
/** |
928 |
|
|
* acquireSharedInterruptibly returns after release, but not before |
929 |
|
|
*/ |
930 |
jsr166 |
1.26 |
public void testAcquireSharedInterruptibly() throws InterruptedException { |
931 |
jsr166 |
1.29 |
final BooleanLatch l = new BooleanLatch(); |
932 |
dl |
1.12 |
|
933 |
jsr166 |
1.29 |
Thread t = new Thread(new CheckedRunnable() { |
934 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
935 |
|
|
threadAssertFalse(l.isSignalled()); |
936 |
|
|
l.acquireSharedInterruptibly(0); |
937 |
|
|
threadAssertTrue(l.isSignalled()); |
938 |
|
|
}}); |
939 |
jsr166 |
1.26 |
|
940 |
|
|
t.start(); |
941 |
|
|
assertFalse(l.isSignalled()); |
942 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
943 |
|
|
l.releaseShared(0); |
944 |
|
|
assertTrue(l.isSignalled()); |
945 |
|
|
t.join(); |
946 |
dl |
1.12 |
} |
947 |
jsr166 |
1.22 |
|
948 |
dl |
1.12 |
|
949 |
|
|
/** |
950 |
|
|
* acquireSharedTimed returns after release |
951 |
|
|
*/ |
952 |
jsr166 |
1.26 |
public void testAsquireSharedTimed() throws InterruptedException { |
953 |
jsr166 |
1.29 |
final BooleanLatch l = new BooleanLatch(); |
954 |
dl |
1.12 |
|
955 |
jsr166 |
1.29 |
Thread t = new Thread(new CheckedRunnable() { |
956 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
957 |
jsr166 |
1.33 |
assertFalse(l.isSignalled()); |
958 |
|
|
long nanos = MILLISECONDS.toNanos(MEDIUM_DELAY_MS); |
959 |
|
|
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
960 |
|
|
assertTrue(l.isSignalled()); |
961 |
jsr166 |
1.28 |
}}); |
962 |
jsr166 |
1.26 |
|
963 |
|
|
t.start(); |
964 |
|
|
assertFalse(l.isSignalled()); |
965 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
966 |
|
|
l.releaseShared(0); |
967 |
|
|
assertTrue(l.isSignalled()); |
968 |
|
|
t.join(); |
969 |
dl |
1.12 |
} |
970 |
jsr166 |
1.22 |
|
971 |
dl |
1.12 |
/** |
972 |
|
|
* acquireSharedInterruptibly throws IE if interrupted before released |
973 |
|
|
*/ |
974 |
jsr166 |
1.26 |
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
975 |
dl |
1.12 |
final BooleanLatch l = new BooleanLatch(); |
976 |
jsr166 |
1.29 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
977 |
|
|
public void realRun() throws InterruptedException { |
978 |
jsr166 |
1.28 |
threadAssertFalse(l.isSignalled()); |
979 |
|
|
l.acquireSharedInterruptibly(0); |
980 |
|
|
}}); |
981 |
|
|
|
982 |
jsr166 |
1.29 |
t.start(); |
983 |
jsr166 |
1.26 |
assertFalse(l.isSignalled()); |
984 |
|
|
t.interrupt(); |
985 |
|
|
t.join(); |
986 |
dl |
1.12 |
} |
987 |
|
|
|
988 |
|
|
/** |
989 |
|
|
* acquireSharedTimed throws IE if interrupted before released |
990 |
|
|
*/ |
991 |
jsr166 |
1.26 |
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
992 |
dl |
1.12 |
final BooleanLatch l = new BooleanLatch(); |
993 |
jsr166 |
1.29 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
994 |
|
|
public void realRun() throws InterruptedException { |
995 |
jsr166 |
1.33 |
assertFalse(l.isSignalled()); |
996 |
|
|
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
997 |
|
|
l.tryAcquireSharedNanos(0, nanos); |
998 |
jsr166 |
1.28 |
}}); |
999 |
|
|
|
1000 |
dl |
1.12 |
t.start(); |
1001 |
jsr166 |
1.26 |
Thread.sleep(SHORT_DELAY_MS); |
1002 |
|
|
assertFalse(l.isSignalled()); |
1003 |
|
|
t.interrupt(); |
1004 |
|
|
t.join(); |
1005 |
dl |
1.12 |
} |
1006 |
|
|
|
1007 |
|
|
/** |
1008 |
|
|
* acquireSharedTimed times out if not released before timeout |
1009 |
|
|
*/ |
1010 |
jsr166 |
1.26 |
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1011 |
dl |
1.12 |
final BooleanLatch l = new BooleanLatch(); |
1012 |
jsr166 |
1.29 |
Thread t = new Thread(new CheckedRunnable() { |
1013 |
jsr166 |
1.28 |
public void realRun() throws InterruptedException { |
1014 |
jsr166 |
1.33 |
assertFalse(l.isSignalled()); |
1015 |
|
|
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1016 |
|
|
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1017 |
jsr166 |
1.28 |
}}); |
1018 |
|
|
|
1019 |
dl |
1.12 |
t.start(); |
1020 |
jsr166 |
1.26 |
Thread.sleep(SHORT_DELAY_MS); |
1021 |
|
|
assertFalse(l.isSignalled()); |
1022 |
|
|
t.join(); |
1023 |
dl |
1.1 |
} |
1024 |
dl |
1.14 |
|
1025 |
dl |
1.1 |
} |