73 |
|
public void run() { |
74 |
|
try { |
75 |
|
sync.acquireInterruptibly(1); |
76 |
< |
} catch(InterruptedException success){} |
76 |
> |
} catch (InterruptedException success) {} |
77 |
|
} |
78 |
|
} |
79 |
|
|
89 |
|
try { |
90 |
|
sync.acquireInterruptibly(1); |
91 |
|
threadShouldThrow(); |
92 |
< |
} catch(InterruptedException success){} |
92 |
> |
} catch (InterruptedException success) {} |
93 |
|
} |
94 |
|
} |
95 |
|
|
125 |
|
/** |
126 |
|
* hasQueuedThreads reports whether there are waiting threads |
127 |
|
*/ |
128 |
< |
public void testhasQueuedThreads() { |
128 |
> |
public void testhasQueuedThreads() throws InterruptedException { |
129 |
|
final Mutex sync = new Mutex(); |
130 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
131 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
132 |
< |
try { |
133 |
< |
assertFalse(sync.hasQueuedThreads()); |
134 |
< |
sync.acquire(1); |
135 |
< |
t1.start(); |
136 |
< |
Thread.sleep(SHORT_DELAY_MS); |
137 |
< |
assertTrue(sync.hasQueuedThreads()); |
138 |
< |
t2.start(); |
139 |
< |
Thread.sleep(SHORT_DELAY_MS); |
140 |
< |
assertTrue(sync.hasQueuedThreads()); |
141 |
< |
t1.interrupt(); |
142 |
< |
Thread.sleep(SHORT_DELAY_MS); |
143 |
< |
assertTrue(sync.hasQueuedThreads()); |
144 |
< |
sync.release(1); |
145 |
< |
Thread.sleep(SHORT_DELAY_MS); |
146 |
< |
assertFalse(sync.hasQueuedThreads()); |
147 |
< |
t1.join(); |
148 |
< |
t2.join(); |
149 |
< |
} catch(Exception e){ |
150 |
< |
unexpectedException(); |
151 |
< |
} |
132 |
> |
assertFalse(sync.hasQueuedThreads()); |
133 |
> |
sync.acquire(1); |
134 |
> |
t1.start(); |
135 |
> |
Thread.sleep(SHORT_DELAY_MS); |
136 |
> |
assertTrue(sync.hasQueuedThreads()); |
137 |
> |
t2.start(); |
138 |
> |
Thread.sleep(SHORT_DELAY_MS); |
139 |
> |
assertTrue(sync.hasQueuedThreads()); |
140 |
> |
t1.interrupt(); |
141 |
> |
Thread.sleep(SHORT_DELAY_MS); |
142 |
> |
assertTrue(sync.hasQueuedThreads()); |
143 |
> |
sync.release(1); |
144 |
> |
Thread.sleep(SHORT_DELAY_MS); |
145 |
> |
assertFalse(sync.hasQueuedThreads()); |
146 |
> |
t1.join(); |
147 |
> |
t2.join(); |
148 |
|
} |
149 |
|
|
150 |
|
/** |
155 |
|
try { |
156 |
|
sync.isQueued(null); |
157 |
|
shouldThrow(); |
158 |
< |
} catch (NullPointerException success) { |
163 |
< |
} |
158 |
> |
} catch (NullPointerException success) {} |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
|
* isQueued reports whether a thread is queued. |
163 |
|
*/ |
164 |
< |
public void testIsQueued() { |
164 |
> |
public void testIsQueued() throws InterruptedException { |
165 |
|
final Mutex sync = new Mutex(); |
166 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
167 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
168 |
< |
try { |
169 |
< |
assertFalse(sync.isQueued(t1)); |
170 |
< |
assertFalse(sync.isQueued(t2)); |
171 |
< |
sync.acquire(1); |
172 |
< |
t1.start(); |
173 |
< |
Thread.sleep(SHORT_DELAY_MS); |
174 |
< |
assertTrue(sync.isQueued(t1)); |
175 |
< |
t2.start(); |
176 |
< |
Thread.sleep(SHORT_DELAY_MS); |
177 |
< |
assertTrue(sync.isQueued(t1)); |
178 |
< |
assertTrue(sync.isQueued(t2)); |
179 |
< |
t1.interrupt(); |
180 |
< |
Thread.sleep(SHORT_DELAY_MS); |
181 |
< |
assertFalse(sync.isQueued(t1)); |
182 |
< |
assertTrue(sync.isQueued(t2)); |
183 |
< |
sync.release(1); |
184 |
< |
Thread.sleep(SHORT_DELAY_MS); |
185 |
< |
assertFalse(sync.isQueued(t1)); |
186 |
< |
Thread.sleep(SHORT_DELAY_MS); |
187 |
< |
assertFalse(sync.isQueued(t2)); |
188 |
< |
t1.join(); |
194 |
< |
t2.join(); |
195 |
< |
} catch(Exception e){ |
196 |
< |
unexpectedException(); |
197 |
< |
} |
168 |
> |
assertFalse(sync.isQueued(t1)); |
169 |
> |
assertFalse(sync.isQueued(t2)); |
170 |
> |
sync.acquire(1); |
171 |
> |
t1.start(); |
172 |
> |
Thread.sleep(SHORT_DELAY_MS); |
173 |
> |
assertTrue(sync.isQueued(t1)); |
174 |
> |
t2.start(); |
175 |
> |
Thread.sleep(SHORT_DELAY_MS); |
176 |
> |
assertTrue(sync.isQueued(t1)); |
177 |
> |
assertTrue(sync.isQueued(t2)); |
178 |
> |
t1.interrupt(); |
179 |
> |
Thread.sleep(SHORT_DELAY_MS); |
180 |
> |
assertFalse(sync.isQueued(t1)); |
181 |
> |
assertTrue(sync.isQueued(t2)); |
182 |
> |
sync.release(1); |
183 |
> |
Thread.sleep(SHORT_DELAY_MS); |
184 |
> |
assertFalse(sync.isQueued(t1)); |
185 |
> |
Thread.sleep(SHORT_DELAY_MS); |
186 |
> |
assertFalse(sync.isQueued(t2)); |
187 |
> |
t1.join(); |
188 |
> |
t2.join(); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
193 |
|
*/ |
194 |
< |
public void testGetFirstQueuedThread() { |
194 |
> |
public void testGetFirstQueuedThread() throws InterruptedException { |
195 |
|
final Mutex sync = new Mutex(); |
196 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
197 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
198 |
< |
try { |
199 |
< |
assertNull(sync.getFirstQueuedThread()); |
200 |
< |
sync.acquire(1); |
201 |
< |
t1.start(); |
202 |
< |
Thread.sleep(SHORT_DELAY_MS); |
203 |
< |
assertEquals(t1, sync.getFirstQueuedThread()); |
204 |
< |
t2.start(); |
205 |
< |
Thread.sleep(SHORT_DELAY_MS); |
206 |
< |
assertEquals(t1, sync.getFirstQueuedThread()); |
207 |
< |
t1.interrupt(); |
208 |
< |
Thread.sleep(SHORT_DELAY_MS); |
209 |
< |
Thread.sleep(SHORT_DELAY_MS); |
210 |
< |
assertEquals(t2, sync.getFirstQueuedThread()); |
211 |
< |
sync.release(1); |
212 |
< |
Thread.sleep(SHORT_DELAY_MS); |
213 |
< |
assertNull(sync.getFirstQueuedThread()); |
214 |
< |
t1.join(); |
224 |
< |
t2.join(); |
225 |
< |
} catch(Exception e){ |
226 |
< |
unexpectedException(); |
227 |
< |
} |
198 |
> |
assertNull(sync.getFirstQueuedThread()); |
199 |
> |
sync.acquire(1); |
200 |
> |
t1.start(); |
201 |
> |
Thread.sleep(SHORT_DELAY_MS); |
202 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
203 |
> |
t2.start(); |
204 |
> |
Thread.sleep(SHORT_DELAY_MS); |
205 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
206 |
> |
t1.interrupt(); |
207 |
> |
Thread.sleep(SHORT_DELAY_MS); |
208 |
> |
Thread.sleep(SHORT_DELAY_MS); |
209 |
> |
assertEquals(t2, sync.getFirstQueuedThread()); |
210 |
> |
sync.release(1); |
211 |
> |
Thread.sleep(SHORT_DELAY_MS); |
212 |
> |
assertNull(sync.getFirstQueuedThread()); |
213 |
> |
t1.join(); |
214 |
> |
t2.join(); |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
/** |
219 |
|
* hasContended reports false if no thread has ever blocked, else true |
220 |
|
*/ |
221 |
< |
public void testHasContended() { |
221 |
> |
public void testHasContended() throws InterruptedException { |
222 |
|
final Mutex sync = new Mutex(); |
223 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
224 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
225 |
< |
try { |
226 |
< |
assertFalse(sync.hasContended()); |
227 |
< |
sync.acquire(1); |
228 |
< |
t1.start(); |
229 |
< |
Thread.sleep(SHORT_DELAY_MS); |
230 |
< |
assertTrue(sync.hasContended()); |
231 |
< |
t2.start(); |
232 |
< |
Thread.sleep(SHORT_DELAY_MS); |
233 |
< |
assertTrue(sync.hasContended()); |
234 |
< |
t1.interrupt(); |
235 |
< |
Thread.sleep(SHORT_DELAY_MS); |
236 |
< |
assertTrue(sync.hasContended()); |
237 |
< |
sync.release(1); |
238 |
< |
Thread.sleep(SHORT_DELAY_MS); |
239 |
< |
assertTrue(sync.hasContended()); |
240 |
< |
t1.join(); |
254 |
< |
t2.join(); |
255 |
< |
} catch(Exception e){ |
256 |
< |
unexpectedException(); |
257 |
< |
} |
225 |
> |
assertFalse(sync.hasContended()); |
226 |
> |
sync.acquire(1); |
227 |
> |
t1.start(); |
228 |
> |
Thread.sleep(SHORT_DELAY_MS); |
229 |
> |
assertTrue(sync.hasContended()); |
230 |
> |
t2.start(); |
231 |
> |
Thread.sleep(SHORT_DELAY_MS); |
232 |
> |
assertTrue(sync.hasContended()); |
233 |
> |
t1.interrupt(); |
234 |
> |
Thread.sleep(SHORT_DELAY_MS); |
235 |
> |
assertTrue(sync.hasContended()); |
236 |
> |
sync.release(1); |
237 |
> |
Thread.sleep(SHORT_DELAY_MS); |
238 |
> |
assertTrue(sync.hasContended()); |
239 |
> |
t1.join(); |
240 |
> |
t2.join(); |
241 |
|
} |
242 |
|
|
243 |
|
/** |
244 |
|
* getQueuedThreads includes waiting threads |
245 |
|
*/ |
246 |
< |
public void testGetQueuedThreads() { |
246 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
247 |
|
final Mutex sync = new Mutex(); |
248 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
249 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
250 |
< |
try { |
251 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
252 |
< |
sync.acquire(1); |
253 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
254 |
< |
t1.start(); |
255 |
< |
Thread.sleep(SHORT_DELAY_MS); |
256 |
< |
assertTrue(sync.getQueuedThreads().contains(t1)); |
257 |
< |
t2.start(); |
258 |
< |
Thread.sleep(SHORT_DELAY_MS); |
259 |
< |
assertTrue(sync.getQueuedThreads().contains(t1)); |
260 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
261 |
< |
t1.interrupt(); |
262 |
< |
Thread.sleep(SHORT_DELAY_MS); |
263 |
< |
assertFalse(sync.getQueuedThreads().contains(t1)); |
264 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
265 |
< |
sync.release(1); |
266 |
< |
Thread.sleep(SHORT_DELAY_MS); |
267 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
268 |
< |
t1.join(); |
286 |
< |
t2.join(); |
287 |
< |
} catch(Exception e){ |
288 |
< |
unexpectedException(); |
289 |
< |
} |
250 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
251 |
> |
sync.acquire(1); |
252 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
253 |
> |
t1.start(); |
254 |
> |
Thread.sleep(SHORT_DELAY_MS); |
255 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
256 |
> |
t2.start(); |
257 |
> |
Thread.sleep(SHORT_DELAY_MS); |
258 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
259 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
260 |
> |
t1.interrupt(); |
261 |
> |
Thread.sleep(SHORT_DELAY_MS); |
262 |
> |
assertFalse(sync.getQueuedThreads().contains(t1)); |
263 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
264 |
> |
sync.release(1); |
265 |
> |
Thread.sleep(SHORT_DELAY_MS); |
266 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
267 |
> |
t1.join(); |
268 |
> |
t2.join(); |
269 |
|
} |
270 |
|
|
271 |
|
/** |
272 |
|
* getExclusiveQueuedThreads includes waiting threads |
273 |
|
*/ |
274 |
< |
public void testGetExclusiveQueuedThreads() { |
274 |
> |
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
275 |
|
final Mutex sync = new Mutex(); |
276 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
277 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
278 |
< |
try { |
279 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
280 |
< |
sync.acquire(1); |
281 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
282 |
< |
t1.start(); |
283 |
< |
Thread.sleep(SHORT_DELAY_MS); |
284 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
285 |
< |
t2.start(); |
286 |
< |
Thread.sleep(SHORT_DELAY_MS); |
287 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
288 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
289 |
< |
t1.interrupt(); |
290 |
< |
Thread.sleep(SHORT_DELAY_MS); |
291 |
< |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
292 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
293 |
< |
sync.release(1); |
294 |
< |
Thread.sleep(SHORT_DELAY_MS); |
295 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
296 |
< |
t1.join(); |
318 |
< |
t2.join(); |
319 |
< |
} catch(Exception e){ |
320 |
< |
unexpectedException(); |
321 |
< |
} |
278 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
279 |
> |
sync.acquire(1); |
280 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
281 |
> |
t1.start(); |
282 |
> |
Thread.sleep(SHORT_DELAY_MS); |
283 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
284 |
> |
t2.start(); |
285 |
> |
Thread.sleep(SHORT_DELAY_MS); |
286 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
287 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
288 |
> |
t1.interrupt(); |
289 |
> |
Thread.sleep(SHORT_DELAY_MS); |
290 |
> |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
291 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
292 |
> |
sync.release(1); |
293 |
> |
Thread.sleep(SHORT_DELAY_MS); |
294 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
295 |
> |
t1.join(); |
296 |
> |
t2.join(); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
301 |
|
*/ |
302 |
< |
public void testGetSharedQueuedThreads() { |
302 |
> |
public void testGetSharedQueuedThreads() throws InterruptedException { |
303 |
|
final Mutex sync = new Mutex(); |
304 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
305 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
306 |
< |
try { |
307 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
308 |
< |
sync.acquire(1); |
309 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
310 |
< |
t1.start(); |
311 |
< |
Thread.sleep(SHORT_DELAY_MS); |
312 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
313 |
< |
t2.start(); |
314 |
< |
Thread.sleep(SHORT_DELAY_MS); |
315 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
316 |
< |
t1.interrupt(); |
317 |
< |
Thread.sleep(SHORT_DELAY_MS); |
318 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
319 |
< |
sync.release(1); |
320 |
< |
Thread.sleep(SHORT_DELAY_MS); |
321 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
322 |
< |
t1.join(); |
348 |
< |
t2.join(); |
349 |
< |
} catch(Exception e){ |
350 |
< |
unexpectedException(); |
351 |
< |
} |
306 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
307 |
> |
sync.acquire(1); |
308 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
309 |
> |
t1.start(); |
310 |
> |
Thread.sleep(SHORT_DELAY_MS); |
311 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
312 |
> |
t2.start(); |
313 |
> |
Thread.sleep(SHORT_DELAY_MS); |
314 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
315 |
> |
t1.interrupt(); |
316 |
> |
Thread.sleep(SHORT_DELAY_MS); |
317 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
318 |
> |
sync.release(1); |
319 |
> |
Thread.sleep(SHORT_DELAY_MS); |
320 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
321 |
> |
t1.join(); |
322 |
> |
t2.join(); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
333 |
|
try { |
334 |
|
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
335 |
|
threadShouldThrow(); |
336 |
< |
} catch(InterruptedException success){} |
336 |
> |
} catch (InterruptedException success) {} |
337 |
|
} |
338 |
|
}); |
339 |
< |
try { |
340 |
< |
t.start(); |
341 |
< |
t.interrupt(); |
371 |
< |
} catch(Exception e){ |
372 |
< |
unexpectedException(); |
373 |
< |
} |
339 |
> |
|
340 |
> |
t.start(); |
341 |
> |
t.interrupt(); |
342 |
|
} |
343 |
|
|
344 |
|
|
345 |
|
/** |
346 |
|
* TryAcquire on exclusively held sync fails |
347 |
|
*/ |
348 |
< |
public void testTryAcquireWhenSynced() { |
348 |
> |
public void testTryAcquireWhenSynced() throws InterruptedException { |
349 |
|
final Mutex sync = new Mutex(); |
350 |
|
sync.acquire(1); |
351 |
|
Thread t = new Thread(new Runnable() { |
353 |
|
threadAssertFalse(sync.tryAcquire(1)); |
354 |
|
} |
355 |
|
}); |
356 |
< |
try { |
357 |
< |
t.start(); |
358 |
< |
t.join(); |
359 |
< |
sync.release(1); |
392 |
< |
} catch(Exception e){ |
393 |
< |
unexpectedException(); |
394 |
< |
} |
356 |
> |
|
357 |
> |
t.start(); |
358 |
> |
t.join(); |
359 |
> |
sync.release(1); |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
|
* tryAcquireNanos on an exclusively held sync times out |
364 |
|
*/ |
365 |
< |
public void testAcquireNanos_Timeout() { |
365 |
> |
public void testAcquireNanos_Timeout() throws InterruptedException { |
366 |
|
final Mutex sync = new Mutex(); |
367 |
|
sync.acquire(1); |
368 |
|
Thread t = new Thread(new Runnable() { |
374 |
|
} |
375 |
|
} |
376 |
|
}); |
377 |
< |
try { |
378 |
< |
t.start(); |
379 |
< |
t.join(); |
380 |
< |
sync.release(1); |
416 |
< |
} catch(Exception e){ |
417 |
< |
unexpectedException(); |
418 |
< |
} |
377 |
> |
|
378 |
> |
t.start(); |
379 |
> |
t.join(); |
380 |
> |
sync.release(1); |
381 |
|
} |
382 |
|
|
383 |
|
|
384 |
|
/** |
385 |
|
* getState is true when acquired and false when not |
386 |
|
*/ |
387 |
< |
public void testGetState() { |
387 |
> |
public void testGetState() throws InterruptedException { |
388 |
|
final Mutex sync = new Mutex(); |
389 |
|
sync.acquire(1); |
390 |
|
assertTrue(sync.isHeldExclusively()); |
396 |
|
try { |
397 |
|
Thread.sleep(SMALL_DELAY_MS); |
398 |
|
} |
399 |
< |
catch(Exception e) { |
399 |
> |
catch (Exception e) { |
400 |
|
threadUnexpectedException(); |
401 |
|
} |
402 |
|
sync.release(1); |
403 |
|
} |
404 |
|
}); |
405 |
< |
try { |
406 |
< |
t.start(); |
407 |
< |
Thread.sleep(SHORT_DELAY_MS); |
408 |
< |
assertTrue(sync.isHeldExclusively()); |
409 |
< |
t.join(); |
410 |
< |
assertFalse(sync.isHeldExclusively()); |
449 |
< |
} catch(Exception e){ |
450 |
< |
unexpectedException(); |
451 |
< |
} |
405 |
> |
|
406 |
> |
t.start(); |
407 |
> |
Thread.sleep(SHORT_DELAY_MS); |
408 |
> |
assertTrue(sync.isHeldExclusively()); |
409 |
> |
t.join(); |
410 |
> |
assertFalse(sync.isHeldExclusively()); |
411 |
|
} |
412 |
|
|
413 |
|
|
414 |
|
/** |
415 |
|
* acquireInterruptibly is interruptible. |
416 |
|
*/ |
417 |
< |
public void testAcquireInterruptibly1() { |
417 |
> |
public void testAcquireInterruptibly1() throws InterruptedException { |
418 |
|
final Mutex sync = new Mutex(); |
419 |
|
sync.acquire(1); |
420 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
421 |
< |
try { |
422 |
< |
t.start(); |
423 |
< |
Thread.sleep(SHORT_DELAY_MS); |
424 |
< |
t.interrupt(); |
425 |
< |
Thread.sleep(SHORT_DELAY_MS); |
426 |
< |
sync.release(1); |
427 |
< |
t.join(); |
469 |
< |
} catch(Exception e){ |
470 |
< |
unexpectedException(); |
471 |
< |
} |
421 |
> |
|
422 |
> |
t.start(); |
423 |
> |
Thread.sleep(SHORT_DELAY_MS); |
424 |
> |
t.interrupt(); |
425 |
> |
Thread.sleep(SHORT_DELAY_MS); |
426 |
> |
sync.release(1); |
427 |
> |
t.join(); |
428 |
|
} |
429 |
|
|
430 |
|
/** |
431 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
432 |
|
*/ |
433 |
< |
public void testAcquireInterruptibly2() { |
433 |
> |
public void testAcquireInterruptibly2() throws InterruptedException { |
434 |
|
final Mutex sync = new Mutex(); |
435 |
< |
try { |
480 |
< |
sync.acquireInterruptibly(1); |
481 |
< |
} catch(Exception e) { |
482 |
< |
unexpectedException(); |
483 |
< |
} |
435 |
> |
sync.acquireInterruptibly(1); |
436 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
437 |
< |
try { |
438 |
< |
t.start(); |
439 |
< |
t.interrupt(); |
440 |
< |
assertTrue(sync.isHeldExclusively()); |
489 |
< |
t.join(); |
490 |
< |
} catch(Exception e){ |
491 |
< |
unexpectedException(); |
492 |
< |
} |
437 |
> |
t.start(); |
438 |
> |
t.interrupt(); |
439 |
> |
assertTrue(sync.isHeldExclusively()); |
440 |
> |
t.join(); |
441 |
|
} |
442 |
|
|
443 |
|
/** |
454 |
|
/** |
455 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
456 |
|
*/ |
457 |
< |
public void testAwait_IllegalMonitor() { |
457 |
> |
public void testAwait_IllegalMonitor() throws InterruptedException { |
458 |
|
final Mutex sync = new Mutex(); |
459 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
460 |
|
try { |
461 |
|
c.await(); |
462 |
|
shouldThrow(); |
463 |
|
} |
464 |
< |
catch (IllegalMonitorStateException success) { |
517 |
< |
} |
518 |
< |
catch (Exception ex) { |
519 |
< |
unexpectedException(); |
520 |
< |
} |
464 |
> |
catch (IllegalMonitorStateException success) {} |
465 |
|
} |
466 |
|
|
467 |
|
/** |
474 |
|
c.signal(); |
475 |
|
shouldThrow(); |
476 |
|
} |
477 |
< |
catch (IllegalMonitorStateException success) { |
534 |
< |
} |
535 |
< |
catch (Exception ex) { |
536 |
< |
unexpectedException(); |
537 |
< |
} |
477 |
> |
catch (IllegalMonitorStateException success) {} |
478 |
|
} |
479 |
|
|
480 |
|
/** |
481 |
|
* awaitNanos without a signal times out |
482 |
|
*/ |
483 |
< |
public void testAwaitNanos_Timeout() { |
483 |
> |
public void testAwaitNanos_Timeout() throws InterruptedException { |
484 |
|
final Mutex sync = new Mutex(); |
485 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
486 |
< |
try { |
487 |
< |
sync.acquire(1); |
488 |
< |
long t = c.awaitNanos(100); |
489 |
< |
assertTrue(t <= 0); |
550 |
< |
sync.release(1); |
551 |
< |
} |
552 |
< |
catch (Exception ex) { |
553 |
< |
unexpectedException(); |
554 |
< |
} |
486 |
> |
sync.acquire(1); |
487 |
> |
long t = c.awaitNanos(100); |
488 |
> |
assertTrue(t <= 0); |
489 |
> |
sync.release(1); |
490 |
|
} |
491 |
|
|
492 |
|
/** |
493 |
|
* Timed await without a signal times out |
494 |
|
*/ |
495 |
< |
public void testAwait_Timeout() { |
495 |
> |
public void testAwait_Timeout() throws InterruptedException { |
496 |
|
final Mutex sync = new Mutex(); |
497 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
498 |
< |
try { |
499 |
< |
sync.acquire(1); |
500 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
566 |
< |
sync.release(1); |
567 |
< |
} |
568 |
< |
catch (Exception ex) { |
569 |
< |
unexpectedException(); |
570 |
< |
} |
498 |
> |
sync.acquire(1); |
499 |
> |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
500 |
> |
sync.release(1); |
501 |
|
} |
502 |
|
|
503 |
|
/** |
504 |
|
* awaitUntil without a signal times out |
505 |
|
*/ |
506 |
< |
public void testAwaitUntil_Timeout() { |
506 |
> |
public void testAwaitUntil_Timeout() throws InterruptedException { |
507 |
|
final Mutex sync = new Mutex(); |
508 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
509 |
< |
try { |
510 |
< |
sync.acquire(1); |
511 |
< |
java.util.Date d = new java.util.Date(); |
512 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
583 |
< |
sync.release(1); |
584 |
< |
} |
585 |
< |
catch (Exception ex) { |
586 |
< |
unexpectedException(); |
587 |
< |
} |
509 |
> |
sync.acquire(1); |
510 |
> |
java.util.Date d = new java.util.Date(); |
511 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
512 |
> |
sync.release(1); |
513 |
|
} |
514 |
|
|
515 |
|
/** |
516 |
|
* await returns when signalled |
517 |
|
*/ |
518 |
< |
public void testAwait() { |
518 |
> |
public void testAwait() throws InterruptedException { |
519 |
|
final Mutex sync = new Mutex(); |
520 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
521 |
|
Thread t = new Thread(new Runnable() { |
525 |
|
c.await(); |
526 |
|
sync.release(1); |
527 |
|
} |
528 |
< |
catch(InterruptedException e) { |
528 |
> |
catch (InterruptedException e) { |
529 |
|
threadUnexpectedException(); |
530 |
|
} |
531 |
|
} |
532 |
|
}); |
533 |
|
|
534 |
< |
try { |
535 |
< |
t.start(); |
536 |
< |
Thread.sleep(SHORT_DELAY_MS); |
537 |
< |
sync.acquire(1); |
538 |
< |
c.signal(); |
539 |
< |
sync.release(1); |
540 |
< |
t.join(SHORT_DELAY_MS); |
616 |
< |
assertFalse(t.isAlive()); |
617 |
< |
} |
618 |
< |
catch (Exception ex) { |
619 |
< |
unexpectedException(); |
620 |
< |
} |
534 |
> |
t.start(); |
535 |
> |
Thread.sleep(SHORT_DELAY_MS); |
536 |
> |
sync.acquire(1); |
537 |
> |
c.signal(); |
538 |
> |
sync.release(1); |
539 |
> |
t.join(SHORT_DELAY_MS); |
540 |
> |
assertFalse(t.isAlive()); |
541 |
|
} |
542 |
|
|
543 |
|
|
550 |
|
try { |
551 |
|
sync.hasWaiters(null); |
552 |
|
shouldThrow(); |
553 |
< |
} catch (NullPointerException success) { |
634 |
< |
} catch (Exception ex) { |
635 |
< |
unexpectedException(); |
636 |
< |
} |
553 |
> |
} catch (NullPointerException success) {} |
554 |
|
} |
555 |
|
|
556 |
|
/** |
561 |
|
try { |
562 |
|
sync.getWaitQueueLength(null); |
563 |
|
shouldThrow(); |
564 |
< |
} catch (NullPointerException success) { |
648 |
< |
} catch (Exception ex) { |
649 |
< |
unexpectedException(); |
650 |
< |
} |
564 |
> |
} catch (NullPointerException success) {} |
565 |
|
} |
566 |
|
|
567 |
|
|
573 |
|
try { |
574 |
|
sync.getWaitingThreads(null); |
575 |
|
shouldThrow(); |
576 |
< |
} catch (NullPointerException success) { |
663 |
< |
} catch (Exception ex) { |
664 |
< |
unexpectedException(); |
665 |
< |
} |
576 |
> |
} catch (NullPointerException success) {} |
577 |
|
} |
578 |
|
|
579 |
|
|
582 |
|
*/ |
583 |
|
public void testHasWaitersIAE() { |
584 |
|
final Mutex sync = new Mutex(); |
585 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
585 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
586 |
|
final Mutex sync2 = new Mutex(); |
587 |
|
try { |
588 |
|
sync2.hasWaiters(c); |
589 |
|
shouldThrow(); |
590 |
< |
} catch (IllegalArgumentException success) { |
680 |
< |
} catch (Exception ex) { |
681 |
< |
unexpectedException(); |
682 |
< |
} |
590 |
> |
} catch (IllegalArgumentException success) {} |
591 |
|
} |
592 |
|
|
593 |
|
/** |
595 |
|
*/ |
596 |
|
public void testHasWaitersIMSE() { |
597 |
|
final Mutex sync = new Mutex(); |
598 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
598 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
599 |
|
try { |
600 |
|
sync.hasWaiters(c); |
601 |
|
shouldThrow(); |
602 |
< |
} catch (IllegalMonitorStateException success) { |
695 |
< |
} catch (Exception ex) { |
696 |
< |
unexpectedException(); |
697 |
< |
} |
602 |
> |
} catch (IllegalMonitorStateException success) {} |
603 |
|
} |
604 |
|
|
605 |
|
|
608 |
|
*/ |
609 |
|
public void testGetWaitQueueLengthIAE() { |
610 |
|
final Mutex sync = new Mutex(); |
611 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
611 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
612 |
|
final Mutex sync2 = new Mutex(); |
613 |
|
try { |
614 |
|
sync2.getWaitQueueLength(c); |
615 |
|
shouldThrow(); |
616 |
< |
} catch (IllegalArgumentException success) { |
712 |
< |
} catch (Exception ex) { |
713 |
< |
unexpectedException(); |
714 |
< |
} |
616 |
> |
} catch (IllegalArgumentException success) {} |
617 |
|
} |
618 |
|
|
619 |
|
/** |
621 |
|
*/ |
622 |
|
public void testGetWaitQueueLengthIMSE() { |
623 |
|
final Mutex sync = new Mutex(); |
624 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
624 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
625 |
|
try { |
626 |
|
sync.getWaitQueueLength(c); |
627 |
|
shouldThrow(); |
628 |
< |
} catch (IllegalMonitorStateException success) { |
727 |
< |
} catch (Exception ex) { |
728 |
< |
unexpectedException(); |
729 |
< |
} |
628 |
> |
} catch (IllegalMonitorStateException success) {} |
629 |
|
} |
630 |
|
|
631 |
|
|
634 |
|
*/ |
635 |
|
public void testGetWaitingThreadsIAE() { |
636 |
|
final Mutex sync = new Mutex(); |
637 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
637 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
638 |
|
final Mutex sync2 = new Mutex(); |
639 |
|
try { |
640 |
|
sync2.getWaitingThreads(c); |
641 |
|
shouldThrow(); |
642 |
< |
} catch (IllegalArgumentException success) { |
744 |
< |
} catch (Exception ex) { |
745 |
< |
unexpectedException(); |
746 |
< |
} |
642 |
> |
} catch (IllegalArgumentException success) {} |
643 |
|
} |
644 |
|
|
645 |
|
/** |
647 |
|
*/ |
648 |
|
public void testGetWaitingThreadsIMSE() { |
649 |
|
final Mutex sync = new Mutex(); |
650 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
650 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
651 |
|
try { |
652 |
|
sync.getWaitingThreads(c); |
653 |
|
shouldThrow(); |
654 |
< |
} catch (IllegalMonitorStateException success) { |
759 |
< |
} catch (Exception ex) { |
760 |
< |
unexpectedException(); |
761 |
< |
} |
654 |
> |
} catch (IllegalMonitorStateException success) {} |
655 |
|
} |
656 |
|
|
657 |
|
|
659 |
|
/** |
660 |
|
* hasWaiters returns true when a thread is waiting, else false |
661 |
|
*/ |
662 |
< |
public void testHasWaiters() { |
662 |
> |
public void testHasWaiters() throws InterruptedException { |
663 |
|
final Mutex sync = new Mutex(); |
664 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
665 |
|
Thread t = new Thread(new Runnable() { |
671 |
|
c.await(); |
672 |
|
sync.release(1); |
673 |
|
} |
674 |
< |
catch(InterruptedException e) { |
674 |
> |
catch (InterruptedException e) { |
675 |
|
threadUnexpectedException(); |
676 |
|
} |
677 |
|
} |
678 |
|
}); |
679 |
|
|
680 |
< |
try { |
681 |
< |
t.start(); |
682 |
< |
Thread.sleep(SHORT_DELAY_MS); |
683 |
< |
sync.acquire(1); |
684 |
< |
assertTrue(sync.hasWaiters(c)); |
685 |
< |
assertEquals(1, sync.getWaitQueueLength(c)); |
686 |
< |
c.signal(); |
687 |
< |
sync.release(1); |
688 |
< |
Thread.sleep(SHORT_DELAY_MS); |
689 |
< |
sync.acquire(1); |
690 |
< |
assertFalse(sync.hasWaiters(c)); |
691 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
692 |
< |
sync.release(1); |
693 |
< |
t.join(SHORT_DELAY_MS); |
801 |
< |
assertFalse(t.isAlive()); |
802 |
< |
} |
803 |
< |
catch (Exception ex) { |
804 |
< |
unexpectedException(); |
805 |
< |
} |
680 |
> |
t.start(); |
681 |
> |
Thread.sleep(SHORT_DELAY_MS); |
682 |
> |
sync.acquire(1); |
683 |
> |
assertTrue(sync.hasWaiters(c)); |
684 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
685 |
> |
c.signal(); |
686 |
> |
sync.release(1); |
687 |
> |
Thread.sleep(SHORT_DELAY_MS); |
688 |
> |
sync.acquire(1); |
689 |
> |
assertFalse(sync.hasWaiters(c)); |
690 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
691 |
> |
sync.release(1); |
692 |
> |
t.join(SHORT_DELAY_MS); |
693 |
> |
assertFalse(t.isAlive()); |
694 |
|
} |
695 |
|
|
696 |
|
/** |
697 |
|
* getWaitQueueLength returns number of waiting threads |
698 |
|
*/ |
699 |
< |
public void testGetWaitQueueLength() { |
699 |
> |
public void testGetWaitQueueLength() throws InterruptedException { |
700 |
|
final Mutex sync = new Mutex(); |
701 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
702 |
|
Thread t1 = new Thread(new Runnable() { |
708 |
|
c.await(); |
709 |
|
sync.release(1); |
710 |
|
} |
711 |
< |
catch(InterruptedException e) { |
711 |
> |
catch (InterruptedException e) { |
712 |
|
threadUnexpectedException(); |
713 |
|
} |
714 |
|
} |
723 |
|
c.await(); |
724 |
|
sync.release(1); |
725 |
|
} |
726 |
< |
catch(InterruptedException e) { |
726 |
> |
catch (InterruptedException e) { |
727 |
|
threadUnexpectedException(); |
728 |
|
} |
729 |
|
} |
730 |
|
}); |
731 |
|
|
732 |
< |
try { |
733 |
< |
t1.start(); |
734 |
< |
Thread.sleep(SHORT_DELAY_MS); |
735 |
< |
t2.start(); |
736 |
< |
Thread.sleep(SHORT_DELAY_MS); |
737 |
< |
sync.acquire(1); |
738 |
< |
assertTrue(sync.hasWaiters(c)); |
739 |
< |
assertEquals(2, sync.getWaitQueueLength(c)); |
740 |
< |
c.signalAll(); |
741 |
< |
sync.release(1); |
742 |
< |
Thread.sleep(SHORT_DELAY_MS); |
743 |
< |
sync.acquire(1); |
744 |
< |
assertFalse(sync.hasWaiters(c)); |
745 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
746 |
< |
sync.release(1); |
747 |
< |
t1.join(SHORT_DELAY_MS); |
748 |
< |
t2.join(SHORT_DELAY_MS); |
749 |
< |
assertFalse(t1.isAlive()); |
862 |
< |
assertFalse(t2.isAlive()); |
863 |
< |
} |
864 |
< |
catch (Exception ex) { |
865 |
< |
unexpectedException(); |
866 |
< |
} |
732 |
> |
t1.start(); |
733 |
> |
Thread.sleep(SHORT_DELAY_MS); |
734 |
> |
t2.start(); |
735 |
> |
Thread.sleep(SHORT_DELAY_MS); |
736 |
> |
sync.acquire(1); |
737 |
> |
assertTrue(sync.hasWaiters(c)); |
738 |
> |
assertEquals(2, sync.getWaitQueueLength(c)); |
739 |
> |
c.signalAll(); |
740 |
> |
sync.release(1); |
741 |
> |
Thread.sleep(SHORT_DELAY_MS); |
742 |
> |
sync.acquire(1); |
743 |
> |
assertFalse(sync.hasWaiters(c)); |
744 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
745 |
> |
sync.release(1); |
746 |
> |
t1.join(SHORT_DELAY_MS); |
747 |
> |
t2.join(SHORT_DELAY_MS); |
748 |
> |
assertFalse(t1.isAlive()); |
749 |
> |
assertFalse(t2.isAlive()); |
750 |
|
} |
751 |
|
|
752 |
|
/** |
753 |
|
* getWaitingThreads returns only and all waiting threads |
754 |
|
*/ |
755 |
< |
public void testGetWaitingThreads() { |
755 |
> |
public void testGetWaitingThreads() throws InterruptedException { |
756 |
|
final Mutex sync = new Mutex(); |
757 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
758 |
|
Thread t1 = new Thread(new Runnable() { |
763 |
|
c.await(); |
764 |
|
sync.release(1); |
765 |
|
} |
766 |
< |
catch(InterruptedException e) { |
766 |
> |
catch (InterruptedException e) { |
767 |
|
threadUnexpectedException(); |
768 |
|
} |
769 |
|
} |
777 |
|
c.await(); |
778 |
|
sync.release(1); |
779 |
|
} |
780 |
< |
catch(InterruptedException e) { |
780 |
> |
catch (InterruptedException e) { |
781 |
|
threadUnexpectedException(); |
782 |
|
} |
783 |
|
} |
784 |
|
}); |
785 |
|
|
786 |
< |
try { |
787 |
< |
sync.acquire(1); |
788 |
< |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
789 |
< |
sync.release(1); |
790 |
< |
t1.start(); |
791 |
< |
Thread.sleep(SHORT_DELAY_MS); |
792 |
< |
t2.start(); |
793 |
< |
Thread.sleep(SHORT_DELAY_MS); |
794 |
< |
sync.acquire(1); |
795 |
< |
assertTrue(sync.hasWaiters(c)); |
796 |
< |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
797 |
< |
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
798 |
< |
c.signalAll(); |
799 |
< |
sync.release(1); |
800 |
< |
Thread.sleep(SHORT_DELAY_MS); |
801 |
< |
sync.acquire(1); |
802 |
< |
assertFalse(sync.hasWaiters(c)); |
803 |
< |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
804 |
< |
sync.release(1); |
805 |
< |
t1.join(SHORT_DELAY_MS); |
806 |
< |
t2.join(SHORT_DELAY_MS); |
807 |
< |
assertFalse(t1.isAlive()); |
925 |
< |
assertFalse(t2.isAlive()); |
926 |
< |
} |
927 |
< |
catch (Exception ex) { |
928 |
< |
unexpectedException(); |
929 |
< |
} |
786 |
> |
sync.acquire(1); |
787 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
788 |
> |
sync.release(1); |
789 |
> |
t1.start(); |
790 |
> |
Thread.sleep(SHORT_DELAY_MS); |
791 |
> |
t2.start(); |
792 |
> |
Thread.sleep(SHORT_DELAY_MS); |
793 |
> |
sync.acquire(1); |
794 |
> |
assertTrue(sync.hasWaiters(c)); |
795 |
> |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
796 |
> |
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
797 |
> |
c.signalAll(); |
798 |
> |
sync.release(1); |
799 |
> |
Thread.sleep(SHORT_DELAY_MS); |
800 |
> |
sync.acquire(1); |
801 |
> |
assertFalse(sync.hasWaiters(c)); |
802 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
803 |
> |
sync.release(1); |
804 |
> |
t1.join(SHORT_DELAY_MS); |
805 |
> |
t2.join(SHORT_DELAY_MS); |
806 |
> |
assertFalse(t1.isAlive()); |
807 |
> |
assertFalse(t2.isAlive()); |
808 |
|
} |
809 |
|
|
810 |
|
|
812 |
|
/** |
813 |
|
* awaitUninterruptibly doesn't abort on interrupt |
814 |
|
*/ |
815 |
< |
public void testAwaitUninterruptibly() { |
815 |
> |
public void testAwaitUninterruptibly() throws InterruptedException { |
816 |
|
final Mutex sync = new Mutex(); |
817 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
818 |
|
Thread t = new Thread(new Runnable() { |
823 |
|
} |
824 |
|
}); |
825 |
|
|
826 |
< |
try { |
827 |
< |
t.start(); |
828 |
< |
Thread.sleep(SHORT_DELAY_MS); |
829 |
< |
t.interrupt(); |
830 |
< |
sync.acquire(1); |
831 |
< |
c.signal(); |
832 |
< |
sync.release(1); |
833 |
< |
t.join(SHORT_DELAY_MS); |
956 |
< |
assertFalse(t.isAlive()); |
957 |
< |
} |
958 |
< |
catch (Exception ex) { |
959 |
< |
unexpectedException(); |
960 |
< |
} |
826 |
> |
t.start(); |
827 |
> |
Thread.sleep(SHORT_DELAY_MS); |
828 |
> |
t.interrupt(); |
829 |
> |
sync.acquire(1); |
830 |
> |
c.signal(); |
831 |
> |
sync.release(1); |
832 |
> |
t.join(SHORT_DELAY_MS); |
833 |
> |
assertFalse(t.isAlive()); |
834 |
|
} |
835 |
|
|
836 |
|
/** |
837 |
|
* await is interruptible |
838 |
|
*/ |
839 |
< |
public void testAwait_Interrupt() { |
839 |
> |
public void testAwait_Interrupt() throws InterruptedException { |
840 |
|
final Mutex sync = new Mutex(); |
841 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
842 |
|
Thread t = new Thread(new Runnable() { |
847 |
|
sync.release(1); |
848 |
|
threadShouldThrow(); |
849 |
|
} |
850 |
< |
catch(InterruptedException success) { |
850 |
> |
catch (InterruptedException success) { |
851 |
|
} |
852 |
|
} |
853 |
|
}); |
854 |
|
|
855 |
< |
try { |
856 |
< |
t.start(); |
857 |
< |
Thread.sleep(SHORT_DELAY_MS); |
858 |
< |
t.interrupt(); |
859 |
< |
t.join(SHORT_DELAY_MS); |
987 |
< |
assertFalse(t.isAlive()); |
988 |
< |
} |
989 |
< |
catch (Exception ex) { |
990 |
< |
unexpectedException(); |
991 |
< |
} |
855 |
> |
t.start(); |
856 |
> |
Thread.sleep(SHORT_DELAY_MS); |
857 |
> |
t.interrupt(); |
858 |
> |
t.join(SHORT_DELAY_MS); |
859 |
> |
assertFalse(t.isAlive()); |
860 |
|
} |
861 |
|
|
862 |
|
/** |
863 |
|
* awaitNanos is interruptible |
864 |
|
*/ |
865 |
< |
public void testAwaitNanos_Interrupt() { |
865 |
> |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
866 |
|
final Mutex sync = new Mutex(); |
867 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
868 |
|
Thread t = new Thread(new Runnable() { |
873 |
|
sync.release(1); |
874 |
|
threadShouldThrow(); |
875 |
|
} |
876 |
< |
catch(InterruptedException success) { |
876 |
> |
catch (InterruptedException success) { |
877 |
|
} |
878 |
|
} |
879 |
|
}); |
880 |
|
|
881 |
< |
try { |
882 |
< |
t.start(); |
883 |
< |
Thread.sleep(SHORT_DELAY_MS); |
884 |
< |
t.interrupt(); |
885 |
< |
t.join(SHORT_DELAY_MS); |
1018 |
< |
assertFalse(t.isAlive()); |
1019 |
< |
} |
1020 |
< |
catch (Exception ex) { |
1021 |
< |
unexpectedException(); |
1022 |
< |
} |
881 |
> |
t.start(); |
882 |
> |
Thread.sleep(SHORT_DELAY_MS); |
883 |
> |
t.interrupt(); |
884 |
> |
t.join(SHORT_DELAY_MS); |
885 |
> |
assertFalse(t.isAlive()); |
886 |
|
} |
887 |
|
|
888 |
|
/** |
889 |
|
* awaitUntil is interruptible |
890 |
|
*/ |
891 |
< |
public void testAwaitUntil_Interrupt() { |
891 |
> |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
892 |
|
final Mutex sync = new Mutex(); |
893 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
894 |
|
Thread t = new Thread(new Runnable() { |
900 |
|
sync.release(1); |
901 |
|
threadShouldThrow(); |
902 |
|
} |
903 |
< |
catch(InterruptedException success) { |
903 |
> |
catch (InterruptedException success) { |
904 |
|
} |
905 |
|
} |
906 |
|
}); |
907 |
|
|
908 |
< |
try { |
909 |
< |
t.start(); |
910 |
< |
Thread.sleep(SHORT_DELAY_MS); |
911 |
< |
t.interrupt(); |
912 |
< |
t.join(SHORT_DELAY_MS); |
1050 |
< |
assertFalse(t.isAlive()); |
1051 |
< |
} |
1052 |
< |
catch (Exception ex) { |
1053 |
< |
unexpectedException(); |
1054 |
< |
} |
908 |
> |
t.start(); |
909 |
> |
Thread.sleep(SHORT_DELAY_MS); |
910 |
> |
t.interrupt(); |
911 |
> |
t.join(SHORT_DELAY_MS); |
912 |
> |
assertFalse(t.isAlive()); |
913 |
|
} |
914 |
|
|
915 |
|
/** |
916 |
|
* signalAll wakes up all threads |
917 |
|
*/ |
918 |
< |
public void testSignalAll() { |
918 |
> |
public void testSignalAll() throws InterruptedException { |
919 |
|
final Mutex sync = new Mutex(); |
920 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
921 |
|
Thread t1 = new Thread(new Runnable() { |
925 |
|
c.await(); |
926 |
|
sync.release(1); |
927 |
|
} |
928 |
< |
catch(InterruptedException e) { |
928 |
> |
catch (InterruptedException e) { |
929 |
|
threadUnexpectedException(); |
930 |
|
} |
931 |
|
} |
938 |
|
c.await(); |
939 |
|
sync.release(1); |
940 |
|
} |
941 |
< |
catch(InterruptedException e) { |
941 |
> |
catch (InterruptedException e) { |
942 |
|
threadUnexpectedException(); |
943 |
|
} |
944 |
|
} |
945 |
|
}); |
946 |
|
|
947 |
< |
try { |
948 |
< |
t1.start(); |
949 |
< |
t2.start(); |
950 |
< |
Thread.sleep(SHORT_DELAY_MS); |
951 |
< |
sync.acquire(1); |
952 |
< |
c.signalAll(); |
953 |
< |
sync.release(1); |
954 |
< |
t1.join(SHORT_DELAY_MS); |
955 |
< |
t2.join(SHORT_DELAY_MS); |
956 |
< |
assertFalse(t1.isAlive()); |
1099 |
< |
assertFalse(t2.isAlive()); |
1100 |
< |
} |
1101 |
< |
catch (Exception ex) { |
1102 |
< |
unexpectedException(); |
1103 |
< |
} |
947 |
> |
t1.start(); |
948 |
> |
t2.start(); |
949 |
> |
Thread.sleep(SHORT_DELAY_MS); |
950 |
> |
sync.acquire(1); |
951 |
> |
c.signalAll(); |
952 |
> |
sync.release(1); |
953 |
> |
t1.join(SHORT_DELAY_MS); |
954 |
> |
t2.join(SHORT_DELAY_MS); |
955 |
> |
assertFalse(t1.isAlive()); |
956 |
> |
assertFalse(t2.isAlive()); |
957 |
|
} |
958 |
|
|
959 |
|
|
972 |
|
/** |
973 |
|
* A serialized AQS deserializes with current state |
974 |
|
*/ |
975 |
< |
public void testSerialization() { |
975 |
> |
public void testSerialization() throws Exception { |
976 |
|
Mutex l = new Mutex(); |
977 |
|
l.acquire(1); |
978 |
|
assertTrue(l.isHeldExclusively()); |
979 |
|
|
980 |
< |
try { |
981 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
982 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
983 |
< |
out.writeObject(l); |
984 |
< |
out.close(); |
985 |
< |
|
986 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
987 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
988 |
< |
Mutex r = (Mutex) in.readObject(); |
1136 |
< |
assertTrue(r.isHeldExclusively()); |
1137 |
< |
} catch(Exception e){ |
1138 |
< |
e.printStackTrace(); |
1139 |
< |
unexpectedException(); |
1140 |
< |
} |
980 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
981 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
982 |
> |
out.writeObject(l); |
983 |
> |
out.close(); |
984 |
> |
|
985 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
986 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
987 |
> |
Mutex r = (Mutex) in.readObject(); |
988 |
> |
assertTrue(r.isHeldExclusively()); |
989 |
|
} |
990 |
|
|
991 |
|
|
1014 |
|
/** |
1015 |
|
* acquireSharedInterruptibly returns after release, but not before |
1016 |
|
*/ |
1017 |
< |
public void testAcquireSharedInterruptibly() { |
1017 |
> |
public void testAcquireSharedInterruptibly() throws InterruptedException { |
1018 |
|
final BooleanLatch l = new BooleanLatch(); |
1019 |
|
|
1020 |
|
Thread t = new Thread(new Runnable() { |
1023 |
|
threadAssertFalse(l.isSignalled()); |
1024 |
|
l.acquireSharedInterruptibly(0); |
1025 |
|
threadAssertTrue(l.isSignalled()); |
1026 |
< |
} catch(InterruptedException e){ |
1026 |
> |
} catch (InterruptedException e) { |
1027 |
|
threadUnexpectedException(); |
1028 |
|
} |
1029 |
|
} |
1030 |
|
}); |
1031 |
< |
try { |
1032 |
< |
t.start(); |
1033 |
< |
assertFalse(l.isSignalled()); |
1034 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1035 |
< |
l.releaseShared(0); |
1036 |
< |
assertTrue(l.isSignalled()); |
1037 |
< |
t.join(); |
1190 |
< |
} catch (InterruptedException e){ |
1191 |
< |
unexpectedException(); |
1192 |
< |
} |
1031 |
> |
|
1032 |
> |
t.start(); |
1033 |
> |
assertFalse(l.isSignalled()); |
1034 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1035 |
> |
l.releaseShared(0); |
1036 |
> |
assertTrue(l.isSignalled()); |
1037 |
> |
t.join(); |
1038 |
|
} |
1039 |
|
|
1040 |
|
|
1041 |
|
/** |
1042 |
|
* acquireSharedTimed returns after release |
1043 |
|
*/ |
1044 |
< |
public void testAsquireSharedTimed() { |
1044 |
> |
public void testAsquireSharedTimed() throws InterruptedException { |
1045 |
|
final BooleanLatch l = new BooleanLatch(); |
1046 |
|
|
1047 |
|
Thread t = new Thread(new Runnable() { |
1051 |
|
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
1052 |
|
threadAssertTrue(l.isSignalled()); |
1053 |
|
|
1054 |
< |
} catch(InterruptedException e){ |
1054 |
> |
} catch (InterruptedException e) { |
1055 |
|
threadUnexpectedException(); |
1056 |
|
} |
1057 |
|
} |
1058 |
|
}); |
1059 |
< |
try { |
1060 |
< |
t.start(); |
1061 |
< |
assertFalse(l.isSignalled()); |
1062 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1063 |
< |
l.releaseShared(0); |
1064 |
< |
assertTrue(l.isSignalled()); |
1065 |
< |
t.join(); |
1221 |
< |
} catch (InterruptedException e){ |
1222 |
< |
unexpectedException(); |
1223 |
< |
} |
1059 |
> |
|
1060 |
> |
t.start(); |
1061 |
> |
assertFalse(l.isSignalled()); |
1062 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1063 |
> |
l.releaseShared(0); |
1064 |
> |
assertTrue(l.isSignalled()); |
1065 |
> |
t.join(); |
1066 |
|
} |
1067 |
|
|
1068 |
|
/** |
1069 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
1070 |
|
*/ |
1071 |
< |
public void testAcquireSharedInterruptibly_InterruptedException() { |
1071 |
> |
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
1072 |
|
final BooleanLatch l = new BooleanLatch(); |
1073 |
|
Thread t = new Thread(new Runnable() { |
1074 |
|
public void run() { |
1076 |
|
threadAssertFalse(l.isSignalled()); |
1077 |
|
l.acquireSharedInterruptibly(0); |
1078 |
|
threadShouldThrow(); |
1079 |
< |
} catch(InterruptedException success){} |
1079 |
> |
} catch (InterruptedException success) {} |
1080 |
|
} |
1081 |
|
}); |
1082 |
|
t.start(); |
1083 |
< |
try { |
1084 |
< |
assertFalse(l.isSignalled()); |
1085 |
< |
t.interrupt(); |
1244 |
< |
t.join(); |
1245 |
< |
} catch (InterruptedException e){ |
1246 |
< |
unexpectedException(); |
1247 |
< |
} |
1083 |
> |
assertFalse(l.isSignalled()); |
1084 |
> |
t.interrupt(); |
1085 |
> |
t.join(); |
1086 |
|
} |
1087 |
|
|
1088 |
|
/** |
1089 |
|
* acquireSharedTimed throws IE if interrupted before released |
1090 |
|
*/ |
1091 |
< |
public void testAcquireSharedNanos_InterruptedException() { |
1091 |
> |
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
1092 |
|
final BooleanLatch l = new BooleanLatch(); |
1093 |
|
Thread t = new Thread(new Runnable() { |
1094 |
|
public void run() { |
1096 |
|
threadAssertFalse(l.isSignalled()); |
1097 |
|
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1098 |
|
threadShouldThrow(); |
1099 |
< |
} catch(InterruptedException success){} |
1099 |
> |
} catch (InterruptedException success) {} |
1100 |
|
} |
1101 |
|
}); |
1102 |
|
t.start(); |
1103 |
< |
try { |
1104 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1105 |
< |
assertFalse(l.isSignalled()); |
1106 |
< |
t.interrupt(); |
1269 |
< |
t.join(); |
1270 |
< |
} catch (InterruptedException e){ |
1271 |
< |
unexpectedException(); |
1272 |
< |
} |
1103 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1104 |
> |
assertFalse(l.isSignalled()); |
1105 |
> |
t.interrupt(); |
1106 |
> |
t.join(); |
1107 |
|
} |
1108 |
|
|
1109 |
|
/** |
1110 |
|
* acquireSharedTimed times out if not released before timeout |
1111 |
|
*/ |
1112 |
< |
public void testAcquireSharedNanos_Timeout() { |
1112 |
> |
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1113 |
|
final BooleanLatch l = new BooleanLatch(); |
1114 |
|
Thread t = new Thread(new Runnable() { |
1115 |
|
public void run() { |
1116 |
|
try { |
1117 |
|
threadAssertFalse(l.isSignalled()); |
1118 |
|
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1119 |
< |
} catch(InterruptedException ie){ |
1119 |
> |
} catch (InterruptedException ie) { |
1120 |
|
threadUnexpectedException(); |
1121 |
|
} |
1122 |
|
} |
1123 |
|
}); |
1124 |
|
t.start(); |
1125 |
< |
try { |
1126 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1127 |
< |
assertFalse(l.isSignalled()); |
1294 |
< |
t.join(); |
1295 |
< |
} catch (InterruptedException e){ |
1296 |
< |
unexpectedException(); |
1297 |
< |
} |
1125 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1126 |
> |
assertFalse(l.isSignalled()); |
1127 |
> |
t.join(); |
1128 |
|
} |
1129 |
|
|
1130 |
|
|