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 |
|
|
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
< |
* addAll throws ISE if no active taker |
100 |
> |
* addAll throws IllegalStateException if no active taker |
101 |
|
*/ |
102 |
|
public void testAddAll_ISE() { testAddAll_ISE(false); } |
103 |
|
public void testAddAll_ISE_fair() { testAddAll_ISE(true); } |
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()); |
159 |
|
pleaseTake.countDown(); |
160 |
|
q.put(one); |
161 |
|
|
162 |
+ |
Thread.currentThread().interrupt(); |
163 |
+ |
try { |
164 |
+ |
q.put(99); |
165 |
+ |
shouldThrow(); |
166 |
+ |
} catch (InterruptedException success) {} |
167 |
+ |
assertFalse(Thread.interrupted()); |
168 |
+ |
|
169 |
|
pleaseInterrupt.countDown(); |
170 |
|
try { |
171 |
|
q.put(99); |
180 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
181 |
|
|
182 |
|
await(pleaseInterrupt); |
183 |
< |
assertThreadStaysAlive(t); |
183 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
184 |
|
t.interrupt(); |
185 |
|
awaitTermination(t); |
186 |
|
assertEquals(0, q.remainingCapacity()); |
189 |
|
/** |
190 |
|
* timed offer times out if elements not taken |
191 |
|
*/ |
192 |
< |
public void testTimedOffer() { testTimedOffer(false); } |
193 |
< |
public void testTimedOffer_fair() { testTimedOffer(true); } |
186 |
< |
public void testTimedOffer(boolean fair) { |
192 |
> |
public void testTimedOffer() { |
193 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
194 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
195 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
196 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
198 |
|
long startTime = System.nanoTime(); |
199 |
|
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
200 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
201 |
+ |
|
202 |
+ |
Thread.currentThread().interrupt(); |
203 |
+ |
try { |
204 |
+ |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
205 |
+ |
shouldThrow(); |
206 |
+ |
} catch (InterruptedException success) {} |
207 |
+ |
assertFalse(Thread.interrupted()); |
208 |
+ |
|
209 |
|
pleaseInterrupt.countDown(); |
210 |
|
try { |
211 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
212 |
|
shouldThrow(); |
213 |
|
} catch (InterruptedException success) {} |
214 |
+ |
assertFalse(Thread.interrupted()); |
215 |
|
}}); |
216 |
|
|
217 |
|
await(pleaseInterrupt); |
218 |
< |
assertThreadStaysAlive(t); |
218 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
219 |
|
t.interrupt(); |
220 |
|
awaitTermination(t); |
221 |
|
} |
244 |
|
/** |
245 |
|
* timed poll with nonzero timeout times out if no active putter |
246 |
|
*/ |
247 |
< |
public void testTimedPoll() { testTimedPoll(false); } |
248 |
< |
public void testTimedPoll_fair() { testTimedPoll(true); } |
233 |
< |
public void testTimedPoll(boolean fair) { |
247 |
> |
public void testTimedPoll() { |
248 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
249 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
250 |
< |
long startTime = System.nanoTime(); |
250 |
> |
final long startTime = System.nanoTime(); |
251 |
|
try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } |
252 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
253 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
257 |
|
* timed poll before a delayed offer times out, returning null; |
258 |
|
* after offer succeeds; on interruption throws |
259 |
|
*/ |
260 |
< |
public void testTimedPollWithOffer() { testTimedPollWithOffer(false); } |
261 |
< |
public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); } |
247 |
< |
public void testTimedPollWithOffer(boolean fair) { |
260 |
> |
public void testTimedPollWithOffer() { |
261 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
262 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
263 |
|
final CountDownLatch pleaseOffer = new CountDownLatch(1); |
264 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
296 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
297 |
|
|
298 |
|
await(pleaseInterrupt); |
299 |
< |
assertThreadStaysAlive(t); |
299 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
300 |
|
t.interrupt(); |
301 |
|
awaitTermination(t); |
302 |
|
} |
415 |
|
public void testToArray2() { testToArray2(false); } |
416 |
|
public void testToArray2_fair() { testToArray2(true); } |
417 |
|
public void testToArray2(boolean fair) { |
418 |
< |
final SynchronousQueue<Integer> q |
405 |
< |
= new SynchronousQueue<Integer>(fair); |
418 |
> |
final SynchronousQueue<Integer> q = new SynchronousQueue<>(fair); |
419 |
|
Integer[] a; |
420 |
|
|
421 |
|
a = new Integer[0]; |
452 |
|
} |
453 |
|
|
454 |
|
/** |
455 |
< |
* iterator remove throws ISE |
455 |
> |
* iterator remove throws IllegalStateException |
456 |
|
*/ |
457 |
|
public void testIteratorRemove() { testIteratorRemove(false); } |
458 |
|
public void testIteratorRemove_fair() { testIteratorRemove(true); } |
583 |
|
fail("timed out"); |
584 |
|
Thread.yield(); |
585 |
|
} |
586 |
< |
assertTrue(l.size() == 1); |
586 |
> |
assertEquals(1, l.size()); |
587 |
|
assertSame(one, l.get(0)); |
588 |
|
awaitTermination(t); |
589 |
|
} |