121 |
|
} |
122 |
|
|
123 |
|
/** A constant to clarify calls to checking methods below. */ |
124 |
< |
final static Thread[] NO_THREADS = new Thread[0]; |
124 |
> |
static final Thread[] NO_THREADS = new Thread[0]; |
125 |
|
|
126 |
|
/** |
127 |
|
* Spin-waits until sync.isQueued(t) becomes true. |
947 |
|
} |
948 |
|
|
949 |
|
/** |
950 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
950 |
> |
* awaitUninterruptibly is uninterruptible |
951 |
|
*/ |
952 |
|
public void testAwaitUninterruptibly() { |
953 |
|
final Mutex sync = new Mutex(); |
954 |
|
final ConditionObject c = sync.newCondition(); |
955 |
< |
final BooleanLatch acquired = new BooleanLatch(); |
955 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
956 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
957 |
|
public void realRun() { |
958 |
|
sync.acquire(); |
959 |
< |
assertTrue(acquired.releaseShared(0)); |
959 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
960 |
|
c.awaitUninterruptibly(); |
961 |
|
assertTrue(Thread.interrupted()); |
962 |
|
assertHasWaitersLocked(sync, c, NO_THREADS); |
963 |
|
sync.release(); |
964 |
|
}}); |
965 |
|
|
966 |
< |
acquired.acquireShared(0); |
966 |
> |
pleaseInterrupt.acquireShared(0); |
967 |
|
sync.acquire(); |
968 |
|
assertHasWaitersLocked(sync, c, t); |
969 |
|
sync.release(); |
990 |
|
public void testInterruptible(final AwaitMethod awaitMethod) { |
991 |
|
final Mutex sync = new Mutex(); |
992 |
|
final ConditionObject c = sync.newCondition(); |
993 |
< |
final BooleanLatch acquired = new BooleanLatch(); |
993 |
> |
final BooleanLatch pleaseInterrupt = new BooleanLatch(); |
994 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
995 |
|
public void realRun() throws InterruptedException { |
996 |
|
sync.acquire(); |
997 |
< |
assertTrue(acquired.releaseShared(0)); |
997 |
> |
assertTrue(pleaseInterrupt.releaseShared(0)); |
998 |
|
await(c, awaitMethod); |
999 |
|
}}); |
1000 |
|
|
1001 |
< |
acquired.acquireShared(0); |
1001 |
> |
pleaseInterrupt.acquireShared(0); |
1002 |
|
t.interrupt(); |
1003 |
|
awaitTermination(t); |
1004 |
|
} |
1190 |
|
*/ |
1191 |
|
public void testTryAcquireSharedNanos_Timeout() { |
1192 |
|
final BooleanLatch l = new BooleanLatch(); |
1193 |
+ |
final BooleanLatch observedQueued = new BooleanLatch(); |
1194 |
+ |
final long timeoutMillis = timeoutMillis(); |
1195 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1196 |
|
public void realRun() throws InterruptedException { |
1197 |
|
assertFalse(l.isSignalled()); |
1198 |
< |
long startTime = System.nanoTime(); |
1199 |
< |
long nanos = MILLISECONDS.toNanos(timeoutMillis()); |
1200 |
< |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1201 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1198 |
> |
for (long millis = timeoutMillis(); |
1199 |
> |
!observedQueued.isSignalled(); |
1200 |
> |
millis *= 2) { |
1201 |
> |
long nanos = MILLISECONDS.toNanos(millis); |
1202 |
> |
long startTime = System.nanoTime(); |
1203 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1204 |
> |
assertTrue(millisElapsedSince(startTime) >= millis); |
1205 |
> |
} |
1206 |
|
assertFalse(l.isSignalled()); |
1207 |
|
}}); |
1208 |
|
|
1209 |
|
waitForQueuedThread(l, t); |
1210 |
+ |
observedQueued.releaseShared(0); |
1211 |
|
assertFalse(l.isSignalled()); |
1212 |
|
awaitTermination(t); |
1213 |
|
assertFalse(l.isSignalled()); |