138 |
|
}}); |
139 |
|
|
140 |
|
await(pleaseInterrupt); |
141 |
< |
assertThreadStaysAlive(t); |
141 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
142 |
|
t.interrupt(); |
143 |
|
awaitTermination(t); |
144 |
|
assertEquals(0, q.remainingCapacity()); |
172 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
173 |
|
|
174 |
|
await(pleaseInterrupt); |
175 |
< |
assertThreadStaysAlive(t); |
175 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
176 |
|
t.interrupt(); |
177 |
|
awaitTermination(t); |
178 |
|
assertEquals(0, q.remainingCapacity()); |
199 |
|
}}); |
200 |
|
|
201 |
|
await(pleaseInterrupt); |
202 |
< |
assertThreadStaysAlive(t); |
202 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
203 |
|
t.interrupt(); |
204 |
|
awaitTermination(t); |
205 |
|
} |
257 |
|
pleaseOffer.countDown(); |
258 |
|
startTime = System.nanoTime(); |
259 |
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
260 |
– |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
260 |
|
|
261 |
|
Thread.currentThread().interrupt(); |
262 |
|
try { |
271 |
|
shouldThrow(); |
272 |
|
} catch (InterruptedException success) {} |
273 |
|
assertFalse(Thread.interrupted()); |
274 |
+ |
|
275 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
276 |
|
}}); |
277 |
|
|
278 |
|
await(pleaseOffer); |
279 |
|
long startTime = System.nanoTime(); |
280 |
|
try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); } |
281 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
282 |
< |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
282 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
283 |
|
|
284 |
|
await(pleaseInterrupt); |
285 |
< |
assertThreadStaysAlive(t); |
285 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
286 |
|
t.interrupt(); |
287 |
|
awaitTermination(t); |
288 |
|
} |
401 |
|
public void testToArray2() { testToArray2(false); } |
402 |
|
public void testToArray2_fair() { testToArray2(true); } |
403 |
|
public void testToArray2(boolean fair) { |
404 |
< |
final SynchronousQueue<Integer> q |
404 |
< |
= new SynchronousQueue<Integer>(fair); |
404 |
> |
final SynchronousQueue<Integer> q = new SynchronousQueue<>(fair); |
405 |
|
Integer[] a; |
406 |
|
|
407 |
|
a = new Integer[0]; |
469 |
|
public void testOfferInExecutor_fair() { testOfferInExecutor(true); } |
470 |
|
public void testOfferInExecutor(boolean fair) { |
471 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
472 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
472 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
473 |
+ |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
474 |
+ |
try (PoolCleaner cleaner = cleaner(executor)) { |
475 |
|
|
476 |
< |
executor.execute(new CheckedRunnable() { |
477 |
< |
public void realRun() throws InterruptedException { |
478 |
< |
assertFalse(q.offer(one)); |
479 |
< |
threadsStarted.await(); |
480 |
< |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
481 |
< |
assertEquals(0, q.remainingCapacity()); |
482 |
< |
}}); |
483 |
< |
|
484 |
< |
executor.execute(new CheckedRunnable() { |
485 |
< |
public void realRun() throws InterruptedException { |
486 |
< |
threadsStarted.await(); |
487 |
< |
assertSame(one, q.take()); |
488 |
< |
}}); |
489 |
< |
|
489 |
< |
joinPool(executor); |
476 |
> |
executor.execute(new CheckedRunnable() { |
477 |
> |
public void realRun() throws InterruptedException { |
478 |
> |
assertFalse(q.offer(one)); |
479 |
> |
threadsStarted.await(); |
480 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
481 |
> |
assertEquals(0, q.remainingCapacity()); |
482 |
> |
}}); |
483 |
> |
|
484 |
> |
executor.execute(new CheckedRunnable() { |
485 |
> |
public void realRun() throws InterruptedException { |
486 |
> |
threadsStarted.await(); |
487 |
> |
assertSame(one, q.take()); |
488 |
> |
}}); |
489 |
> |
} |
490 |
|
} |
491 |
|
|
492 |
|
/** |
497 |
|
public void testPollInExecutor(boolean fair) { |
498 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
499 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
500 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
501 |
< |
executor.execute(new CheckedRunnable() { |
502 |
< |
public void realRun() throws InterruptedException { |
503 |
< |
assertNull(q.poll()); |
504 |
< |
threadsStarted.await(); |
505 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
506 |
< |
assertTrue(q.isEmpty()); |
507 |
< |
}}); |
508 |
< |
|
509 |
< |
executor.execute(new CheckedRunnable() { |
510 |
< |
public void realRun() throws InterruptedException { |
511 |
< |
threadsStarted.await(); |
512 |
< |
q.put(one); |
513 |
< |
}}); |
514 |
< |
|
515 |
< |
joinPool(executor); |
500 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
501 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
502 |
> |
executor.execute(new CheckedRunnable() { |
503 |
> |
public void realRun() throws InterruptedException { |
504 |
> |
assertNull(q.poll()); |
505 |
> |
threadsStarted.await(); |
506 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
507 |
> |
assertTrue(q.isEmpty()); |
508 |
> |
}}); |
509 |
> |
|
510 |
> |
executor.execute(new CheckedRunnable() { |
511 |
> |
public void realRun() throws InterruptedException { |
512 |
> |
threadsStarted.await(); |
513 |
> |
q.put(one); |
514 |
> |
}}); |
515 |
> |
} |
516 |
|
} |
517 |
|
|
518 |
|
/** |
569 |
|
fail("timed out"); |
570 |
|
Thread.yield(); |
571 |
|
} |
572 |
< |
assertTrue(l.size() == 1); |
572 |
> |
assertEquals(1, l.size()); |
573 |
|
assertSame(one, l.get(0)); |
574 |
|
awaitTermination(t); |
575 |
|
} |
590 |
|
}}); |
591 |
|
|
592 |
|
ArrayList l = new ArrayList(); |
593 |
< |
delay(SHORT_DELAY_MS); |
594 |
< |
q.drainTo(l, 1); |
593 |
> |
int drained; |
594 |
> |
while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); |
595 |
> |
assertEquals(1, drained); |
596 |
|
assertEquals(1, l.size()); |
597 |
< |
q.drainTo(l, 1); |
597 |
> |
while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); |
598 |
> |
assertEquals(1, drained); |
599 |
|
assertEquals(2, l.size()); |
600 |
|
assertTrue(l.contains(one)); |
601 |
|
assertTrue(l.contains(two)); |