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()); |
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() { |
203 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
204 |
|
shouldThrow(); |
205 |
|
} catch (InterruptedException success) {} |
206 |
+ |
assertFalse(Thread.interrupted()); |
207 |
|
}}); |
208 |
|
|
209 |
|
await(pleaseInterrupt); |
210 |
< |
assertThreadStaysAlive(t); |
210 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
211 |
|
t.interrupt(); |
212 |
|
awaitTermination(t); |
213 |
|
} |
236 |
|
/** |
237 |
|
* timed poll with nonzero timeout times out if no active putter |
238 |
|
*/ |
239 |
< |
public void testTimedPoll() { testTimedPoll(false); } |
240 |
< |
public void testTimedPoll_fair() { testTimedPoll(true); } |
233 |
< |
public void testTimedPoll(boolean fair) { |
239 |
> |
public void testTimedPoll() { |
240 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
241 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
242 |
< |
long startTime = System.nanoTime(); |
242 |
> |
final long startTime = System.nanoTime(); |
243 |
|
try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } |
244 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
245 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
249 |
|
* timed poll before a delayed offer times out, returning null; |
250 |
|
* after offer succeeds; on interruption throws |
251 |
|
*/ |
252 |
< |
public void testTimedPollWithOffer() { testTimedPollWithOffer(false); } |
253 |
< |
public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); } |
247 |
< |
public void testTimedPollWithOffer(boolean fair) { |
252 |
> |
public void testTimedPollWithOffer() { |
253 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
254 |
|
final SynchronousQueue q = new SynchronousQueue(fair); |
255 |
|
final CountDownLatch pleaseOffer = new CountDownLatch(1); |
256 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
288 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
289 |
|
|
290 |
|
await(pleaseInterrupt); |
291 |
< |
assertThreadStaysAlive(t); |
291 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
292 |
|
t.interrupt(); |
293 |
|
awaitTermination(t); |
294 |
|
} |