18 |
|
import java.util.concurrent.Executors; |
19 |
|
import java.util.concurrent.ExecutorService; |
20 |
|
import java.util.concurrent.SynchronousQueue; |
21 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
22 |
|
|
23 |
|
import junit.framework.Test; |
24 |
|
|
139 |
|
}}); |
140 |
|
|
141 |
|
await(pleaseInterrupt); |
142 |
< |
assertThreadStaysAlive(t); |
142 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
143 |
|
t.interrupt(); |
144 |
|
awaitTermination(t); |
145 |
|
assertEquals(0, q.remainingCapacity()); |
173 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
174 |
|
|
175 |
|
await(pleaseInterrupt); |
176 |
< |
assertThreadStaysAlive(t); |
176 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
177 |
|
t.interrupt(); |
178 |
|
awaitTermination(t); |
179 |
|
assertEquals(0, q.remainingCapacity()); |
182 |
|
/** |
183 |
|
* timed offer times out if elements not taken |
184 |
|
*/ |
185 |
< |
public void testTimedOffer() { testTimedOffer(false); } |
186 |
< |
public void testTimedOffer_fair() { testTimedOffer(true); } |
186 |
< |
public void testTimedOffer(boolean fair) { |
185 |
> |
public void testTimedOffer() { |
186 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
187 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
188 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
189 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
196 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
197 |
|
shouldThrow(); |
198 |
|
} catch (InterruptedException success) {} |
199 |
+ |
assertFalse(Thread.interrupted()); |
200 |
|
}}); |
201 |
|
|
202 |
|
await(pleaseInterrupt); |
203 |
< |
assertThreadStaysAlive(t); |
203 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
204 |
|
t.interrupt(); |
205 |
|
awaitTermination(t); |
206 |
|
} |
229 |
|
/** |
230 |
|
* timed poll with nonzero timeout times out if no active putter |
231 |
|
*/ |
232 |
< |
public void testTimedPoll() { testTimedPoll(false); } |
233 |
< |
public void testTimedPoll_fair() { testTimedPoll(true); } |
233 |
< |
public void testTimedPoll(boolean fair) { |
232 |
> |
public void testTimedPoll() { |
233 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
234 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
235 |
< |
long startTime = System.nanoTime(); |
235 |
> |
final long startTime = System.nanoTime(); |
236 |
|
try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } |
237 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
238 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
242 |
|
* timed poll before a delayed offer times out, returning null; |
243 |
|
* after offer succeeds; on interruption throws |
244 |
|
*/ |
245 |
< |
public void testTimedPollWithOffer() { testTimedPollWithOffer(false); } |
246 |
< |
public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); } |
247 |
< |
public void testTimedPollWithOffer(boolean fair) { |
245 |
> |
public void testTimedPollWithOffer() { |
246 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
247 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
248 |
|
final CountDownLatch pleaseOffer = new CountDownLatch(1); |
249 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
281 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
282 |
|
|
283 |
|
await(pleaseInterrupt); |
284 |
< |
assertThreadStaysAlive(t); |
284 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
285 |
|
t.interrupt(); |
286 |
|
awaitTermination(t); |
287 |
|
} |
400 |
|
public void testToArray2() { testToArray2(false); } |
401 |
|
public void testToArray2_fair() { testToArray2(true); } |
402 |
|
public void testToArray2(boolean fair) { |
403 |
< |
final SynchronousQueue<Integer> q |
405 |
< |
= new SynchronousQueue<Integer>(fair); |
403 |
> |
final SynchronousQueue<Integer> q = new SynchronousQueue<>(fair); |
404 |
|
Integer[] a; |
405 |
|
|
406 |
|
a = new Integer[0]; |
568 |
|
fail("timed out"); |
569 |
|
Thread.yield(); |
570 |
|
} |
571 |
< |
assertTrue(l.size() == 1); |
571 |
> |
assertEquals(1, l.size()); |
572 |
|
assertSame(one, l.get(0)); |
573 |
|
awaitTermination(t); |
574 |
|
} |