8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.locks.*; |
10 |
|
import java.util.concurrent.*; |
11 |
+ |
import java.io.*; |
12 |
|
|
13 |
< |
public class ReentrantReadWriteLockTest extends TestCase { |
13 |
< |
static int HOLD_COUNT_TEST_LIMIT = 20; |
14 |
< |
|
13 |
> |
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
|
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
18 |
– |
|
17 |
|
public static Test suite() { |
18 |
|
return new TestSuite(ReentrantReadWriteLockTest.class); |
19 |
|
} |
20 |
|
|
21 |
< |
|
24 |
< |
private static long SHORT_DELAY_MS = 100; |
25 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
26 |
< |
private static long LONG_DELAY_MS = 10000; |
21 |
> |
static int HOLD_COUNT_TEST_LIMIT = 20; |
22 |
|
|
23 |
|
/* |
24 |
|
* Unlocks an unlocked lock, throws Illegal Monitor State |
25 |
|
* |
26 |
|
*/ |
32 |
– |
|
27 |
|
public void testIllegalMonitorStateException(){ |
28 |
|
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
29 |
|
try{ |
30 |
|
rl.writeLock().unlock(); |
31 |
|
fail("Should of thown Illegal Monitor State Exception"); |
32 |
|
|
33 |
< |
}catch(IllegalMonitorStateException sucess){} |
33 |
> |
}catch(IllegalMonitorStateException success){} |
34 |
> |
} |
35 |
|
|
36 |
|
|
42 |
– |
} |
43 |
– |
|
44 |
– |
/* |
45 |
– |
* makes a lock, locks it, tries to aquire the lock in another thread |
46 |
– |
* interrupts that thread and waits for an interrupted Exception to |
47 |
– |
* be thrown. |
48 |
– |
*/ |
37 |
|
|
38 |
|
public void testInterruptedException(){ |
39 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
42 |
|
public void run(){ |
43 |
|
try{ |
44 |
|
lock.writeLock().lockInterruptibly(); |
45 |
< |
fail("should throw"); |
46 |
< |
}catch(InterruptedException sucess){} |
45 |
> |
threadFail("should throw"); |
46 |
> |
}catch(InterruptedException success){} |
47 |
|
} |
48 |
|
}); |
49 |
< |
t.start(); |
50 |
< |
t.interrupt(); |
51 |
< |
lock.writeLock().unlock(); |
49 |
> |
try { |
50 |
> |
t.start(); |
51 |
> |
t.interrupt(); |
52 |
> |
lock.writeLock().unlock(); |
53 |
> |
t.join(); |
54 |
> |
} catch(Exception e){ |
55 |
> |
fail("unexpected exception"); |
56 |
> |
} |
57 |
|
} |
58 |
|
|
66 |
– |
/* |
67 |
– |
* tests for interrupted exception on a timed wait |
68 |
– |
* |
69 |
– |
*/ |
70 |
– |
|
71 |
– |
|
59 |
|
public void testInterruptedException2(){ |
60 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
61 |
|
lock.writeLock().lock(); |
63 |
|
public void run(){ |
64 |
|
try{ |
65 |
|
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
66 |
< |
fail("should throw"); |
67 |
< |
}catch(InterruptedException sucess){} |
66 |
> |
threadFail("should throw"); |
67 |
> |
}catch(InterruptedException success){} |
68 |
> |
} |
69 |
> |
}); |
70 |
> |
try { |
71 |
> |
t.start(); |
72 |
> |
t.interrupt(); |
73 |
> |
lock.writeLock().unlock(); |
74 |
> |
t.join(); |
75 |
> |
} catch(Exception e){ |
76 |
> |
fail("unexpected exception"); |
77 |
> |
} |
78 |
> |
} |
79 |
> |
|
80 |
> |
public void testInterruptedException3(){ |
81 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
82 |
> |
lock.writeLock().lock(); |
83 |
> |
Thread t = new Thread(new Runnable() { |
84 |
> |
public void run(){ |
85 |
> |
try{ |
86 |
> |
lock.readLock().lockInterruptibly(); |
87 |
> |
threadFail("should throw"); |
88 |
> |
}catch(InterruptedException success){} |
89 |
> |
} |
90 |
> |
}); |
91 |
> |
try { |
92 |
> |
t.start(); |
93 |
> |
t.interrupt(); |
94 |
> |
lock.writeLock().unlock(); |
95 |
> |
t.join(); |
96 |
> |
} catch(Exception e){ |
97 |
> |
fail("unexpected exception"); |
98 |
> |
} |
99 |
> |
} |
100 |
> |
|
101 |
> |
public void testInterruptedException4(){ |
102 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
103 |
> |
lock.writeLock().lock(); |
104 |
> |
Thread t = new Thread(new Runnable() { |
105 |
> |
public void run(){ |
106 |
> |
try{ |
107 |
> |
lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS); |
108 |
> |
threadFail("should throw"); |
109 |
> |
}catch(InterruptedException success){} |
110 |
|
} |
111 |
|
}); |
112 |
< |
t.start(); |
113 |
< |
t.interrupt(); |
112 |
> |
try { |
113 |
> |
t.start(); |
114 |
> |
t.interrupt(); |
115 |
> |
t.join(); |
116 |
> |
} catch(Exception e){ |
117 |
> |
fail("unexpected exception"); |
118 |
> |
} |
119 |
|
} |
120 |
|
|
121 |
|
|
122 |
+ |
public void testTryLockWhenLocked() { |
123 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
124 |
+ |
lock.writeLock().lock(); |
125 |
+ |
Thread t = new Thread(new Runnable() { |
126 |
+ |
public void run(){ |
127 |
+ |
threadAssertFalse(lock.writeLock().tryLock()); |
128 |
+ |
} |
129 |
+ |
}); |
130 |
+ |
try { |
131 |
+ |
t.start(); |
132 |
+ |
t.join(); |
133 |
+ |
lock.writeLock().unlock(); |
134 |
+ |
} catch(Exception e){ |
135 |
+ |
fail("unexpected exception"); |
136 |
+ |
} |
137 |
+ |
} |
138 |
|
|
139 |
< |
/* |
140 |
< |
* current thread locks interruptibly the thread |
141 |
< |
* another thread tries to aquire the lock and blocks |
142 |
< |
* on the call. interrupt the attempted aquireLock |
143 |
< |
* assert that the first lock() call actually locked the lock |
144 |
< |
* assert that the current thread is the one holding the lock |
145 |
< |
*/ |
139 |
> |
public void testTryLockWhenLocked2() { |
140 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
141 |
> |
lock.writeLock().lock(); |
142 |
> |
Thread t = new Thread(new Runnable() { |
143 |
> |
public void run(){ |
144 |
> |
threadAssertFalse(lock.readLock().tryLock()); |
145 |
> |
} |
146 |
> |
}); |
147 |
> |
try { |
148 |
> |
t.start(); |
149 |
> |
t.join(); |
150 |
> |
lock.writeLock().unlock(); |
151 |
> |
} catch(Exception e){ |
152 |
> |
fail("unexpected exception"); |
153 |
> |
} |
154 |
> |
} |
155 |
|
|
156 |
< |
public void testLockedInterruptibly() { |
157 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
158 |
< |
try {lock.writeLock().lockInterruptibly();} catch(Exception e) {} |
156 |
> |
public void testMultipleReadLocks() { |
157 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
158 |
> |
lock.readLock().lock(); |
159 |
> |
Thread t = new Thread(new Runnable() { |
160 |
> |
public void run(){ |
161 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
162 |
> |
lock.readLock().unlock(); |
163 |
> |
} |
164 |
> |
}); |
165 |
> |
try { |
166 |
> |
t.start(); |
167 |
> |
t.join(); |
168 |
> |
lock.readLock().unlock(); |
169 |
> |
} catch(Exception e){ |
170 |
> |
fail("unexpected exception"); |
171 |
> |
} |
172 |
> |
} |
173 |
> |
|
174 |
> |
public void testWriteAfterMultipleReadLocks() { |
175 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
176 |
> |
lock.readLock().lock(); |
177 |
> |
Thread t1 = new Thread(new Runnable() { |
178 |
> |
public void run(){ |
179 |
> |
lock.readLock().lock(); |
180 |
> |
lock.readLock().unlock(); |
181 |
> |
} |
182 |
> |
}); |
183 |
> |
Thread t2 = new Thread(new Runnable() { |
184 |
> |
public void run(){ |
185 |
> |
lock.writeLock().lock(); |
186 |
> |
lock.writeLock().unlock(); |
187 |
> |
} |
188 |
> |
}); |
189 |
> |
|
190 |
> |
try { |
191 |
> |
t1.start(); |
192 |
> |
t2.start(); |
193 |
> |
Thread.sleep(SHORT_DELAY_MS); |
194 |
> |
lock.readLock().unlock(); |
195 |
> |
t1.join(MEDIUM_DELAY_MS); |
196 |
> |
t2.join(MEDIUM_DELAY_MS); |
197 |
> |
assertTrue(!t1.isAlive()); |
198 |
> |
assertTrue(!t2.isAlive()); |
199 |
> |
|
200 |
> |
} catch(Exception e){ |
201 |
> |
fail("unexpected exception"); |
202 |
> |
} |
203 |
> |
} |
204 |
> |
|
205 |
> |
public void testReadAfterWriteLock() { |
206 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
207 |
> |
lock.writeLock().lock(); |
208 |
> |
Thread t1 = new Thread(new Runnable() { |
209 |
> |
public void run(){ |
210 |
> |
lock.readLock().lock(); |
211 |
> |
lock.readLock().unlock(); |
212 |
> |
} |
213 |
> |
}); |
214 |
> |
Thread t2 = new Thread(new Runnable() { |
215 |
> |
public void run(){ |
216 |
> |
lock.readLock().lock(); |
217 |
> |
lock.readLock().unlock(); |
218 |
> |
} |
219 |
> |
}); |
220 |
> |
|
221 |
> |
try { |
222 |
> |
t1.start(); |
223 |
> |
t2.start(); |
224 |
> |
Thread.sleep(SHORT_DELAY_MS); |
225 |
> |
lock.writeLock().unlock(); |
226 |
> |
t1.join(MEDIUM_DELAY_MS); |
227 |
> |
t2.join(MEDIUM_DELAY_MS); |
228 |
> |
assertTrue(!t1.isAlive()); |
229 |
> |
assertTrue(!t2.isAlive()); |
230 |
> |
|
231 |
> |
} catch(Exception e){ |
232 |
> |
fail("unexpected exception"); |
233 |
> |
} |
234 |
> |
} |
235 |
> |
|
236 |
> |
|
237 |
> |
public void testTryLockWhenReadLocked() { |
238 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
239 |
> |
lock.readLock().lock(); |
240 |
> |
Thread t = new Thread(new Runnable() { |
241 |
> |
public void run(){ |
242 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
243 |
> |
lock.readLock().unlock(); |
244 |
> |
} |
245 |
> |
}); |
246 |
> |
try { |
247 |
> |
t.start(); |
248 |
> |
t.join(); |
249 |
> |
lock.readLock().unlock(); |
250 |
> |
} catch(Exception e){ |
251 |
> |
fail("unexpected exception"); |
252 |
> |
} |
253 |
> |
} |
254 |
> |
|
255 |
> |
|
256 |
> |
|
257 |
> |
public void testWriteTryLockWhenReadLocked() { |
258 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
259 |
> |
lock.readLock().lock(); |
260 |
> |
Thread t = new Thread(new Runnable() { |
261 |
> |
public void run(){ |
262 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
263 |
> |
} |
264 |
> |
}); |
265 |
> |
try { |
266 |
> |
t.start(); |
267 |
> |
t.join(); |
268 |
> |
lock.readLock().unlock(); |
269 |
> |
} catch(Exception e){ |
270 |
> |
fail("unexpected exception"); |
271 |
> |
} |
272 |
> |
} |
273 |
> |
|
274 |
> |
|
275 |
> |
|
276 |
> |
public void testTryLock_Timeout(){ |
277 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
278 |
> |
lock.writeLock().lock(); |
279 |
> |
Thread t = new Thread(new Runnable() { |
280 |
> |
public void run(){ |
281 |
> |
try { |
282 |
> |
threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
283 |
> |
} catch (Exception ex) { |
284 |
> |
threadFail("unexpected exception"); |
285 |
> |
} |
286 |
> |
} |
287 |
> |
}); |
288 |
> |
try { |
289 |
> |
t.start(); |
290 |
> |
t.join(); |
291 |
> |
lock.writeLock().unlock(); |
292 |
> |
} catch(Exception e){ |
293 |
> |
fail("unexpected exception"); |
294 |
> |
} |
295 |
> |
} |
296 |
> |
|
297 |
> |
public void testTryLock_Timeout2(){ |
298 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
299 |
> |
lock.writeLock().lock(); |
300 |
> |
Thread t = new Thread(new Runnable() { |
301 |
> |
public void run(){ |
302 |
> |
try { |
303 |
> |
threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
304 |
> |
} catch (Exception ex) { |
305 |
> |
threadFail("unexpected exception"); |
306 |
> |
} |
307 |
> |
} |
308 |
> |
}); |
309 |
> |
try { |
310 |
> |
t.start(); |
311 |
> |
t.join(); |
312 |
> |
lock.writeLock().unlock(); |
313 |
> |
} catch(Exception e){ |
314 |
> |
fail("unexpected exception"); |
315 |
> |
} |
316 |
> |
} |
317 |
> |
|
318 |
> |
|
319 |
> |
public void testLockInterruptibly() { |
320 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
321 |
> |
try { |
322 |
> |
lock.writeLock().lockInterruptibly(); |
323 |
> |
} catch(Exception e) { |
324 |
> |
fail("unexpected exception"); |
325 |
> |
} |
326 |
|
Thread t = new Thread(new Runnable() { |
327 |
|
public void run() { |
328 |
|
try { |
329 |
|
lock.writeLock().lockInterruptibly(); |
330 |
< |
fail("Failed to generate an Interrupted Exception"); |
330 |
> |
threadFail("should throw"); |
331 |
|
} |
332 |
< |
catch(InterruptedException e) {} |
332 |
> |
catch(InterruptedException success) { |
333 |
> |
} |
334 |
|
} |
335 |
|
}); |
336 |
< |
t.start(); |
337 |
< |
t.interrupt(); |
336 |
> |
try { |
337 |
> |
t.start(); |
338 |
> |
t.interrupt(); |
339 |
> |
t.join(); |
340 |
> |
lock.writeLock().unlock(); |
341 |
> |
} catch(Exception e){ |
342 |
> |
fail("unexpected exception"); |
343 |
> |
} |
344 |
|
} |
345 |
< |
|
345 |
> |
|
346 |
> |
public void testLockInterruptibly2() { |
347 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
348 |
> |
try { |
349 |
> |
lock.writeLock().lockInterruptibly(); |
350 |
> |
} catch(Exception e) { |
351 |
> |
fail("unexpected exception"); |
352 |
> |
} |
353 |
> |
Thread t = new Thread(new Runnable() { |
354 |
> |
public void run() { |
355 |
> |
try { |
356 |
> |
lock.readLock().lockInterruptibly(); |
357 |
> |
threadFail("should throw"); |
358 |
> |
} |
359 |
> |
catch(InterruptedException success) { |
360 |
> |
} |
361 |
> |
} |
362 |
> |
}); |
363 |
> |
try { |
364 |
> |
t.start(); |
365 |
> |
t.interrupt(); |
366 |
> |
t.join(); |
367 |
> |
lock.writeLock().unlock(); |
368 |
> |
} catch(Exception e){ |
369 |
> |
fail("unexpected exception"); |
370 |
> |
} |
371 |
> |
} |
372 |
> |
|
373 |
> |
public void testAwait_IllegalMonitor() { |
374 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
375 |
> |
final Condition c = lock.writeLock().newCondition(); |
376 |
> |
try { |
377 |
> |
c.await(); |
378 |
> |
fail("should throw"); |
379 |
> |
} |
380 |
> |
catch (IllegalMonitorStateException success) { |
381 |
> |
} |
382 |
> |
catch (Exception ex) { |
383 |
> |
fail("should throw IMSE"); |
384 |
> |
} |
385 |
> |
} |
386 |
> |
|
387 |
> |
public void testSignal_IllegalMonitor() { |
388 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
389 |
> |
final Condition c = lock.writeLock().newCondition(); |
390 |
> |
try { |
391 |
> |
c.signal(); |
392 |
> |
fail("should throw"); |
393 |
> |
} |
394 |
> |
catch (IllegalMonitorStateException success) { |
395 |
> |
} |
396 |
> |
catch (Exception ex) { |
397 |
> |
fail("should throw IMSE"); |
398 |
> |
} |
399 |
> |
} |
400 |
> |
|
401 |
> |
public void testAwaitNanos_Timeout() { |
402 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
403 |
> |
final Condition c = lock.writeLock().newCondition(); |
404 |
> |
try { |
405 |
> |
lock.writeLock().lock(); |
406 |
> |
long t = c.awaitNanos(100); |
407 |
> |
assertTrue(t <= 0); |
408 |
> |
lock.writeLock().unlock(); |
409 |
> |
} |
410 |
> |
catch (Exception ex) { |
411 |
> |
fail("unexpected exception"); |
412 |
> |
} |
413 |
> |
} |
414 |
> |
|
415 |
> |
public void testAwait_Timeout() { |
416 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
417 |
> |
final Condition c = lock.writeLock().newCondition(); |
418 |
> |
try { |
419 |
> |
lock.writeLock().lock(); |
420 |
> |
assertFalse(c.await(10, TimeUnit.MILLISECONDS)); |
421 |
> |
lock.writeLock().unlock(); |
422 |
> |
} |
423 |
> |
catch (Exception ex) { |
424 |
> |
fail("unexpected exception"); |
425 |
> |
} |
426 |
> |
} |
427 |
> |
|
428 |
> |
public void testAwaitUntil_Timeout() { |
429 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
430 |
> |
final Condition c = lock.writeLock().newCondition(); |
431 |
> |
try { |
432 |
> |
lock.writeLock().lock(); |
433 |
> |
java.util.Date d = new java.util.Date(); |
434 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
435 |
> |
lock.writeLock().unlock(); |
436 |
> |
} |
437 |
> |
catch (Exception ex) { |
438 |
> |
fail("unexpected exception"); |
439 |
> |
} |
440 |
> |
} |
441 |
> |
|
442 |
> |
public void testAwait() { |
443 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
444 |
> |
final Condition c = lock.writeLock().newCondition(); |
445 |
> |
Thread t = new Thread(new Runnable() { |
446 |
> |
public void run() { |
447 |
> |
try { |
448 |
> |
lock.writeLock().lock(); |
449 |
> |
c.await(); |
450 |
> |
lock.writeLock().unlock(); |
451 |
> |
} |
452 |
> |
catch(InterruptedException e) { |
453 |
> |
threadFail("unexpected exception"); |
454 |
> |
} |
455 |
> |
} |
456 |
> |
}); |
457 |
> |
|
458 |
> |
try { |
459 |
> |
t.start(); |
460 |
> |
Thread.sleep(SHORT_DELAY_MS); |
461 |
> |
lock.writeLock().lock(); |
462 |
> |
c.signal(); |
463 |
> |
lock.writeLock().unlock(); |
464 |
> |
t.join(SHORT_DELAY_MS); |
465 |
> |
assertFalse(t.isAlive()); |
466 |
> |
} |
467 |
> |
catch (Exception ex) { |
468 |
> |
fail("unexpected exception"); |
469 |
> |
} |
470 |
> |
} |
471 |
> |
|
472 |
> |
public void testAwaitUninterruptibly() { |
473 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
474 |
> |
final Condition c = lock.writeLock().newCondition(); |
475 |
> |
Thread t = new Thread(new Runnable() { |
476 |
> |
public void run() { |
477 |
> |
lock.writeLock().lock(); |
478 |
> |
c.awaitUninterruptibly(); |
479 |
> |
lock.writeLock().unlock(); |
480 |
> |
} |
481 |
> |
}); |
482 |
> |
|
483 |
> |
try { |
484 |
> |
t.start(); |
485 |
> |
Thread.sleep(SHORT_DELAY_MS); |
486 |
> |
t.interrupt(); |
487 |
> |
lock.writeLock().lock(); |
488 |
> |
c.signal(); |
489 |
> |
lock.writeLock().unlock(); |
490 |
> |
t.join(SHORT_DELAY_MS); |
491 |
> |
assertFalse(t.isAlive()); |
492 |
> |
} |
493 |
> |
catch (Exception ex) { |
494 |
> |
fail("unexpected exception"); |
495 |
> |
} |
496 |
> |
} |
497 |
> |
|
498 |
> |
public void testAwait_Interrupt() { |
499 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
500 |
> |
final Condition c = lock.writeLock().newCondition(); |
501 |
> |
Thread t = new Thread(new Runnable() { |
502 |
> |
public void run() { |
503 |
> |
try { |
504 |
> |
lock.writeLock().lock(); |
505 |
> |
c.await(); |
506 |
> |
lock.writeLock().unlock(); |
507 |
> |
threadFail("should throw"); |
508 |
> |
} |
509 |
> |
catch(InterruptedException success) { |
510 |
> |
} |
511 |
> |
} |
512 |
> |
}); |
513 |
> |
|
514 |
> |
try { |
515 |
> |
t.start(); |
516 |
> |
Thread.sleep(SHORT_DELAY_MS); |
517 |
> |
t.interrupt(); |
518 |
> |
t.join(SHORT_DELAY_MS); |
519 |
> |
assertFalse(t.isAlive()); |
520 |
> |
} |
521 |
> |
catch (Exception ex) { |
522 |
> |
fail("unexpected exception"); |
523 |
> |
} |
524 |
> |
} |
525 |
> |
|
526 |
> |
public void testAwaitNanos_Interrupt() { |
527 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
528 |
> |
final Condition c = lock.writeLock().newCondition(); |
529 |
> |
Thread t = new Thread(new Runnable() { |
530 |
> |
public void run() { |
531 |
> |
try { |
532 |
> |
lock.writeLock().lock(); |
533 |
> |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
534 |
> |
lock.writeLock().unlock(); |
535 |
> |
threadFail("should throw"); |
536 |
> |
} |
537 |
> |
catch(InterruptedException success) { |
538 |
> |
} |
539 |
> |
} |
540 |
> |
}); |
541 |
> |
|
542 |
> |
try { |
543 |
> |
t.start(); |
544 |
> |
Thread.sleep(SHORT_DELAY_MS); |
545 |
> |
t.interrupt(); |
546 |
> |
t.join(SHORT_DELAY_MS); |
547 |
> |
assertFalse(t.isAlive()); |
548 |
> |
} |
549 |
> |
catch (Exception ex) { |
550 |
> |
fail("unexpected exception"); |
551 |
> |
} |
552 |
> |
} |
553 |
> |
|
554 |
> |
public void testAwaitUntil_Interrupt() { |
555 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
556 |
> |
final Condition c = lock.writeLock().newCondition(); |
557 |
> |
Thread t = new Thread(new Runnable() { |
558 |
> |
public void run() { |
559 |
> |
try { |
560 |
> |
lock.writeLock().lock(); |
561 |
> |
java.util.Date d = new java.util.Date(); |
562 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
563 |
> |
lock.writeLock().unlock(); |
564 |
> |
threadFail("should throw"); |
565 |
> |
} |
566 |
> |
catch(InterruptedException success) { |
567 |
> |
} |
568 |
> |
} |
569 |
> |
}); |
570 |
> |
|
571 |
> |
try { |
572 |
> |
t.start(); |
573 |
> |
Thread.sleep(SHORT_DELAY_MS); |
574 |
> |
t.interrupt(); |
575 |
> |
t.join(SHORT_DELAY_MS); |
576 |
> |
assertFalse(t.isAlive()); |
577 |
> |
} |
578 |
> |
catch (Exception ex) { |
579 |
> |
fail("unexpected exception"); |
580 |
> |
} |
581 |
> |
} |
582 |
> |
|
583 |
> |
public void testSignalAll() { |
584 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
585 |
> |
final Condition c = lock.writeLock().newCondition(); |
586 |
> |
Thread t1 = new Thread(new Runnable() { |
587 |
> |
public void run() { |
588 |
> |
try { |
589 |
> |
lock.writeLock().lock(); |
590 |
> |
c.await(); |
591 |
> |
lock.writeLock().unlock(); |
592 |
> |
} |
593 |
> |
catch(InterruptedException e) { |
594 |
> |
threadFail("unexpected exception"); |
595 |
> |
} |
596 |
> |
} |
597 |
> |
}); |
598 |
> |
|
599 |
> |
Thread t2 = new Thread(new Runnable() { |
600 |
> |
public void run() { |
601 |
> |
try { |
602 |
> |
lock.writeLock().lock(); |
603 |
> |
c.await(); |
604 |
> |
lock.writeLock().unlock(); |
605 |
> |
} |
606 |
> |
catch(InterruptedException e) { |
607 |
> |
threadFail("unexpected exception"); |
608 |
> |
} |
609 |
> |
} |
610 |
> |
}); |
611 |
> |
|
612 |
> |
try { |
613 |
> |
t1.start(); |
614 |
> |
t2.start(); |
615 |
> |
Thread.sleep(SHORT_DELAY_MS); |
616 |
> |
lock.writeLock().lock(); |
617 |
> |
c.signalAll(); |
618 |
> |
lock.writeLock().unlock(); |
619 |
> |
t1.join(SHORT_DELAY_MS); |
620 |
> |
t2.join(SHORT_DELAY_MS); |
621 |
> |
assertFalse(t1.isAlive()); |
622 |
> |
assertFalse(t2.isAlive()); |
623 |
> |
} |
624 |
> |
catch (Exception ex) { |
625 |
> |
fail("unexpected exception"); |
626 |
> |
} |
627 |
> |
} |
628 |
> |
|
629 |
> |
public void testSerialization() { |
630 |
> |
ReentrantReadWriteLock l = new ReentrantReadWriteLock(); |
631 |
> |
l.readLock().lock(); |
632 |
> |
l.readLock().unlock(); |
633 |
> |
|
634 |
> |
try { |
635 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
636 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
637 |
> |
out.writeObject(l); |
638 |
> |
out.close(); |
639 |
> |
|
640 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
641 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
642 |
> |
ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject(); |
643 |
> |
r.readLock().lock(); |
644 |
> |
r.readLock().unlock(); |
645 |
> |
} catch(Exception e){ |
646 |
> |
e.printStackTrace(); |
647 |
> |
fail("unexpected exception"); |
648 |
> |
} |
649 |
> |
} |
650 |
> |
|
651 |
|
|
652 |
|
} |