18 |
|
import java.util.concurrent.Executors; |
19 |
|
import java.util.concurrent.ExecutorService; |
20 |
|
import java.util.concurrent.SynchronousQueue; |
21 |
– |
import java.util.concurrent.ThreadLocalRandom; |
21 |
|
|
22 |
|
import junit.framework.Test; |
23 |
|
|
189 |
|
* timed offer times out if elements not taken |
190 |
|
*/ |
191 |
|
public void testTimedOffer() { |
192 |
< |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
192 |
> |
final boolean fair = randomBoolean(); |
193 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
194 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
195 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
208 |
|
|
209 |
|
pleaseInterrupt.countDown(); |
210 |
|
try { |
211 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
211 |
> |
q.offer(new Object(), LONGER_DELAY_MS, MILLISECONDS); |
212 |
|
shouldThrow(); |
213 |
|
} catch (InterruptedException success) {} |
214 |
|
assertFalse(Thread.interrupted()); |
216 |
– |
|
217 |
– |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
215 |
|
}}); |
216 |
|
|
217 |
|
await(pleaseInterrupt); |
245 |
|
* timed poll with nonzero timeout times out if no active putter |
246 |
|
*/ |
247 |
|
public void testTimedPoll() { |
248 |
< |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
248 |
> |
final boolean fair = randomBoolean(); |
249 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
250 |
|
final long startTime = System.nanoTime(); |
251 |
|
try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } |
258 |
|
* after offer succeeds; on interruption throws |
259 |
|
*/ |
260 |
|
public void testTimedPollWithOffer() { |
261 |
< |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
261 |
> |
final boolean fair = randomBoolean(); |
262 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
263 |
|
final CountDownLatch pleaseOffer = new CountDownLatch(1); |
264 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
437 |
|
public void testToArray_null(boolean fair) { |
438 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
439 |
|
try { |
440 |
< |
Object[] o = q.toArray((Object[])null); |
440 |
> |
Object[] unused = q.toArray((Object[])null); |
441 |
|
shouldThrow(); |
442 |
|
} catch (NullPointerException success) {} |
443 |
|
} |