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