75 |
|
} |
76 |
|
|
77 |
|
/** |
78 |
< |
* tryAcquire succeeds when sufficent permits, else fails |
78 |
> |
* tryAcquire succeeds when sufficient permits, else fails |
79 |
|
*/ |
80 |
|
public void testTryAcquireInSameThread() { |
81 |
|
Semaphore s = new Semaphore(2, false); |
291 |
|
} |
292 |
|
|
293 |
|
/** |
294 |
+ |
* hasQueuedThreads reports whether there are waiting threads |
295 |
+ |
*/ |
296 |
+ |
public void testHasQueuedThreads() { |
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 |
+ |
assertFalse(lock.hasQueuedThreads()); |
302 |
+ |
lock.acquireUninterruptibly(); |
303 |
+ |
t1.start(); |
304 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
305 |
+ |
assertTrue(lock.hasQueuedThreads()); |
306 |
+ |
t2.start(); |
307 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
308 |
+ |
assertTrue(lock.hasQueuedThreads()); |
309 |
+ |
t1.interrupt(); |
310 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
311 |
+ |
assertTrue(lock.hasQueuedThreads()); |
312 |
+ |
lock.release(); |
313 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
314 |
+ |
assertFalse(lock.hasQueuedThreads()); |
315 |
+ |
t1.join(); |
316 |
+ |
t2.join(); |
317 |
+ |
} catch(Exception e){ |
318 |
+ |
unexpectedException(); |
319 |
+ |
} |
320 |
+ |
} |
321 |
+ |
|
322 |
+ |
/** |
323 |
|
* getQueueLength reports number of waiting threads |
324 |
|
*/ |
325 |
|
public void testGetQueueLength() { |
433 |
|
} |
434 |
|
|
435 |
|
/** |
436 |
< |
* tryAcquire succeeds when sufficent permits, else fails |
436 |
> |
* tryAcquire succeeds when sufficient permits, else fails |
437 |
|
*/ |
438 |
|
public void testTryAcquireInSameThread_fair() { |
439 |
|
Semaphore s = new Semaphore(2, true); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
< |
* tryAcquire(n) succeeds when sufficent permits, else fails |
448 |
> |
* tryAcquire(n) succeeds when sufficient permits, else fails |
449 |
|
*/ |
450 |
|
public void testTryAcquireNInSameThread_fair() { |
451 |
|
Semaphore s = new Semaphore(2, true); |