327 |
|
}}); |
328 |
|
|
329 |
|
t.start(); |
330 |
< |
Thread.sleep(SHORT_DELAY_MS); |
330 |
> |
delay(SHORT_DELAY_MS); |
331 |
|
t.interrupt(); |
332 |
|
t.join(); |
333 |
|
assertEquals(SIZE, q.size()); |
351 |
|
}}); |
352 |
|
|
353 |
|
t.start(); |
354 |
< |
Thread.sleep(SHORT_DELAY_MS); |
354 |
> |
delay(SHORT_DELAY_MS); |
355 |
|
assertEquals(q.remainingCapacity(), 0); |
356 |
|
assertEquals(0, q.take()); |
357 |
< |
Thread.sleep(SHORT_DELAY_MS); |
357 |
> |
delay(SHORT_DELAY_MS); |
358 |
|
t.interrupt(); |
359 |
|
t.join(); |
360 |
|
assertEquals(q.remainingCapacity(), 0); |
365 |
|
*/ |
366 |
|
public void testTimedOffer() throws InterruptedException { |
367 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
368 |
< |
Thread t = new Thread(new CheckedRunnable() { |
368 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
369 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
370 |
|
public void realRun() throws InterruptedException { |
371 |
|
q.put(new Object()); |
372 |
|
q.put(new Object()); |
373 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
373 |
> |
long startTime = System.nanoTime(); |
374 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
375 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
376 |
> |
pleaseInterrupt.countDown(); |
377 |
|
try { |
378 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
378 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
379 |
|
shouldThrow(); |
380 |
|
} catch (InterruptedException success) {} |
381 |
|
}}); |
382 |
|
|
383 |
< |
t.start(); |
380 |
< |
Thread.sleep(SHORT_DELAY_MS); |
383 |
> |
await(pleaseInterrupt); |
384 |
|
t.interrupt(); |
385 |
< |
t.join(); |
385 |
> |
awaitTermination(t); |
386 |
|
} |
387 |
|
|
388 |
|
/** |
412 |
|
}}); |
413 |
|
|
414 |
|
t.start(); |
415 |
< |
Thread.sleep(SHORT_DELAY_MS); |
415 |
> |
delay(SHORT_DELAY_MS); |
416 |
|
t.interrupt(); |
417 |
|
t.join(); |
418 |
|
} |
755 |
|
|
756 |
|
executor.execute(new CheckedRunnable() { |
757 |
|
public void realRun() throws InterruptedException { |
758 |
< |
Thread.sleep(SMALL_DELAY_MS); |
758 |
> |
delay(SMALL_DELAY_MS); |
759 |
|
assertSame(one, q.take()); |
760 |
|
}}); |
761 |
|
|
777 |
|
|
778 |
|
executor.execute(new CheckedRunnable() { |
779 |
|
public void realRun() throws InterruptedException { |
780 |
< |
Thread.sleep(SMALL_DELAY_MS); |
780 |
> |
delay(SMALL_DELAY_MS); |
781 |
|
q.put(one); |
782 |
|
}}); |
783 |
|
|