76 |
|
public void run() { |
77 |
|
try { |
78 |
|
sync.acquireInterruptibly(1); |
79 |
< |
} catch (InterruptedException success){} |
79 |
> |
} catch (InterruptedException success) {} |
80 |
|
} |
81 |
|
} |
82 |
|
|
92 |
|
try { |
93 |
|
sync.acquireInterruptibly(1); |
94 |
|
threadShouldThrow(); |
95 |
< |
} catch (InterruptedException success){} |
95 |
> |
} catch (InterruptedException success) {} |
96 |
|
} |
97 |
|
} |
98 |
|
|
128 |
|
/** |
129 |
|
* hasQueuedThreads reports whether there are waiting threads |
130 |
|
*/ |
131 |
< |
public void testhasQueuedThreads() { |
131 |
> |
public void testhasQueuedThreads() throws InterruptedException { |
132 |
|
final Mutex sync = new Mutex(); |
133 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
134 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
135 |
< |
try { |
136 |
< |
assertFalse(sync.hasQueuedThreads()); |
137 |
< |
sync.acquire(1); |
138 |
< |
t1.start(); |
139 |
< |
Thread.sleep(SHORT_DELAY_MS); |
140 |
< |
assertTrue(sync.hasQueuedThreads()); |
141 |
< |
t2.start(); |
142 |
< |
Thread.sleep(SHORT_DELAY_MS); |
143 |
< |
assertTrue(sync.hasQueuedThreads()); |
144 |
< |
t1.interrupt(); |
145 |
< |
Thread.sleep(SHORT_DELAY_MS); |
146 |
< |
assertTrue(sync.hasQueuedThreads()); |
147 |
< |
sync.release(1); |
148 |
< |
Thread.sleep(SHORT_DELAY_MS); |
149 |
< |
assertFalse(sync.hasQueuedThreads()); |
150 |
< |
t1.join(); |
151 |
< |
t2.join(); |
152 |
< |
} catch (Exception e){ |
153 |
< |
unexpectedException(); |
154 |
< |
} |
135 |
> |
assertFalse(sync.hasQueuedThreads()); |
136 |
> |
sync.acquire(1); |
137 |
> |
t1.start(); |
138 |
> |
Thread.sleep(SHORT_DELAY_MS); |
139 |
> |
assertTrue(sync.hasQueuedThreads()); |
140 |
> |
t2.start(); |
141 |
> |
Thread.sleep(SHORT_DELAY_MS); |
142 |
> |
assertTrue(sync.hasQueuedThreads()); |
143 |
> |
t1.interrupt(); |
144 |
> |
Thread.sleep(SHORT_DELAY_MS); |
145 |
> |
assertTrue(sync.hasQueuedThreads()); |
146 |
> |
sync.release(1); |
147 |
> |
Thread.sleep(SHORT_DELAY_MS); |
148 |
> |
assertFalse(sync.hasQueuedThreads()); |
149 |
> |
t1.join(); |
150 |
> |
t2.join(); |
151 |
|
} |
152 |
|
|
153 |
|
/** |
158 |
|
try { |
159 |
|
sync.isQueued(null); |
160 |
|
shouldThrow(); |
161 |
< |
} catch (NullPointerException success) { |
166 |
< |
} |
161 |
> |
} catch (NullPointerException success) {} |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
|
* isQueued reports whether a thread is queued. |
166 |
|
*/ |
167 |
< |
public void testIsQueued() { |
167 |
> |
public void testIsQueued() throws InterruptedException { |
168 |
|
final Mutex sync = new Mutex(); |
169 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
170 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
171 |
< |
try { |
172 |
< |
assertFalse(sync.isQueued(t1)); |
173 |
< |
assertFalse(sync.isQueued(t2)); |
174 |
< |
sync.acquire(1); |
175 |
< |
t1.start(); |
176 |
< |
Thread.sleep(SHORT_DELAY_MS); |
177 |
< |
assertTrue(sync.isQueued(t1)); |
178 |
< |
t2.start(); |
179 |
< |
Thread.sleep(SHORT_DELAY_MS); |
180 |
< |
assertTrue(sync.isQueued(t1)); |
181 |
< |
assertTrue(sync.isQueued(t2)); |
182 |
< |
t1.interrupt(); |
183 |
< |
Thread.sleep(SHORT_DELAY_MS); |
184 |
< |
assertFalse(sync.isQueued(t1)); |
185 |
< |
assertTrue(sync.isQueued(t2)); |
186 |
< |
sync.release(1); |
187 |
< |
Thread.sleep(SHORT_DELAY_MS); |
188 |
< |
assertFalse(sync.isQueued(t1)); |
189 |
< |
Thread.sleep(SHORT_DELAY_MS); |
190 |
< |
assertFalse(sync.isQueued(t2)); |
191 |
< |
t1.join(); |
197 |
< |
t2.join(); |
198 |
< |
} catch (Exception e){ |
199 |
< |
unexpectedException(); |
200 |
< |
} |
171 |
> |
assertFalse(sync.isQueued(t1)); |
172 |
> |
assertFalse(sync.isQueued(t2)); |
173 |
> |
sync.acquire(1); |
174 |
> |
t1.start(); |
175 |
> |
Thread.sleep(SHORT_DELAY_MS); |
176 |
> |
assertTrue(sync.isQueued(t1)); |
177 |
> |
t2.start(); |
178 |
> |
Thread.sleep(SHORT_DELAY_MS); |
179 |
> |
assertTrue(sync.isQueued(t1)); |
180 |
> |
assertTrue(sync.isQueued(t2)); |
181 |
> |
t1.interrupt(); |
182 |
> |
Thread.sleep(SHORT_DELAY_MS); |
183 |
> |
assertFalse(sync.isQueued(t1)); |
184 |
> |
assertTrue(sync.isQueued(t2)); |
185 |
> |
sync.release(1); |
186 |
> |
Thread.sleep(SHORT_DELAY_MS); |
187 |
> |
assertFalse(sync.isQueued(t1)); |
188 |
> |
Thread.sleep(SHORT_DELAY_MS); |
189 |
> |
assertFalse(sync.isQueued(t2)); |
190 |
> |
t1.join(); |
191 |
> |
t2.join(); |
192 |
|
} |
193 |
|
|
194 |
|
/** |
195 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
196 |
|
*/ |
197 |
< |
public void testGetFirstQueuedThread() { |
197 |
> |
public void testGetFirstQueuedThread() throws InterruptedException { |
198 |
|
final Mutex sync = new Mutex(); |
199 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
200 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
201 |
< |
try { |
202 |
< |
assertNull(sync.getFirstQueuedThread()); |
203 |
< |
sync.acquire(1); |
204 |
< |
t1.start(); |
205 |
< |
Thread.sleep(SHORT_DELAY_MS); |
206 |
< |
assertEquals(t1, sync.getFirstQueuedThread()); |
207 |
< |
t2.start(); |
208 |
< |
Thread.sleep(SHORT_DELAY_MS); |
209 |
< |
assertEquals(t1, sync.getFirstQueuedThread()); |
210 |
< |
t1.interrupt(); |
211 |
< |
Thread.sleep(SHORT_DELAY_MS); |
212 |
< |
Thread.sleep(SHORT_DELAY_MS); |
213 |
< |
assertEquals(t2, sync.getFirstQueuedThread()); |
214 |
< |
sync.release(1); |
215 |
< |
Thread.sleep(SHORT_DELAY_MS); |
216 |
< |
assertNull(sync.getFirstQueuedThread()); |
217 |
< |
t1.join(); |
227 |
< |
t2.join(); |
228 |
< |
} catch (Exception e){ |
229 |
< |
unexpectedException(); |
230 |
< |
} |
201 |
> |
assertNull(sync.getFirstQueuedThread()); |
202 |
> |
sync.acquire(1); |
203 |
> |
t1.start(); |
204 |
> |
Thread.sleep(SHORT_DELAY_MS); |
205 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
206 |
> |
t2.start(); |
207 |
> |
Thread.sleep(SHORT_DELAY_MS); |
208 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
209 |
> |
t1.interrupt(); |
210 |
> |
Thread.sleep(SHORT_DELAY_MS); |
211 |
> |
Thread.sleep(SHORT_DELAY_MS); |
212 |
> |
assertEquals(t2, sync.getFirstQueuedThread()); |
213 |
> |
sync.release(1); |
214 |
> |
Thread.sleep(SHORT_DELAY_MS); |
215 |
> |
assertNull(sync.getFirstQueuedThread()); |
216 |
> |
t1.join(); |
217 |
> |
t2.join(); |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
|
/** |
222 |
|
* hasContended reports false if no thread has ever blocked, else true |
223 |
|
*/ |
224 |
< |
public void testHasContended() { |
224 |
> |
public void testHasContended() throws InterruptedException { |
225 |
|
final Mutex sync = new Mutex(); |
226 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
227 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
228 |
< |
try { |
229 |
< |
assertFalse(sync.hasContended()); |
230 |
< |
sync.acquire(1); |
231 |
< |
t1.start(); |
232 |
< |
Thread.sleep(SHORT_DELAY_MS); |
233 |
< |
assertTrue(sync.hasContended()); |
234 |
< |
t2.start(); |
235 |
< |
Thread.sleep(SHORT_DELAY_MS); |
236 |
< |
assertTrue(sync.hasContended()); |
237 |
< |
t1.interrupt(); |
238 |
< |
Thread.sleep(SHORT_DELAY_MS); |
239 |
< |
assertTrue(sync.hasContended()); |
240 |
< |
sync.release(1); |
241 |
< |
Thread.sleep(SHORT_DELAY_MS); |
242 |
< |
assertTrue(sync.hasContended()); |
243 |
< |
t1.join(); |
257 |
< |
t2.join(); |
258 |
< |
} catch (Exception e){ |
259 |
< |
unexpectedException(); |
260 |
< |
} |
228 |
> |
assertFalse(sync.hasContended()); |
229 |
> |
sync.acquire(1); |
230 |
> |
t1.start(); |
231 |
> |
Thread.sleep(SHORT_DELAY_MS); |
232 |
> |
assertTrue(sync.hasContended()); |
233 |
> |
t2.start(); |
234 |
> |
Thread.sleep(SHORT_DELAY_MS); |
235 |
> |
assertTrue(sync.hasContended()); |
236 |
> |
t1.interrupt(); |
237 |
> |
Thread.sleep(SHORT_DELAY_MS); |
238 |
> |
assertTrue(sync.hasContended()); |
239 |
> |
sync.release(1); |
240 |
> |
Thread.sleep(SHORT_DELAY_MS); |
241 |
> |
assertTrue(sync.hasContended()); |
242 |
> |
t1.join(); |
243 |
> |
t2.join(); |
244 |
|
} |
245 |
|
|
246 |
|
/** |
247 |
|
* getQueuedThreads includes waiting threads |
248 |
|
*/ |
249 |
< |
public void testGetQueuedThreads() { |
249 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
250 |
|
final Mutex sync = new Mutex(); |
251 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
252 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
253 |
< |
try { |
254 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
255 |
< |
sync.acquire(1); |
256 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
257 |
< |
t1.start(); |
258 |
< |
Thread.sleep(SHORT_DELAY_MS); |
259 |
< |
assertTrue(sync.getQueuedThreads().contains(t1)); |
260 |
< |
t2.start(); |
261 |
< |
Thread.sleep(SHORT_DELAY_MS); |
262 |
< |
assertTrue(sync.getQueuedThreads().contains(t1)); |
263 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
264 |
< |
t1.interrupt(); |
265 |
< |
Thread.sleep(SHORT_DELAY_MS); |
266 |
< |
assertFalse(sync.getQueuedThreads().contains(t1)); |
267 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
268 |
< |
sync.release(1); |
269 |
< |
Thread.sleep(SHORT_DELAY_MS); |
270 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
271 |
< |
t1.join(); |
289 |
< |
t2.join(); |
290 |
< |
} catch (Exception e){ |
291 |
< |
unexpectedException(); |
292 |
< |
} |
253 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
254 |
> |
sync.acquire(1); |
255 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
256 |
> |
t1.start(); |
257 |
> |
Thread.sleep(SHORT_DELAY_MS); |
258 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
259 |
> |
t2.start(); |
260 |
> |
Thread.sleep(SHORT_DELAY_MS); |
261 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
262 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
263 |
> |
t1.interrupt(); |
264 |
> |
Thread.sleep(SHORT_DELAY_MS); |
265 |
> |
assertFalse(sync.getQueuedThreads().contains(t1)); |
266 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
267 |
> |
sync.release(1); |
268 |
> |
Thread.sleep(SHORT_DELAY_MS); |
269 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
270 |
> |
t1.join(); |
271 |
> |
t2.join(); |
272 |
|
} |
273 |
|
|
274 |
|
/** |
275 |
|
* getExclusiveQueuedThreads includes waiting threads |
276 |
|
*/ |
277 |
< |
public void testGetExclusiveQueuedThreads() { |
277 |
> |
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
278 |
|
final Mutex sync = new Mutex(); |
279 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
280 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
281 |
< |
try { |
282 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
283 |
< |
sync.acquire(1); |
284 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
285 |
< |
t1.start(); |
286 |
< |
Thread.sleep(SHORT_DELAY_MS); |
287 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
288 |
< |
t2.start(); |
289 |
< |
Thread.sleep(SHORT_DELAY_MS); |
290 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
291 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
292 |
< |
t1.interrupt(); |
293 |
< |
Thread.sleep(SHORT_DELAY_MS); |
294 |
< |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
295 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
296 |
< |
sync.release(1); |
297 |
< |
Thread.sleep(SHORT_DELAY_MS); |
298 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
299 |
< |
t1.join(); |
321 |
< |
t2.join(); |
322 |
< |
} catch (Exception e){ |
323 |
< |
unexpectedException(); |
324 |
< |
} |
281 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
282 |
> |
sync.acquire(1); |
283 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
284 |
> |
t1.start(); |
285 |
> |
Thread.sleep(SHORT_DELAY_MS); |
286 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
287 |
> |
t2.start(); |
288 |
> |
Thread.sleep(SHORT_DELAY_MS); |
289 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
290 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
291 |
> |
t1.interrupt(); |
292 |
> |
Thread.sleep(SHORT_DELAY_MS); |
293 |
> |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
294 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
295 |
> |
sync.release(1); |
296 |
> |
Thread.sleep(SHORT_DELAY_MS); |
297 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
298 |
> |
t1.join(); |
299 |
> |
t2.join(); |
300 |
|
} |
301 |
|
|
302 |
|
/** |
303 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
304 |
|
*/ |
305 |
< |
public void testGetSharedQueuedThreads() { |
305 |
> |
public void testGetSharedQueuedThreads() throws InterruptedException { |
306 |
|
final Mutex sync = new Mutex(); |
307 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
308 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
309 |
< |
try { |
310 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
311 |
< |
sync.acquire(1); |
312 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
313 |
< |
t1.start(); |
314 |
< |
Thread.sleep(SHORT_DELAY_MS); |
315 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
316 |
< |
t2.start(); |
317 |
< |
Thread.sleep(SHORT_DELAY_MS); |
318 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
319 |
< |
t1.interrupt(); |
320 |
< |
Thread.sleep(SHORT_DELAY_MS); |
321 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
322 |
< |
sync.release(1); |
323 |
< |
Thread.sleep(SHORT_DELAY_MS); |
324 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
325 |
< |
t1.join(); |
351 |
< |
t2.join(); |
352 |
< |
} catch (Exception e){ |
353 |
< |
unexpectedException(); |
354 |
< |
} |
309 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
310 |
> |
sync.acquire(1); |
311 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
312 |
> |
t1.start(); |
313 |
> |
Thread.sleep(SHORT_DELAY_MS); |
314 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
315 |
> |
t2.start(); |
316 |
> |
Thread.sleep(SHORT_DELAY_MS); |
317 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
318 |
> |
t1.interrupt(); |
319 |
> |
Thread.sleep(SHORT_DELAY_MS); |
320 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
321 |
> |
sync.release(1); |
322 |
> |
Thread.sleep(SHORT_DELAY_MS); |
323 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
324 |
> |
t1.join(); |
325 |
> |
t2.join(); |
326 |
|
} |
327 |
|
|
328 |
|
/** |
329 |
|
* tryAcquireNanos is interruptible. |
330 |
|
*/ |
331 |
< |
public void testInterruptedException2() { |
331 |
> |
public void testInterruptedException2() throws InterruptedException { |
332 |
|
final Mutex sync = new Mutex(); |
333 |
|
sync.acquire(1); |
334 |
|
Thread t = new Thread(new Runnable() { |
336 |
|
try { |
337 |
|
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
338 |
|
threadShouldThrow(); |
339 |
< |
} catch (InterruptedException success){} |
339 |
> |
} catch (InterruptedException success) {} |
340 |
|
} |
341 |
|
}); |
342 |
< |
try { |
343 |
< |
t.start(); |
344 |
< |
t.interrupt(); |
345 |
< |
} catch (Exception e){ |
375 |
< |
unexpectedException(); |
376 |
< |
} |
342 |
> |
|
343 |
> |
t.start(); |
344 |
> |
t.interrupt(); |
345 |
> |
t.join(); |
346 |
|
} |
347 |
|
|
348 |
|
|
349 |
|
/** |
350 |
|
* TryAcquire on exclusively held sync fails |
351 |
|
*/ |
352 |
< |
public void testTryAcquireWhenSynced() { |
352 |
> |
public void testTryAcquireWhenSynced() throws InterruptedException { |
353 |
|
final Mutex sync = new Mutex(); |
354 |
|
sync.acquire(1); |
355 |
|
Thread t = new Thread(new Runnable() { |
357 |
|
threadAssertFalse(sync.tryAcquire(1)); |
358 |
|
} |
359 |
|
}); |
360 |
< |
try { |
361 |
< |
t.start(); |
362 |
< |
t.join(); |
363 |
< |
sync.release(1); |
395 |
< |
} catch (Exception e){ |
396 |
< |
unexpectedException(); |
397 |
< |
} |
360 |
> |
|
361 |
> |
t.start(); |
362 |
> |
t.join(); |
363 |
> |
sync.release(1); |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
|
* tryAcquireNanos on an exclusively held sync times out |
368 |
|
*/ |
369 |
< |
public void testAcquireNanos_Timeout() { |
369 |
> |
public void testAcquireNanos_Timeout() throws InterruptedException { |
370 |
|
final Mutex sync = new Mutex(); |
371 |
|
sync.acquire(1); |
372 |
|
Thread t = new Thread(new Runnable() { |
374 |
|
try { |
375 |
|
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
376 |
|
} catch (Exception ex) { |
377 |
< |
threadUnexpectedException(); |
377 |
> |
threadUnexpectedException(ex); |
378 |
|
} |
379 |
|
} |
380 |
|
}); |
381 |
< |
try { |
382 |
< |
t.start(); |
383 |
< |
t.join(); |
384 |
< |
sync.release(1); |
419 |
< |
} catch (Exception e){ |
420 |
< |
unexpectedException(); |
421 |
< |
} |
381 |
> |
|
382 |
> |
t.start(); |
383 |
> |
t.join(); |
384 |
> |
sync.release(1); |
385 |
|
} |
386 |
|
|
387 |
|
|
388 |
|
/** |
389 |
|
* getState is true when acquired and false when not |
390 |
|
*/ |
391 |
< |
public void testGetState() { |
391 |
> |
public void testGetState() throws InterruptedException { |
392 |
|
final Mutex sync = new Mutex(); |
393 |
|
sync.acquire(1); |
394 |
|
assertTrue(sync.isHeldExclusively()); |
401 |
|
Thread.sleep(SMALL_DELAY_MS); |
402 |
|
} |
403 |
|
catch (Exception e) { |
404 |
< |
threadUnexpectedException(); |
404 |
> |
threadUnexpectedException(e); |
405 |
|
} |
406 |
|
sync.release(1); |
407 |
|
} |
408 |
|
}); |
409 |
< |
try { |
410 |
< |
t.start(); |
411 |
< |
Thread.sleep(SHORT_DELAY_MS); |
412 |
< |
assertTrue(sync.isHeldExclusively()); |
413 |
< |
t.join(); |
414 |
< |
assertFalse(sync.isHeldExclusively()); |
452 |
< |
} catch (Exception e){ |
453 |
< |
unexpectedException(); |
454 |
< |
} |
409 |
> |
|
410 |
> |
t.start(); |
411 |
> |
Thread.sleep(SHORT_DELAY_MS); |
412 |
> |
assertTrue(sync.isHeldExclusively()); |
413 |
> |
t.join(); |
414 |
> |
assertFalse(sync.isHeldExclusively()); |
415 |
|
} |
416 |
|
|
417 |
|
|
418 |
|
/** |
419 |
|
* acquireInterruptibly is interruptible. |
420 |
|
*/ |
421 |
< |
public void testAcquireInterruptibly1() { |
421 |
> |
public void testAcquireInterruptibly1() throws InterruptedException { |
422 |
|
final Mutex sync = new Mutex(); |
423 |
|
sync.acquire(1); |
424 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
425 |
< |
try { |
426 |
< |
t.start(); |
427 |
< |
Thread.sleep(SHORT_DELAY_MS); |
428 |
< |
t.interrupt(); |
429 |
< |
Thread.sleep(SHORT_DELAY_MS); |
430 |
< |
sync.release(1); |
471 |
< |
t.join(); |
472 |
< |
} catch (Exception e){ |
473 |
< |
unexpectedException(); |
474 |
< |
} |
425 |
> |
t.start(); |
426 |
> |
Thread.sleep(SHORT_DELAY_MS); |
427 |
> |
t.interrupt(); |
428 |
> |
Thread.sleep(SHORT_DELAY_MS); |
429 |
> |
sync.release(1); |
430 |
> |
t.join(); |
431 |
|
} |
432 |
|
|
433 |
|
/** |
434 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
435 |
|
*/ |
436 |
< |
public void testAcquireInterruptibly2() { |
436 |
> |
public void testAcquireInterruptibly2() throws InterruptedException { |
437 |
|
final Mutex sync = new Mutex(); |
438 |
< |
try { |
483 |
< |
sync.acquireInterruptibly(1); |
484 |
< |
} catch (Exception e) { |
485 |
< |
unexpectedException(); |
486 |
< |
} |
438 |
> |
sync.acquireInterruptibly(1); |
439 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
440 |
< |
try { |
441 |
< |
t.start(); |
442 |
< |
t.interrupt(); |
443 |
< |
assertTrue(sync.isHeldExclusively()); |
492 |
< |
t.join(); |
493 |
< |
} catch (Exception e){ |
494 |
< |
unexpectedException(); |
495 |
< |
} |
440 |
> |
t.start(); |
441 |
> |
t.interrupt(); |
442 |
> |
assertTrue(sync.isHeldExclusively()); |
443 |
> |
t.join(); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
457 |
|
/** |
458 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
459 |
|
*/ |
460 |
< |
public void testAwait_IllegalMonitor() { |
460 |
> |
public void testAwait_IllegalMonitor() throws InterruptedException { |
461 |
|
final Mutex sync = new Mutex(); |
462 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
463 |
|
try { |
466 |
|
} |
467 |
|
catch (IllegalMonitorStateException success) { |
468 |
|
} |
521 |
– |
catch (Exception ex) { |
522 |
– |
unexpectedException(); |
523 |
– |
} |
469 |
|
} |
470 |
|
|
471 |
|
/** |
472 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
473 |
|
*/ |
474 |
< |
public void testSignal_IllegalMonitor() { |
474 |
> |
public void testSignal_IllegalMonitor() throws InterruptedException { |
475 |
|
final Mutex sync = new Mutex(); |
476 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
477 |
|
try { |
478 |
|
c.signal(); |
479 |
|
shouldThrow(); |
480 |
|
} |
481 |
< |
catch (IllegalMonitorStateException success) { |
537 |
< |
} |
538 |
< |
catch (Exception ex) { |
539 |
< |
unexpectedException(); |
540 |
< |
} |
481 |
> |
catch (IllegalMonitorStateException success) {} |
482 |
|
} |
483 |
|
|
484 |
|
/** |
485 |
|
* awaitNanos without a signal times out |
486 |
|
*/ |
487 |
< |
public void testAwaitNanos_Timeout() { |
487 |
> |
public void testAwaitNanos_Timeout() throws InterruptedException { |
488 |
|
final Mutex sync = new Mutex(); |
489 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
490 |
< |
try { |
491 |
< |
sync.acquire(1); |
492 |
< |
long t = c.awaitNanos(100); |
493 |
< |
assertTrue(t <= 0); |
553 |
< |
sync.release(1); |
554 |
< |
} |
555 |
< |
catch (Exception ex) { |
556 |
< |
unexpectedException(); |
557 |
< |
} |
490 |
> |
sync.acquire(1); |
491 |
> |
long t = c.awaitNanos(100); |
492 |
> |
assertTrue(t <= 0); |
493 |
> |
sync.release(1); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
497 |
|
* Timed await without a signal times out |
498 |
|
*/ |
499 |
< |
public void testAwait_Timeout() { |
499 |
> |
public void testAwait_Timeout() throws InterruptedException { |
500 |
|
final Mutex sync = new Mutex(); |
501 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
502 |
< |
try { |
503 |
< |
sync.acquire(1); |
504 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
569 |
< |
sync.release(1); |
570 |
< |
} |
571 |
< |
catch (Exception ex) { |
572 |
< |
unexpectedException(); |
573 |
< |
} |
502 |
> |
sync.acquire(1); |
503 |
> |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
504 |
> |
sync.release(1); |
505 |
|
} |
506 |
|
|
507 |
|
/** |
508 |
|
* awaitUntil without a signal times out |
509 |
|
*/ |
510 |
< |
public void testAwaitUntil_Timeout() { |
510 |
> |
public void testAwaitUntil_Timeout() throws InterruptedException { |
511 |
|
final Mutex sync = new Mutex(); |
512 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
513 |
< |
try { |
514 |
< |
sync.acquire(1); |
515 |
< |
java.util.Date d = new java.util.Date(); |
516 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
586 |
< |
sync.release(1); |
587 |
< |
} |
588 |
< |
catch (Exception ex) { |
589 |
< |
unexpectedException(); |
590 |
< |
} |
513 |
> |
sync.acquire(1); |
514 |
> |
java.util.Date d = new java.util.Date(); |
515 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
516 |
> |
sync.release(1); |
517 |
|
} |
518 |
|
|
519 |
|
/** |
520 |
|
* await returns when signalled |
521 |
|
*/ |
522 |
< |
public void testAwait() { |
522 |
> |
public void testAwait() throws InterruptedException { |
523 |
|
final Mutex sync = new Mutex(); |
524 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
525 |
|
Thread t = new Thread(new Runnable() { |
530 |
|
sync.release(1); |
531 |
|
} |
532 |
|
catch (InterruptedException e) { |
533 |
< |
threadUnexpectedException(); |
533 |
> |
threadUnexpectedException(e); |
534 |
|
} |
535 |
|
} |
536 |
|
}); |
537 |
|
|
538 |
< |
try { |
539 |
< |
t.start(); |
540 |
< |
Thread.sleep(SHORT_DELAY_MS); |
541 |
< |
sync.acquire(1); |
542 |
< |
c.signal(); |
543 |
< |
sync.release(1); |
544 |
< |
t.join(SHORT_DELAY_MS); |
619 |
< |
assertFalse(t.isAlive()); |
620 |
< |
} |
621 |
< |
catch (Exception ex) { |
622 |
< |
unexpectedException(); |
623 |
< |
} |
538 |
> |
t.start(); |
539 |
> |
Thread.sleep(SHORT_DELAY_MS); |
540 |
> |
sync.acquire(1); |
541 |
> |
c.signal(); |
542 |
> |
sync.release(1); |
543 |
> |
t.join(SHORT_DELAY_MS); |
544 |
> |
assertFalse(t.isAlive()); |
545 |
|
} |
546 |
|
|
547 |
|
|
554 |
|
try { |
555 |
|
sync.hasWaiters(null); |
556 |
|
shouldThrow(); |
557 |
< |
} catch (NullPointerException success) { |
637 |
< |
} catch (Exception ex) { |
638 |
< |
unexpectedException(); |
639 |
< |
} |
557 |
> |
} catch (NullPointerException success) {} |
558 |
|
} |
559 |
|
|
560 |
|
/** |
565 |
|
try { |
566 |
|
sync.getWaitQueueLength(null); |
567 |
|
shouldThrow(); |
568 |
< |
} catch (NullPointerException success) { |
651 |
< |
} catch (Exception ex) { |
652 |
< |
unexpectedException(); |
653 |
< |
} |
568 |
> |
} catch (NullPointerException success) {} |
569 |
|
} |
570 |
|
|
571 |
|
|
577 |
|
try { |
578 |
|
sync.getWaitingThreads(null); |
579 |
|
shouldThrow(); |
580 |
< |
} catch (NullPointerException success) { |
666 |
< |
} catch (Exception ex) { |
667 |
< |
unexpectedException(); |
668 |
< |
} |
580 |
> |
} catch (NullPointerException success) {} |
581 |
|
} |
582 |
|
|
583 |
|
|
586 |
|
*/ |
587 |
|
public void testHasWaitersIAE() { |
588 |
|
final Mutex sync = new Mutex(); |
589 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
589 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
590 |
|
final Mutex sync2 = new Mutex(); |
591 |
|
try { |
592 |
|
sync2.hasWaiters(c); |
593 |
|
shouldThrow(); |
594 |
< |
} catch (IllegalArgumentException success) { |
683 |
< |
} catch (Exception ex) { |
684 |
< |
unexpectedException(); |
685 |
< |
} |
594 |
> |
} catch (IllegalArgumentException success) {} |
595 |
|
} |
596 |
|
|
597 |
|
/** |
599 |
|
*/ |
600 |
|
public void testHasWaitersIMSE() { |
601 |
|
final Mutex sync = new Mutex(); |
602 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
602 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
603 |
|
try { |
604 |
|
sync.hasWaiters(c); |
605 |
|
shouldThrow(); |
606 |
< |
} catch (IllegalMonitorStateException success) { |
698 |
< |
} catch (Exception ex) { |
699 |
< |
unexpectedException(); |
700 |
< |
} |
606 |
> |
} catch (IllegalMonitorStateException success) {} |
607 |
|
} |
608 |
|
|
609 |
|
|
612 |
|
*/ |
613 |
|
public void testGetWaitQueueLengthIAE() { |
614 |
|
final Mutex sync = new Mutex(); |
615 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
615 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
616 |
|
final Mutex sync2 = new Mutex(); |
617 |
|
try { |
618 |
|
sync2.getWaitQueueLength(c); |
619 |
|
shouldThrow(); |
620 |
< |
} catch (IllegalArgumentException success) { |
715 |
< |
} catch (Exception ex) { |
716 |
< |
unexpectedException(); |
717 |
< |
} |
620 |
> |
} catch (IllegalArgumentException success) {} |
621 |
|
} |
622 |
|
|
623 |
|
/** |
625 |
|
*/ |
626 |
|
public void testGetWaitQueueLengthIMSE() { |
627 |
|
final Mutex sync = new Mutex(); |
628 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
628 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
629 |
|
try { |
630 |
|
sync.getWaitQueueLength(c); |
631 |
|
shouldThrow(); |
632 |
< |
} catch (IllegalMonitorStateException success) { |
730 |
< |
} catch (Exception ex) { |
731 |
< |
unexpectedException(); |
732 |
< |
} |
632 |
> |
} catch (IllegalMonitorStateException success) {} |
633 |
|
} |
634 |
|
|
635 |
|
|
638 |
|
*/ |
639 |
|
public void testGetWaitingThreadsIAE() { |
640 |
|
final Mutex sync = new Mutex(); |
641 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
641 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
642 |
|
final Mutex sync2 = new Mutex(); |
643 |
|
try { |
644 |
|
sync2.getWaitingThreads(c); |
645 |
|
shouldThrow(); |
646 |
< |
} catch (IllegalArgumentException success) { |
747 |
< |
} catch (Exception ex) { |
748 |
< |
unexpectedException(); |
749 |
< |
} |
646 |
> |
} catch (IllegalArgumentException success) {} |
647 |
|
} |
648 |
|
|
649 |
|
/** |
651 |
|
*/ |
652 |
|
public void testGetWaitingThreadsIMSE() { |
653 |
|
final Mutex sync = new Mutex(); |
654 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
654 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
655 |
|
try { |
656 |
|
sync.getWaitingThreads(c); |
657 |
|
shouldThrow(); |
658 |
< |
} catch (IllegalMonitorStateException success) { |
762 |
< |
} catch (Exception ex) { |
763 |
< |
unexpectedException(); |
764 |
< |
} |
658 |
> |
} catch (IllegalMonitorStateException success) {} |
659 |
|
} |
660 |
|
|
661 |
|
|
663 |
|
/** |
664 |
|
* hasWaiters returns true when a thread is waiting, else false |
665 |
|
*/ |
666 |
< |
public void testHasWaiters() { |
666 |
> |
public void testHasWaiters() throws InterruptedException { |
667 |
|
final Mutex sync = new Mutex(); |
668 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
669 |
|
Thread t = new Thread(new Runnable() { |
676 |
|
sync.release(1); |
677 |
|
} |
678 |
|
catch (InterruptedException e) { |
679 |
< |
threadUnexpectedException(); |
679 |
> |
threadUnexpectedException(e); |
680 |
|
} |
681 |
|
} |
682 |
|
}); |
683 |
|
|
684 |
< |
try { |
685 |
< |
t.start(); |
686 |
< |
Thread.sleep(SHORT_DELAY_MS); |
687 |
< |
sync.acquire(1); |
688 |
< |
assertTrue(sync.hasWaiters(c)); |
689 |
< |
assertEquals(1, sync.getWaitQueueLength(c)); |
690 |
< |
c.signal(); |
691 |
< |
sync.release(1); |
692 |
< |
Thread.sleep(SHORT_DELAY_MS); |
693 |
< |
sync.acquire(1); |
694 |
< |
assertFalse(sync.hasWaiters(c)); |
695 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
696 |
< |
sync.release(1); |
697 |
< |
t.join(SHORT_DELAY_MS); |
804 |
< |
assertFalse(t.isAlive()); |
805 |
< |
} |
806 |
< |
catch (Exception ex) { |
807 |
< |
unexpectedException(); |
808 |
< |
} |
684 |
> |
t.start(); |
685 |
> |
Thread.sleep(SHORT_DELAY_MS); |
686 |
> |
sync.acquire(1); |
687 |
> |
assertTrue(sync.hasWaiters(c)); |
688 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
689 |
> |
c.signal(); |
690 |
> |
sync.release(1); |
691 |
> |
Thread.sleep(SHORT_DELAY_MS); |
692 |
> |
sync.acquire(1); |
693 |
> |
assertFalse(sync.hasWaiters(c)); |
694 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
695 |
> |
sync.release(1); |
696 |
> |
t.join(SHORT_DELAY_MS); |
697 |
> |
assertFalse(t.isAlive()); |
698 |
|
} |
699 |
|
|
700 |
|
/** |
701 |
|
* getWaitQueueLength returns number of waiting threads |
702 |
|
*/ |
703 |
< |
public void testGetWaitQueueLength() { |
703 |
> |
public void testGetWaitQueueLength() throws InterruptedException { |
704 |
|
final Mutex sync = new Mutex(); |
705 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
706 |
|
Thread t1 = new Thread(new Runnable() { |
713 |
|
sync.release(1); |
714 |
|
} |
715 |
|
catch (InterruptedException e) { |
716 |
< |
threadUnexpectedException(); |
716 |
> |
threadUnexpectedException(e); |
717 |
|
} |
718 |
|
} |
719 |
|
}); |
728 |
|
sync.release(1); |
729 |
|
} |
730 |
|
catch (InterruptedException e) { |
731 |
< |
threadUnexpectedException(); |
731 |
> |
threadUnexpectedException(e); |
732 |
|
} |
733 |
|
} |
734 |
|
}); |
735 |
|
|
736 |
< |
try { |
737 |
< |
t1.start(); |
738 |
< |
Thread.sleep(SHORT_DELAY_MS); |
739 |
< |
t2.start(); |
740 |
< |
Thread.sleep(SHORT_DELAY_MS); |
741 |
< |
sync.acquire(1); |
742 |
< |
assertTrue(sync.hasWaiters(c)); |
743 |
< |
assertEquals(2, sync.getWaitQueueLength(c)); |
744 |
< |
c.signalAll(); |
745 |
< |
sync.release(1); |
746 |
< |
Thread.sleep(SHORT_DELAY_MS); |
747 |
< |
sync.acquire(1); |
748 |
< |
assertFalse(sync.hasWaiters(c)); |
749 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
750 |
< |
sync.release(1); |
751 |
< |
t1.join(SHORT_DELAY_MS); |
752 |
< |
t2.join(SHORT_DELAY_MS); |
753 |
< |
assertFalse(t1.isAlive()); |
865 |
< |
assertFalse(t2.isAlive()); |
866 |
< |
} |
867 |
< |
catch (Exception ex) { |
868 |
< |
unexpectedException(); |
869 |
< |
} |
736 |
> |
t1.start(); |
737 |
> |
Thread.sleep(SHORT_DELAY_MS); |
738 |
> |
t2.start(); |
739 |
> |
Thread.sleep(SHORT_DELAY_MS); |
740 |
> |
sync.acquire(1); |
741 |
> |
assertTrue(sync.hasWaiters(c)); |
742 |
> |
assertEquals(2, sync.getWaitQueueLength(c)); |
743 |
> |
c.signalAll(); |
744 |
> |
sync.release(1); |
745 |
> |
Thread.sleep(SHORT_DELAY_MS); |
746 |
> |
sync.acquire(1); |
747 |
> |
assertFalse(sync.hasWaiters(c)); |
748 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
749 |
> |
sync.release(1); |
750 |
> |
t1.join(SHORT_DELAY_MS); |
751 |
> |
t2.join(SHORT_DELAY_MS); |
752 |
> |
assertFalse(t1.isAlive()); |
753 |
> |
assertFalse(t2.isAlive()); |
754 |
|
} |
755 |
|
|
756 |
|
/** |
757 |
|
* getWaitingThreads returns only and all waiting threads |
758 |
|
*/ |
759 |
< |
public void testGetWaitingThreads() { |
759 |
> |
public void testGetWaitingThreads() throws InterruptedException { |
760 |
|
final Mutex sync = new Mutex(); |
761 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
762 |
|
Thread t1 = new Thread(new Runnable() { |
768 |
|
sync.release(1); |
769 |
|
} |
770 |
|
catch (InterruptedException e) { |
771 |
< |
threadUnexpectedException(); |
771 |
> |
threadUnexpectedException(e); |
772 |
|
} |
773 |
|
} |
774 |
|
}); |
782 |
|
sync.release(1); |
783 |
|
} |
784 |
|
catch (InterruptedException e) { |
785 |
< |
threadUnexpectedException(); |
785 |
> |
threadUnexpectedException(e); |
786 |
|
} |
787 |
|
} |
788 |
|
}); |
789 |
|
|
906 |
– |
try { |
790 |
|
sync.acquire(1); |
791 |
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
792 |
|
sync.release(1); |
809 |
|
t2.join(SHORT_DELAY_MS); |
810 |
|
assertFalse(t1.isAlive()); |
811 |
|
assertFalse(t2.isAlive()); |
929 |
– |
} |
930 |
– |
catch (Exception ex) { |
931 |
– |
unexpectedException(); |
932 |
– |
} |
812 |
|
} |
813 |
|
|
814 |
|
|
816 |
|
/** |
817 |
|
* awaitUninterruptibly doesn't abort on interrupt |
818 |
|
*/ |
819 |
< |
public void testAwaitUninterruptibly() { |
819 |
> |
public void testAwaitUninterruptibly() throws InterruptedException { |
820 |
|
final Mutex sync = new Mutex(); |
821 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
822 |
|
Thread t = new Thread(new Runnable() { |
827 |
|
} |
828 |
|
}); |
829 |
|
|
830 |
< |
try { |
831 |
< |
t.start(); |
832 |
< |
Thread.sleep(SHORT_DELAY_MS); |
833 |
< |
t.interrupt(); |
834 |
< |
sync.acquire(1); |
835 |
< |
c.signal(); |
836 |
< |
sync.release(1); |
837 |
< |
t.join(SHORT_DELAY_MS); |
959 |
< |
assertFalse(t.isAlive()); |
960 |
< |
} |
961 |
< |
catch (Exception ex) { |
962 |
< |
unexpectedException(); |
963 |
< |
} |
830 |
> |
t.start(); |
831 |
> |
Thread.sleep(SHORT_DELAY_MS); |
832 |
> |
t.interrupt(); |
833 |
> |
sync.acquire(1); |
834 |
> |
c.signal(); |
835 |
> |
sync.release(1); |
836 |
> |
t.join(SHORT_DELAY_MS); |
837 |
> |
assertFalse(t.isAlive()); |
838 |
|
} |
839 |
|
|
840 |
|
/** |
841 |
|
* await is interruptible |
842 |
|
*/ |
843 |
< |
public void testAwait_Interrupt() { |
843 |
> |
public void testAwait_Interrupt() throws InterruptedException { |
844 |
|
final Mutex sync = new Mutex(); |
845 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
846 |
|
Thread t = new Thread(new Runnable() { |
856 |
|
} |
857 |
|
}); |
858 |
|
|
859 |
< |
try { |
860 |
< |
t.start(); |
861 |
< |
Thread.sleep(SHORT_DELAY_MS); |
862 |
< |
t.interrupt(); |
863 |
< |
t.join(SHORT_DELAY_MS); |
990 |
< |
assertFalse(t.isAlive()); |
991 |
< |
} |
992 |
< |
catch (Exception ex) { |
993 |
< |
unexpectedException(); |
994 |
< |
} |
859 |
> |
t.start(); |
860 |
> |
Thread.sleep(SHORT_DELAY_MS); |
861 |
> |
t.interrupt(); |
862 |
> |
t.join(SHORT_DELAY_MS); |
863 |
> |
assertFalse(t.isAlive()); |
864 |
|
} |
865 |
|
|
866 |
|
/** |
867 |
|
* awaitNanos is interruptible |
868 |
|
*/ |
869 |
< |
public void testAwaitNanos_Interrupt() { |
869 |
> |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
870 |
|
final Mutex sync = new Mutex(); |
871 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
872 |
|
Thread t = new Thread(new Runnable() { |
882 |
|
} |
883 |
|
}); |
884 |
|
|
885 |
< |
try { |
886 |
< |
t.start(); |
887 |
< |
Thread.sleep(SHORT_DELAY_MS); |
888 |
< |
t.interrupt(); |
889 |
< |
t.join(SHORT_DELAY_MS); |
1021 |
< |
assertFalse(t.isAlive()); |
1022 |
< |
} |
1023 |
< |
catch (Exception ex) { |
1024 |
< |
unexpectedException(); |
1025 |
< |
} |
885 |
> |
t.start(); |
886 |
> |
Thread.sleep(SHORT_DELAY_MS); |
887 |
> |
t.interrupt(); |
888 |
> |
t.join(SHORT_DELAY_MS); |
889 |
> |
assertFalse(t.isAlive()); |
890 |
|
} |
891 |
|
|
892 |
|
/** |
893 |
|
* awaitUntil is interruptible |
894 |
|
*/ |
895 |
< |
public void testAwaitUntil_Interrupt() { |
895 |
> |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
896 |
|
final Mutex sync = new Mutex(); |
897 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
898 |
|
Thread t = new Thread(new Runnable() { |
909 |
|
} |
910 |
|
}); |
911 |
|
|
912 |
< |
try { |
913 |
< |
t.start(); |
914 |
< |
Thread.sleep(SHORT_DELAY_MS); |
915 |
< |
t.interrupt(); |
916 |
< |
t.join(SHORT_DELAY_MS); |
1053 |
< |
assertFalse(t.isAlive()); |
1054 |
< |
} |
1055 |
< |
catch (Exception ex) { |
1056 |
< |
unexpectedException(); |
1057 |
< |
} |
912 |
> |
t.start(); |
913 |
> |
Thread.sleep(SHORT_DELAY_MS); |
914 |
> |
t.interrupt(); |
915 |
> |
t.join(SHORT_DELAY_MS); |
916 |
> |
assertFalse(t.isAlive()); |
917 |
|
} |
918 |
|
|
919 |
|
/** |
920 |
|
* signalAll wakes up all threads |
921 |
|
*/ |
922 |
< |
public void testSignalAll() { |
922 |
> |
public void testSignalAll() throws InterruptedException { |
923 |
|
final Mutex sync = new Mutex(); |
924 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
925 |
|
Thread t1 = new Thread(new Runnable() { |
948 |
|
} |
949 |
|
}); |
950 |
|
|
951 |
< |
try { |
952 |
< |
t1.start(); |
953 |
< |
t2.start(); |
954 |
< |
Thread.sleep(SHORT_DELAY_MS); |
955 |
< |
sync.acquire(1); |
956 |
< |
c.signalAll(); |
957 |
< |
sync.release(1); |
958 |
< |
t1.join(SHORT_DELAY_MS); |
959 |
< |
t2.join(SHORT_DELAY_MS); |
960 |
< |
assertFalse(t1.isAlive()); |
1102 |
< |
assertFalse(t2.isAlive()); |
1103 |
< |
} |
1104 |
< |
catch (Exception ex) { |
1105 |
< |
unexpectedException(); |
1106 |
< |
} |
951 |
> |
t1.start(); |
952 |
> |
t2.start(); |
953 |
> |
Thread.sleep(SHORT_DELAY_MS); |
954 |
> |
sync.acquire(1); |
955 |
> |
c.signalAll(); |
956 |
> |
sync.release(1); |
957 |
> |
t1.join(SHORT_DELAY_MS); |
958 |
> |
t2.join(SHORT_DELAY_MS); |
959 |
> |
assertFalse(t1.isAlive()); |
960 |
> |
assertFalse(t2.isAlive()); |
961 |
|
} |
962 |
|
|
963 |
|
|
976 |
|
/** |
977 |
|
* A serialized AQS deserializes with current state |
978 |
|
*/ |
979 |
< |
public void testSerialization() { |
979 |
> |
public void testSerialization() throws Exception { |
980 |
|
Mutex l = new Mutex(); |
981 |
|
l.acquire(1); |
982 |
|
assertTrue(l.isHeldExclusively()); |
983 |
|
|
984 |
< |
try { |
985 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
986 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
987 |
< |
out.writeObject(l); |
988 |
< |
out.close(); |
989 |
< |
|
990 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
991 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
992 |
< |
Mutex r = (Mutex) in.readObject(); |
1139 |
< |
assertTrue(r.isHeldExclusively()); |
1140 |
< |
} catch (Exception e){ |
1141 |
< |
e.printStackTrace(); |
1142 |
< |
unexpectedException(); |
1143 |
< |
} |
984 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
985 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
986 |
> |
out.writeObject(l); |
987 |
> |
out.close(); |
988 |
> |
|
989 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
990 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
991 |
> |
Mutex r = (Mutex) in.readObject(); |
992 |
> |
assertTrue(r.isHeldExclusively()); |
993 |
|
} |
994 |
|
|
995 |
|
|
1018 |
|
/** |
1019 |
|
* acquireSharedInterruptibly returns after release, but not before |
1020 |
|
*/ |
1021 |
< |
public void testAcquireSharedInterruptibly() { |
1021 |
> |
public void testAcquireSharedInterruptibly() throws InterruptedException { |
1022 |
|
final BooleanLatch l = new BooleanLatch(); |
1023 |
|
|
1024 |
|
Thread t = new Thread(new Runnable() { |
1027 |
|
threadAssertFalse(l.isSignalled()); |
1028 |
|
l.acquireSharedInterruptibly(0); |
1029 |
|
threadAssertTrue(l.isSignalled()); |
1030 |
< |
} catch (InterruptedException e){ |
1030 |
> |
} catch (InterruptedException e) { |
1031 |
|
threadUnexpectedException(); |
1032 |
|
} |
1033 |
|
} |
1034 |
|
}); |
1035 |
< |
try { |
1036 |
< |
t.start(); |
1037 |
< |
assertFalse(l.isSignalled()); |
1038 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1039 |
< |
l.releaseShared(0); |
1040 |
< |
assertTrue(l.isSignalled()); |
1041 |
< |
t.join(); |
1193 |
< |
} catch (InterruptedException e){ |
1194 |
< |
unexpectedException(); |
1195 |
< |
} |
1035 |
> |
|
1036 |
> |
t.start(); |
1037 |
> |
assertFalse(l.isSignalled()); |
1038 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1039 |
> |
l.releaseShared(0); |
1040 |
> |
assertTrue(l.isSignalled()); |
1041 |
> |
t.join(); |
1042 |
|
} |
1043 |
|
|
1044 |
|
|
1045 |
|
/** |
1046 |
|
* acquireSharedTimed returns after release |
1047 |
|
*/ |
1048 |
< |
public void testAsquireSharedTimed() { |
1048 |
> |
public void testAsquireSharedTimed() throws InterruptedException { |
1049 |
|
final BooleanLatch l = new BooleanLatch(); |
1050 |
|
|
1051 |
|
Thread t = new Thread(new Runnable() { |
1055 |
|
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
1056 |
|
threadAssertTrue(l.isSignalled()); |
1057 |
|
|
1058 |
< |
} catch (InterruptedException e){ |
1058 |
> |
} catch (InterruptedException e) { |
1059 |
|
threadUnexpectedException(); |
1060 |
|
} |
1061 |
|
} |
1062 |
|
}); |
1063 |
< |
try { |
1064 |
< |
t.start(); |
1065 |
< |
assertFalse(l.isSignalled()); |
1066 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1067 |
< |
l.releaseShared(0); |
1068 |
< |
assertTrue(l.isSignalled()); |
1069 |
< |
t.join(); |
1224 |
< |
} catch (InterruptedException e){ |
1225 |
< |
unexpectedException(); |
1226 |
< |
} |
1063 |
> |
|
1064 |
> |
t.start(); |
1065 |
> |
assertFalse(l.isSignalled()); |
1066 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1067 |
> |
l.releaseShared(0); |
1068 |
> |
assertTrue(l.isSignalled()); |
1069 |
> |
t.join(); |
1070 |
|
} |
1071 |
|
|
1072 |
|
/** |
1073 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
1074 |
|
*/ |
1075 |
< |
public void testAcquireSharedInterruptibly_InterruptedException() { |
1075 |
> |
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
1076 |
|
final BooleanLatch l = new BooleanLatch(); |
1077 |
|
Thread t = new Thread(new Runnable() { |
1078 |
|
public void run() { |
1080 |
|
threadAssertFalse(l.isSignalled()); |
1081 |
|
l.acquireSharedInterruptibly(0); |
1082 |
|
threadShouldThrow(); |
1083 |
< |
} catch (InterruptedException success){} |
1083 |
> |
} catch (InterruptedException success) {} |
1084 |
|
} |
1085 |
|
}); |
1086 |
+ |
|
1087 |
|
t.start(); |
1088 |
< |
try { |
1089 |
< |
assertFalse(l.isSignalled()); |
1090 |
< |
t.interrupt(); |
1247 |
< |
t.join(); |
1248 |
< |
} catch (InterruptedException e){ |
1249 |
< |
unexpectedException(); |
1250 |
< |
} |
1088 |
> |
assertFalse(l.isSignalled()); |
1089 |
> |
t.interrupt(); |
1090 |
> |
t.join(); |
1091 |
|
} |
1092 |
|
|
1093 |
|
/** |
1094 |
|
* acquireSharedTimed throws IE if interrupted before released |
1095 |
|
*/ |
1096 |
< |
public void testAcquireSharedNanos_InterruptedException() { |
1096 |
> |
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
1097 |
|
final BooleanLatch l = new BooleanLatch(); |
1098 |
|
Thread t = new Thread(new Runnable() { |
1099 |
|
public void run() { |
1101 |
|
threadAssertFalse(l.isSignalled()); |
1102 |
|
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1103 |
|
threadShouldThrow(); |
1104 |
< |
} catch (InterruptedException success){} |
1104 |
> |
} catch (InterruptedException success) {} |
1105 |
|
} |
1106 |
|
}); |
1107 |
+ |
|
1108 |
|
t.start(); |
1109 |
< |
try { |
1110 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1111 |
< |
assertFalse(l.isSignalled()); |
1112 |
< |
t.interrupt(); |
1272 |
< |
t.join(); |
1273 |
< |
} catch (InterruptedException e){ |
1274 |
< |
unexpectedException(); |
1275 |
< |
} |
1109 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1110 |
> |
assertFalse(l.isSignalled()); |
1111 |
> |
t.interrupt(); |
1112 |
> |
t.join(); |
1113 |
|
} |
1114 |
|
|
1115 |
|
/** |
1116 |
|
* acquireSharedTimed times out if not released before timeout |
1117 |
|
*/ |
1118 |
< |
public void testAcquireSharedNanos_Timeout() { |
1118 |
> |
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1119 |
|
final BooleanLatch l = new BooleanLatch(); |
1120 |
|
Thread t = new Thread(new Runnable() { |
1121 |
|
public void run() { |
1122 |
|
try { |
1123 |
|
threadAssertFalse(l.isSignalled()); |
1124 |
|
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1125 |
< |
} catch (InterruptedException ie){ |
1125 |
> |
} catch (InterruptedException ie) { |
1126 |
|
threadUnexpectedException(); |
1127 |
|
} |
1128 |
|
} |
1129 |
|
}); |
1130 |
+ |
|
1131 |
|
t.start(); |
1132 |
< |
try { |
1133 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1134 |
< |
assertFalse(l.isSignalled()); |
1297 |
< |
t.join(); |
1298 |
< |
} catch (InterruptedException e){ |
1299 |
< |
unexpectedException(); |
1300 |
< |
} |
1132 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1133 |
> |
assertFalse(l.isSignalled()); |
1134 |
> |
t.join(); |
1135 |
|
} |
1136 |
|
|
1137 |
|
|