8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
11 |
> |
import java.util.concurrent.CountDownLatch; |
12 |
> |
import java.util.concurrent.Semaphore; |
13 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
– |
import java.io.*; |
14 |
|
|
15 |
|
public class SemaphoreTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
72 |
|
throw new AssertionFailedError("timed out"); |
73 |
|
Thread.yield(); |
74 |
|
} |
75 |
< |
assert(s.hasQueuedThreads()); |
75 |
> |
assertTrue(s.hasQueuedThreads()); |
76 |
|
assertTrue(t.isAlive()); |
77 |
|
} |
78 |
|
|
223 |
|
public void testInterruptible_tryAcquireTimedN_fair() { testInterruptible(true, AcquireMethod.tryAcquireTimedN); } |
224 |
|
public void testInterruptible(boolean fair, final AcquireMethod acquirer) { |
225 |
|
final PublicSemaphore s = new PublicSemaphore(0, fair); |
226 |
< |
final Semaphore anotherInterruptPlease = new Semaphore(0, fair); |
226 |
> |
final Semaphore pleaseInterrupt = new Semaphore(0, fair); |
227 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
228 |
|
public void realRun() { |
229 |
|
// Interrupt before acquire |
246 |
|
shouldThrow(); |
247 |
|
} catch (InterruptedException success) {} |
248 |
|
|
249 |
< |
anotherInterruptPlease.release(); |
249 |
> |
pleaseInterrupt.release(); |
250 |
|
|
251 |
|
// Interrupt during acquire(N) |
252 |
|
try { |
257 |
|
|
258 |
|
waitForQueuedThread(s, t); |
259 |
|
t.interrupt(); |
260 |
< |
try { |
261 |
< |
assertTrue(anotherInterruptPlease.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
262 |
< |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
260 |
> |
await(pleaseInterrupt); |
261 |
|
waitForQueuedThread(s, t); |
262 |
|
t.interrupt(); |
263 |
|
awaitTermination(t); |
273 |
|
public void testUninterruptible_acquireUninterruptiblyN_fair() { testUninterruptible(true, AcquireMethod.acquireUninterruptiblyN); } |
274 |
|
public void testUninterruptible(boolean fair, final AcquireMethod acquirer) { |
275 |
|
final PublicSemaphore s = new PublicSemaphore(0, fair); |
276 |
< |
final Semaphore anotherInterruptPlease = new Semaphore(0, fair); |
277 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
276 |
> |
final Semaphore pleaseInterrupt = new Semaphore(-1, fair); |
277 |
> |
|
278 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
279 |
|
public void realRun() throws InterruptedException { |
280 |
|
// Interrupt before acquire |
281 |
+ |
pleaseInterrupt.release(); |
282 |
|
Thread.currentThread().interrupt(); |
283 |
|
acquirer.acquire(s); |
284 |
|
assertTrue(Thread.interrupted()); |
285 |
+ |
}}); |
286 |
|
|
287 |
< |
anotherInterruptPlease.release(); |
288 |
< |
|
287 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
288 |
> |
public void realRun() throws InterruptedException { |
289 |
|
// Interrupt during acquire |
290 |
+ |
pleaseInterrupt.release(); |
291 |
|
acquirer.acquire(s); |
292 |
|
assertTrue(Thread.interrupted()); |
293 |
|
}}); |
294 |
|
|
295 |
< |
waitForQueuedThread(s, t); |
296 |
< |
assertThreadStaysAlive(t); |
297 |
< |
s.release(); |
295 |
> |
await(pleaseInterrupt); |
296 |
> |
waitForQueuedThread(s, t1); |
297 |
> |
waitForQueuedThread(s, t2); |
298 |
> |
t2.interrupt(); |
299 |
|
|
300 |
< |
try { |
301 |
< |
assertTrue(anotherInterruptPlease.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
299 |
< |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
300 |
< |
waitForQueuedThread(s, t); |
301 |
< |
t.interrupt(); |
302 |
< |
assertThreadStaysAlive(t); |
303 |
< |
s.release(); |
300 |
> |
assertThreadStaysAlive(t1); |
301 |
> |
assertTrue(t2.isAlive()); |
302 |
|
|
303 |
< |
awaitTermination(t); |
303 |
> |
s.release(2); |
304 |
> |
|
305 |
> |
awaitTermination(t1); |
306 |
> |
awaitTermination(t2); |
307 |
|
} |
308 |
|
|
309 |
|
/** |