1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
20 |
|
} |
21 |
|
|
22 |
|
/** |
23 |
+ |
* Subclass to expose protected methods |
24 |
+ |
*/ |
25 |
+ |
static class PublicSemaphore extends Semaphore { |
26 |
+ |
PublicSemaphore(int p, boolean f) { super(p, f); } |
27 |
+ |
public Collection<Thread> getQueuedThreads() { |
28 |
+ |
return super.getQueuedThreads(); |
29 |
+ |
} |
30 |
+ |
public void reducePermits(int p) { |
31 |
+ |
super.reducePermits(p); |
32 |
+ |
} |
33 |
+ |
} |
34 |
+ |
|
35 |
+ |
/** |
36 |
+ |
* A runnable calling acquire |
37 |
+ |
*/ |
38 |
+ |
class InterruptibleLockRunnable implements Runnable { |
39 |
+ |
final Semaphore lock; |
40 |
+ |
InterruptibleLockRunnable(Semaphore l) { lock = l; } |
41 |
+ |
public void run() { |
42 |
+ |
try { |
43 |
+ |
lock.acquire(); |
44 |
+ |
} catch(InterruptedException success){} |
45 |
+ |
} |
46 |
+ |
} |
47 |
+ |
|
48 |
+ |
|
49 |
+ |
/** |
50 |
+ |
* A runnable calling acquire that expects to be |
51 |
+ |
* interrupted |
52 |
+ |
*/ |
53 |
+ |
class InterruptedLockRunnable implements Runnable { |
54 |
+ |
final Semaphore lock; |
55 |
+ |
InterruptedLockRunnable(Semaphore l) { lock = l; } |
56 |
+ |
public void run() { |
57 |
+ |
try { |
58 |
+ |
lock.acquire(); |
59 |
+ |
threadShouldThrow(); |
60 |
+ |
} catch(InterruptedException success){} |
61 |
+ |
} |
62 |
+ |
} |
63 |
+ |
|
64 |
+ |
/** |
65 |
|
* Zero, negative, and positive initial values are allowed in constructor |
66 |
|
*/ |
67 |
|
public void testConstructor() { |
68 |
< |
Semaphore s0 = new Semaphore(0); |
68 |
> |
Semaphore s0 = new Semaphore(0, false); |
69 |
|
assertEquals(0, s0.availablePermits()); |
70 |
< |
Semaphore s1 = new Semaphore(-1); |
70 |
> |
assertFalse(s0.isFair()); |
71 |
> |
Semaphore s1 = new Semaphore(-1, false); |
72 |
|
assertEquals(-1, s1.availablePermits()); |
73 |
< |
Semaphore s2 = new Semaphore(-1); |
73 |
> |
Semaphore s2 = new Semaphore(-1, false); |
74 |
|
assertEquals(-1, s2.availablePermits()); |
75 |
|
} |
76 |
|
|
78 |
|
* tryAcquire succeeds when sufficent permits, else fails |
79 |
|
*/ |
80 |
|
public void testTryAcquireInSameThread() { |
81 |
< |
Semaphore s = new Semaphore(2); |
81 |
> |
Semaphore s = new Semaphore(2, false); |
82 |
|
assertEquals(2, s.availablePermits()); |
83 |
|
assertTrue(s.tryAcquire()); |
84 |
|
assertTrue(s.tryAcquire()); |
90 |
|
* Acquire and release of semaphore succeed if initially available |
91 |
|
*/ |
92 |
|
public void testAcquireReleaseInSameThread() { |
93 |
< |
Semaphore s = new Semaphore(1); |
93 |
> |
Semaphore s = new Semaphore(1, false); |
94 |
|
try { |
95 |
|
s.acquire(); |
96 |
|
s.release(); |
113 |
|
* initially available |
114 |
|
*/ |
115 |
|
public void testAcquireUninterruptiblyReleaseInSameThread() { |
116 |
< |
Semaphore s = new Semaphore(1); |
116 |
> |
Semaphore s = new Semaphore(1, false); |
117 |
|
try { |
118 |
|
s.acquireUninterruptibly(); |
119 |
|
s.release(); |
135 |
|
* initially available |
136 |
|
*/ |
137 |
|
public void testTimedAcquireReleaseInSameThread() { |
138 |
< |
Semaphore s = new Semaphore(1); |
138 |
> |
Semaphore s = new Semaphore(1, false); |
139 |
|
try { |
140 |
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
141 |
|
s.release(); |
157 |
|
* A release in one thread enables an acquire in another thread |
158 |
|
*/ |
159 |
|
public void testAcquireReleaseInDifferentThreads() { |
160 |
< |
final Semaphore s = new Semaphore(0); |
160 |
> |
final Semaphore s = new Semaphore(0, false); |
161 |
|
Thread t = new Thread(new Runnable() { |
162 |
|
public void run() { |
163 |
|
try { |
188 |
|
* A release in one thread enables an uninterruptible acquire in another thread |
189 |
|
*/ |
190 |
|
public void testUninterruptibleAcquireReleaseInDifferentThreads() { |
191 |
< |
final Semaphore s = new Semaphore(0); |
191 |
> |
final Semaphore s = new Semaphore(0, false); |
192 |
|
Thread t = new Thread(new Runnable() { |
193 |
|
public void run() { |
194 |
|
s.acquireUninterruptibly(); |
216 |
|
* A release in one thread enables a timed acquire in another thread |
217 |
|
*/ |
218 |
|
public void testTimedAcquireReleaseInDifferentThreads() { |
219 |
< |
final Semaphore s = new Semaphore(1); |
219 |
> |
final Semaphore s = new Semaphore(1, false); |
220 |
|
Thread t = new Thread(new Runnable() { |
221 |
|
public void run() { |
222 |
|
try { |
247 |
|
* A waiting acquire blocks interruptibly |
248 |
|
*/ |
249 |
|
public void testAcquire_InterruptedException() { |
250 |
< |
final Semaphore s = new Semaphore(0); |
250 |
> |
final Semaphore s = new Semaphore(0, false); |
251 |
|
Thread t = new Thread(new Runnable() { |
252 |
|
public void run() { |
253 |
|
try { |
270 |
|
* A waiting timed acquire blocks interruptibly |
271 |
|
*/ |
272 |
|
public void testTryAcquire_InterruptedException() { |
273 |
< |
final Semaphore s = new Semaphore(0); |
273 |
> |
final Semaphore s = new Semaphore(0, false); |
274 |
|
Thread t = new Thread(new Runnable() { |
275 |
|
public void run() { |
276 |
|
try { |
291 |
|
} |
292 |
|
|
293 |
|
/** |
294 |
+ |
* getQueueLength reports number of waiting threads |
295 |
+ |
*/ |
296 |
+ |
public void testGetQueueLength() { |
297 |
+ |
final Semaphore lock = new Semaphore(1, false); |
298 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
299 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
300 |
+ |
try { |
301 |
+ |
assertEquals(0, lock.getQueueLength()); |
302 |
+ |
lock.acquireUninterruptibly(); |
303 |
+ |
t1.start(); |
304 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
305 |
+ |
assertEquals(1, lock.getQueueLength()); |
306 |
+ |
t2.start(); |
307 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
308 |
+ |
assertEquals(2, lock.getQueueLength()); |
309 |
+ |
t1.interrupt(); |
310 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
311 |
+ |
assertEquals(1, lock.getQueueLength()); |
312 |
+ |
lock.release(); |
313 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
314 |
+ |
assertEquals(0, lock.getQueueLength()); |
315 |
+ |
t1.join(); |
316 |
+ |
t2.join(); |
317 |
+ |
} catch(Exception e){ |
318 |
+ |
unexpectedException(); |
319 |
+ |
} |
320 |
+ |
} |
321 |
+ |
|
322 |
+ |
/** |
323 |
+ |
* getQueuedThreads includes waiting threads |
324 |
+ |
*/ |
325 |
+ |
public void testGetQueuedThreads() { |
326 |
+ |
final PublicSemaphore lock = new PublicSemaphore(1, false); |
327 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
328 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
329 |
+ |
try { |
330 |
+ |
assertTrue(lock.getQueuedThreads().isEmpty()); |
331 |
+ |
lock.acquireUninterruptibly(); |
332 |
+ |
assertTrue(lock.getQueuedThreads().isEmpty()); |
333 |
+ |
t1.start(); |
334 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
335 |
+ |
assertTrue(lock.getQueuedThreads().contains(t1)); |
336 |
+ |
t2.start(); |
337 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
338 |
+ |
assertTrue(lock.getQueuedThreads().contains(t1)); |
339 |
+ |
assertTrue(lock.getQueuedThreads().contains(t2)); |
340 |
+ |
t1.interrupt(); |
341 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
342 |
+ |
assertFalse(lock.getQueuedThreads().contains(t1)); |
343 |
+ |
assertTrue(lock.getQueuedThreads().contains(t2)); |
344 |
+ |
lock.release(); |
345 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
346 |
+ |
assertTrue(lock.getQueuedThreads().isEmpty()); |
347 |
+ |
t1.join(); |
348 |
+ |
t2.join(); |
349 |
+ |
} catch(Exception e){ |
350 |
+ |
unexpectedException(); |
351 |
+ |
} |
352 |
+ |
} |
353 |
+ |
|
354 |
+ |
|
355 |
+ |
/** |
356 |
+ |
* reducePermits reduces number of permits |
357 |
+ |
*/ |
358 |
+ |
public void testReducePermits() { |
359 |
+ |
PublicSemaphore s = new PublicSemaphore(10, false); |
360 |
+ |
assertEquals(10, s.availablePermits()); |
361 |
+ |
s.reducePermits(1); |
362 |
+ |
assertEquals(9, s.availablePermits()); |
363 |
+ |
s.reducePermits(10); |
364 |
+ |
assertEquals(-1, s.availablePermits()); |
365 |
+ |
} |
366 |
+ |
|
367 |
+ |
/** |
368 |
|
* a deserialized serialized semaphore has same number of permits |
369 |
|
*/ |
370 |
|
public void testSerialization() { |
371 |
< |
Semaphore l = new Semaphore(3); |
371 |
> |
Semaphore l = new Semaphore(3, false); |
372 |
> |
try { |
373 |
> |
l.acquire(); |
374 |
> |
l.release(); |
375 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
376 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
377 |
> |
out.writeObject(l); |
378 |
> |
out.close(); |
379 |
> |
|
380 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
381 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
382 |
> |
Semaphore r = (Semaphore) in.readObject(); |
383 |
> |
assertEquals(3, r.availablePermits()); |
384 |
> |
assertFalse(r.isFair()); |
385 |
> |
r.acquire(); |
386 |
> |
r.release(); |
387 |
> |
} catch(Exception e){ |
388 |
> |
unexpectedException(); |
389 |
> |
} |
390 |
> |
} |
391 |
> |
|
392 |
> |
|
393 |
> |
/** |
394 |
> |
* Zero, negative, and positive initial values are allowed in constructor |
395 |
> |
*/ |
396 |
> |
public void testConstructor_fair() { |
397 |
> |
Semaphore s0 = new Semaphore(0, true); |
398 |
> |
assertEquals(0, s0.availablePermits()); |
399 |
> |
assertTrue(s0.isFair()); |
400 |
> |
Semaphore s1 = new Semaphore(-1, true); |
401 |
> |
assertEquals(-1, s1.availablePermits()); |
402 |
> |
Semaphore s2 = new Semaphore(-1, true); |
403 |
> |
assertEquals(-1, s2.availablePermits()); |
404 |
> |
} |
405 |
> |
|
406 |
> |
/** |
407 |
> |
* tryAcquire succeeds when sufficent permits, else fails |
408 |
> |
*/ |
409 |
> |
public void testTryAcquireInSameThread_fair() { |
410 |
> |
Semaphore s = new Semaphore(2, true); |
411 |
> |
assertEquals(2, s.availablePermits()); |
412 |
> |
assertTrue(s.tryAcquire()); |
413 |
> |
assertTrue(s.tryAcquire()); |
414 |
> |
assertEquals(0, s.availablePermits()); |
415 |
> |
assertFalse(s.tryAcquire()); |
416 |
> |
} |
417 |
> |
|
418 |
> |
/** |
419 |
> |
* tryAcquire(n) succeeds when sufficent permits, else fails |
420 |
> |
*/ |
421 |
> |
public void testTryAcquireNInSameThread_fair() { |
422 |
> |
Semaphore s = new Semaphore(2, true); |
423 |
> |
assertEquals(2, s.availablePermits()); |
424 |
> |
assertTrue(s.tryAcquire(2)); |
425 |
> |
assertEquals(0, s.availablePermits()); |
426 |
> |
assertFalse(s.tryAcquire()); |
427 |
> |
} |
428 |
> |
|
429 |
> |
/** |
430 |
> |
* Acquire and release of semaphore succeed if initially available |
431 |
> |
*/ |
432 |
> |
public void testAcquireReleaseInSameThread_fair() { |
433 |
> |
Semaphore s = new Semaphore(1, true); |
434 |
> |
try { |
435 |
> |
s.acquire(); |
436 |
> |
s.release(); |
437 |
> |
s.acquire(); |
438 |
> |
s.release(); |
439 |
> |
s.acquire(); |
440 |
> |
s.release(); |
441 |
> |
s.acquire(); |
442 |
> |
s.release(); |
443 |
> |
s.acquire(); |
444 |
> |
s.release(); |
445 |
> |
assertEquals(1, s.availablePermits()); |
446 |
> |
} catch( InterruptedException e){ |
447 |
> |
unexpectedException(); |
448 |
> |
} |
449 |
> |
} |
450 |
> |
|
451 |
> |
/** |
452 |
> |
* Acquire(n) and release(n) of semaphore succeed if initially available |
453 |
> |
*/ |
454 |
> |
public void testAcquireReleaseNInSameThread_fair() { |
455 |
> |
Semaphore s = new Semaphore(1, true); |
456 |
> |
try { |
457 |
> |
s.release(1); |
458 |
> |
s.acquire(1); |
459 |
> |
s.release(2); |
460 |
> |
s.acquire(2); |
461 |
> |
s.release(3); |
462 |
> |
s.acquire(3); |
463 |
> |
s.release(4); |
464 |
> |
s.acquire(4); |
465 |
> |
s.release(5); |
466 |
> |
s.acquire(5); |
467 |
> |
assertEquals(1, s.availablePermits()); |
468 |
> |
} catch( InterruptedException e){ |
469 |
> |
unexpectedException(); |
470 |
> |
} |
471 |
> |
} |
472 |
> |
|
473 |
> |
/** |
474 |
> |
* Acquire(n) and release(n) of semaphore succeed if initially available |
475 |
> |
*/ |
476 |
> |
public void testAcquireUninterruptiblyReleaseNInSameThread_fair() { |
477 |
> |
Semaphore s = new Semaphore(1, true); |
478 |
> |
try { |
479 |
> |
s.release(1); |
480 |
> |
s.acquireUninterruptibly(1); |
481 |
> |
s.release(2); |
482 |
> |
s.acquireUninterruptibly(2); |
483 |
> |
s.release(3); |
484 |
> |
s.acquireUninterruptibly(3); |
485 |
> |
s.release(4); |
486 |
> |
s.acquireUninterruptibly(4); |
487 |
> |
s.release(5); |
488 |
> |
s.acquireUninterruptibly(5); |
489 |
> |
assertEquals(1, s.availablePermits()); |
490 |
> |
} finally { |
491 |
> |
} |
492 |
> |
} |
493 |
> |
|
494 |
> |
/** |
495 |
> |
* release(n) in one thread enables timed acquire(n) in another thread |
496 |
> |
*/ |
497 |
> |
public void testTimedAcquireReleaseNInSameThread_fair() { |
498 |
> |
Semaphore s = new Semaphore(1, true); |
499 |
> |
try { |
500 |
> |
s.release(1); |
501 |
> |
assertTrue(s.tryAcquire(1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
502 |
> |
s.release(2); |
503 |
> |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
504 |
> |
s.release(3); |
505 |
> |
assertTrue(s.tryAcquire(3, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
506 |
> |
s.release(4); |
507 |
> |
assertTrue(s.tryAcquire(4, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
508 |
> |
s.release(5); |
509 |
> |
assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
510 |
> |
assertEquals(1, s.availablePermits()); |
511 |
> |
} catch( InterruptedException e){ |
512 |
> |
unexpectedException(); |
513 |
> |
} |
514 |
> |
} |
515 |
> |
|
516 |
> |
/** |
517 |
> |
* release in one thread enables timed acquire in another thread |
518 |
> |
*/ |
519 |
> |
public void testTimedAcquireReleaseInSameThread_fair() { |
520 |
> |
Semaphore s = new Semaphore(1, true); |
521 |
> |
try { |
522 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
523 |
> |
s.release(); |
524 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
525 |
> |
s.release(); |
526 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
527 |
> |
s.release(); |
528 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
529 |
> |
s.release(); |
530 |
> |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
531 |
> |
s.release(); |
532 |
> |
assertEquals(1, s.availablePermits()); |
533 |
> |
} catch( InterruptedException e){ |
534 |
> |
unexpectedException(); |
535 |
> |
} |
536 |
> |
} |
537 |
> |
|
538 |
> |
/** |
539 |
> |
* A release in one thread enables an acquire in another thread |
540 |
> |
*/ |
541 |
> |
public void testAcquireReleaseInDifferentThreads_fair() { |
542 |
> |
final Semaphore s = new Semaphore(0, true); |
543 |
> |
Thread t = new Thread(new Runnable() { |
544 |
> |
public void run() { |
545 |
> |
try { |
546 |
> |
s.acquire(); |
547 |
> |
s.acquire(); |
548 |
> |
s.acquire(); |
549 |
> |
s.acquire(); |
550 |
> |
} catch(InterruptedException ie){ |
551 |
> |
threadUnexpectedException(); |
552 |
> |
} |
553 |
> |
} |
554 |
> |
}); |
555 |
> |
try { |
556 |
> |
t.start(); |
557 |
> |
Thread.sleep(SHORT_DELAY_MS); |
558 |
> |
s.release(); |
559 |
> |
s.release(); |
560 |
> |
s.release(); |
561 |
> |
s.release(); |
562 |
> |
s.release(); |
563 |
> |
s.release(); |
564 |
> |
t.join(); |
565 |
> |
assertEquals(2, s.availablePermits()); |
566 |
> |
} catch( InterruptedException e){ |
567 |
> |
unexpectedException(); |
568 |
> |
} |
569 |
> |
} |
570 |
> |
|
571 |
> |
/** |
572 |
> |
* release(n) in one thread enables acquire(n) in another thread |
573 |
> |
*/ |
574 |
> |
public void testAcquireReleaseNInDifferentThreads_fair() { |
575 |
> |
final Semaphore s = new Semaphore(0, true); |
576 |
> |
Thread t = new Thread(new Runnable() { |
577 |
> |
public void run() { |
578 |
> |
try { |
579 |
> |
s.acquire(2); |
580 |
> |
s.acquire(2); |
581 |
> |
s.release(4); |
582 |
> |
} catch(InterruptedException ie){ |
583 |
> |
threadUnexpectedException(); |
584 |
> |
} |
585 |
> |
} |
586 |
> |
}); |
587 |
> |
try { |
588 |
> |
t.start(); |
589 |
> |
Thread.sleep(SHORT_DELAY_MS); |
590 |
> |
s.release(6); |
591 |
> |
s.acquire(2); |
592 |
> |
s.acquire(2); |
593 |
> |
s.release(2); |
594 |
> |
t.join(); |
595 |
> |
} catch( InterruptedException e){ |
596 |
> |
unexpectedException(); |
597 |
> |
} |
598 |
> |
} |
599 |
> |
|
600 |
> |
|
601 |
> |
|
602 |
> |
/** |
603 |
> |
* release in one thread enables timed acquire in another thread |
604 |
> |
*/ |
605 |
> |
public void testTimedAcquireReleaseInDifferentThreads_fair() { |
606 |
> |
final Semaphore s = new Semaphore(1, true); |
607 |
> |
Thread t = new Thread(new Runnable() { |
608 |
> |
public void run() { |
609 |
> |
try { |
610 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
611 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
612 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
613 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
614 |
> |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
615 |
> |
|
616 |
> |
} catch(InterruptedException ie){ |
617 |
> |
threadUnexpectedException(); |
618 |
> |
} |
619 |
> |
} |
620 |
> |
}); |
621 |
> |
t.start(); |
622 |
> |
try { |
623 |
> |
s.release(); |
624 |
> |
s.release(); |
625 |
> |
s.release(); |
626 |
> |
s.release(); |
627 |
> |
s.release(); |
628 |
> |
t.join(); |
629 |
> |
} catch( InterruptedException e){ |
630 |
> |
unexpectedException(); |
631 |
> |
} |
632 |
> |
} |
633 |
> |
|
634 |
> |
/** |
635 |
> |
* release(n) in one thread enables timed acquire(n) in another thread |
636 |
> |
*/ |
637 |
> |
public void testTimedAcquireReleaseNInDifferentThreads_fair() { |
638 |
> |
final Semaphore s = new Semaphore(2, true); |
639 |
> |
Thread t = new Thread(new Runnable() { |
640 |
> |
public void run() { |
641 |
> |
try { |
642 |
> |
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
643 |
> |
s.release(2); |
644 |
> |
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
645 |
> |
s.release(2); |
646 |
> |
} catch(InterruptedException ie){ |
647 |
> |
threadUnexpectedException(); |
648 |
> |
} |
649 |
> |
} |
650 |
> |
}); |
651 |
> |
t.start(); |
652 |
> |
try { |
653 |
> |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
654 |
> |
s.release(2); |
655 |
> |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
656 |
> |
s.release(2); |
657 |
> |
t.join(); |
658 |
> |
} catch( InterruptedException e){ |
659 |
> |
unexpectedException(); |
660 |
> |
} |
661 |
> |
} |
662 |
> |
|
663 |
> |
/** |
664 |
> |
* A waiting acquire blocks interruptibly |
665 |
> |
*/ |
666 |
> |
public void testAcquire_InterruptedException_fair() { |
667 |
> |
final Semaphore s = new Semaphore(0, true); |
668 |
> |
Thread t = new Thread(new Runnable() { |
669 |
> |
public void run() { |
670 |
> |
try { |
671 |
> |
s.acquire(); |
672 |
> |
threadShouldThrow(); |
673 |
> |
} catch(InterruptedException success){} |
674 |
> |
} |
675 |
> |
}); |
676 |
> |
t.start(); |
677 |
> |
try { |
678 |
> |
Thread.sleep(SHORT_DELAY_MS); |
679 |
> |
t.interrupt(); |
680 |
> |
t.join(); |
681 |
> |
} catch(InterruptedException e){ |
682 |
> |
unexpectedException(); |
683 |
> |
} |
684 |
> |
} |
685 |
> |
|
686 |
> |
/** |
687 |
> |
* A waiting acquire(n) blocks interruptibly |
688 |
> |
*/ |
689 |
> |
public void testAcquireN_InterruptedException_fair() { |
690 |
> |
final Semaphore s = new Semaphore(2, true); |
691 |
> |
Thread t = new Thread(new Runnable() { |
692 |
> |
public void run() { |
693 |
> |
try { |
694 |
> |
s.acquire(3); |
695 |
> |
threadShouldThrow(); |
696 |
> |
} catch(InterruptedException success){} |
697 |
> |
} |
698 |
> |
}); |
699 |
> |
t.start(); |
700 |
> |
try { |
701 |
> |
Thread.sleep(SHORT_DELAY_MS); |
702 |
> |
t.interrupt(); |
703 |
> |
t.join(); |
704 |
> |
} catch(InterruptedException e){ |
705 |
> |
unexpectedException(); |
706 |
> |
} |
707 |
> |
} |
708 |
> |
|
709 |
> |
/** |
710 |
> |
* A waiting tryAcquire blocks interruptibly |
711 |
> |
*/ |
712 |
> |
public void testTryAcquire_InterruptedException_fair() { |
713 |
> |
final Semaphore s = new Semaphore(0, true); |
714 |
> |
Thread t = new Thread(new Runnable() { |
715 |
> |
public void run() { |
716 |
> |
try { |
717 |
> |
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
718 |
> |
threadShouldThrow(); |
719 |
> |
} catch(InterruptedException success){ |
720 |
> |
} |
721 |
> |
} |
722 |
> |
}); |
723 |
> |
t.start(); |
724 |
> |
try { |
725 |
> |
Thread.sleep(SHORT_DELAY_MS); |
726 |
> |
t.interrupt(); |
727 |
> |
t.join(); |
728 |
> |
} catch(InterruptedException e){ |
729 |
> |
unexpectedException(); |
730 |
> |
} |
731 |
> |
} |
732 |
> |
|
733 |
> |
/** |
734 |
> |
* A waiting tryAcquire(n) blocks interruptibly |
735 |
> |
*/ |
736 |
> |
public void testTryAcquireN_InterruptedException_fair() { |
737 |
> |
final Semaphore s = new Semaphore(1, true); |
738 |
> |
Thread t = new Thread(new Runnable() { |
739 |
> |
public void run() { |
740 |
> |
try { |
741 |
> |
s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
742 |
> |
threadShouldThrow(); |
743 |
> |
} catch(InterruptedException success){ |
744 |
> |
} |
745 |
> |
} |
746 |
> |
}); |
747 |
> |
t.start(); |
748 |
> |
try { |
749 |
> |
Thread.sleep(SHORT_DELAY_MS); |
750 |
> |
t.interrupt(); |
751 |
> |
t.join(); |
752 |
> |
} catch(InterruptedException e){ |
753 |
> |
unexpectedException(); |
754 |
> |
} |
755 |
> |
} |
756 |
> |
|
757 |
> |
/** |
758 |
> |
* getQueueLength reports number of waiting threads |
759 |
> |
*/ |
760 |
> |
public void testGetQueueLength_fair() { |
761 |
> |
final Semaphore lock = new Semaphore(1, true); |
762 |
> |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
763 |
> |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
764 |
> |
try { |
765 |
> |
assertEquals(0, lock.getQueueLength()); |
766 |
> |
lock.acquireUninterruptibly(); |
767 |
> |
t1.start(); |
768 |
> |
Thread.sleep(SHORT_DELAY_MS); |
769 |
> |
assertEquals(1, lock.getQueueLength()); |
770 |
> |
t2.start(); |
771 |
> |
Thread.sleep(SHORT_DELAY_MS); |
772 |
> |
assertEquals(2, lock.getQueueLength()); |
773 |
> |
t1.interrupt(); |
774 |
> |
Thread.sleep(SHORT_DELAY_MS); |
775 |
> |
assertEquals(1, lock.getQueueLength()); |
776 |
> |
lock.release(); |
777 |
> |
Thread.sleep(SHORT_DELAY_MS); |
778 |
> |
assertEquals(0, lock.getQueueLength()); |
779 |
> |
t1.join(); |
780 |
> |
t2.join(); |
781 |
> |
} catch(Exception e){ |
782 |
> |
unexpectedException(); |
783 |
> |
} |
784 |
> |
} |
785 |
> |
|
786 |
> |
|
787 |
> |
/** |
788 |
> |
* a deserialized serialized semaphore has same number of permits |
789 |
> |
*/ |
790 |
> |
public void testSerialization_fair() { |
791 |
> |
Semaphore l = new Semaphore(3, true); |
792 |
> |
|
793 |
|
try { |
794 |
|
l.acquire(); |
795 |
|
l.release(); |
802 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
803 |
|
Semaphore r = (Semaphore) in.readObject(); |
804 |
|
assertEquals(3, r.availablePermits()); |
805 |
+ |
assertTrue(r.isFair()); |
806 |
|
r.acquire(); |
807 |
|
r.release(); |
808 |
|
} catch(Exception e){ |
810 |
|
} |
811 |
|
} |
812 |
|
|
813 |
+ |
|
814 |
|
} |