68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* A runnable calling acquireInterruptibly |
71 |
> |
* A runnable calling acquireInterruptibly that does not expect to |
72 |
> |
* be interrupted. |
73 |
|
*/ |
74 |
< |
class InterruptibleSyncRunnable implements Runnable { |
74 |
> |
class InterruptibleSyncRunnable extends CheckedRunnable { |
75 |
|
final Mutex sync; |
76 |
|
InterruptibleSyncRunnable(Mutex l) { sync = l; } |
77 |
< |
public void run() { |
78 |
< |
try { |
78 |
< |
sync.acquireInterruptibly(1); |
79 |
< |
} catch (InterruptedException success) {} |
77 |
> |
public void realRun() throws InterruptedException { |
78 |
> |
sync.acquireInterruptibly(1); |
79 |
|
} |
80 |
|
} |
81 |
|
|
82 |
|
|
83 |
|
/** |
84 |
|
* A runnable calling acquireInterruptibly that expects to be |
85 |
< |
* interrupted |
85 |
> |
* interrupted. |
86 |
|
*/ |
87 |
< |
class InterruptedSyncRunnable implements Runnable { |
87 |
> |
class InterruptedSyncRunnable extends CheckedInterruptedRunnable { |
88 |
|
final Mutex sync; |
89 |
|
InterruptedSyncRunnable(Mutex l) { sync = l; } |
90 |
< |
public void run() { |
91 |
< |
try { |
93 |
< |
sync.acquireInterruptibly(1); |
94 |
< |
threadShouldThrow(); |
95 |
< |
} catch (InterruptedException success) {} |
90 |
> |
public void realRun() throws InterruptedException { |
91 |
> |
sync.acquireInterruptibly(1); |
92 |
|
} |
93 |
|
} |
94 |
|
|
124 |
|
/** |
125 |
|
* hasQueuedThreads reports whether there are waiting threads |
126 |
|
*/ |
127 |
< |
public void testhasQueuedThreads() { |
127 |
> |
public void testhasQueuedThreads() throws InterruptedException { |
128 |
|
final Mutex sync = new Mutex(); |
129 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
130 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
131 |
< |
try { |
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(); |
151 |
< |
t2.join(); |
152 |
< |
} catch (Exception e) { |
153 |
< |
unexpectedException(); |
154 |
< |
} |
131 |
> |
assertFalse(sync.hasQueuedThreads()); |
132 |
> |
sync.acquire(1); |
133 |
> |
t1.start(); |
134 |
> |
Thread.sleep(SHORT_DELAY_MS); |
135 |
> |
assertTrue(sync.hasQueuedThreads()); |
136 |
> |
t2.start(); |
137 |
> |
Thread.sleep(SHORT_DELAY_MS); |
138 |
> |
assertTrue(sync.hasQueuedThreads()); |
139 |
> |
t1.interrupt(); |
140 |
> |
Thread.sleep(SHORT_DELAY_MS); |
141 |
> |
assertTrue(sync.hasQueuedThreads()); |
142 |
> |
sync.release(1); |
143 |
> |
Thread.sleep(SHORT_DELAY_MS); |
144 |
> |
assertFalse(sync.hasQueuedThreads()); |
145 |
> |
t1.join(); |
146 |
> |
t2.join(); |
147 |
|
} |
148 |
|
|
149 |
|
/** |
154 |
|
try { |
155 |
|
sync.isQueued(null); |
156 |
|
shouldThrow(); |
157 |
< |
} catch (NullPointerException success) { |
166 |
< |
} |
157 |
> |
} catch (NullPointerException success) {} |
158 |
|
} |
159 |
|
|
160 |
|
/** |
161 |
|
* isQueued reports whether a thread is queued. |
162 |
|
*/ |
163 |
< |
public void testIsQueued() { |
163 |
> |
public void testIsQueued() throws InterruptedException { |
164 |
|
final Mutex sync = new Mutex(); |
165 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
166 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
167 |
< |
try { |
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(); |
197 |
< |
t2.join(); |
198 |
< |
} catch (Exception e) { |
199 |
< |
unexpectedException(); |
200 |
< |
} |
167 |
> |
assertFalse(sync.isQueued(t1)); |
168 |
> |
assertFalse(sync.isQueued(t2)); |
169 |
> |
sync.acquire(1); |
170 |
> |
t1.start(); |
171 |
> |
Thread.sleep(SHORT_DELAY_MS); |
172 |
> |
assertTrue(sync.isQueued(t1)); |
173 |
> |
t2.start(); |
174 |
> |
Thread.sleep(SHORT_DELAY_MS); |
175 |
> |
assertTrue(sync.isQueued(t1)); |
176 |
> |
assertTrue(sync.isQueued(t2)); |
177 |
> |
t1.interrupt(); |
178 |
> |
Thread.sleep(SHORT_DELAY_MS); |
179 |
> |
assertFalse(sync.isQueued(t1)); |
180 |
> |
assertTrue(sync.isQueued(t2)); |
181 |
> |
sync.release(1); |
182 |
> |
Thread.sleep(SHORT_DELAY_MS); |
183 |
> |
assertFalse(sync.isQueued(t1)); |
184 |
> |
Thread.sleep(SHORT_DELAY_MS); |
185 |
> |
assertFalse(sync.isQueued(t2)); |
186 |
> |
t1.join(); |
187 |
> |
t2.join(); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
192 |
|
*/ |
193 |
< |
public void testGetFirstQueuedThread() { |
193 |
> |
public void testGetFirstQueuedThread() throws InterruptedException { |
194 |
|
final Mutex sync = new Mutex(); |
195 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
196 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
197 |
< |
try { |
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(); |
227 |
< |
t2.join(); |
228 |
< |
} catch (Exception e) { |
229 |
< |
unexpectedException(); |
230 |
< |
} |
197 |
> |
assertNull(sync.getFirstQueuedThread()); |
198 |
> |
sync.acquire(1); |
199 |
> |
t1.start(); |
200 |
> |
Thread.sleep(SHORT_DELAY_MS); |
201 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
202 |
> |
t2.start(); |
203 |
> |
Thread.sleep(SHORT_DELAY_MS); |
204 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
205 |
> |
t1.interrupt(); |
206 |
> |
Thread.sleep(SHORT_DELAY_MS); |
207 |
> |
Thread.sleep(SHORT_DELAY_MS); |
208 |
> |
assertEquals(t2, sync.getFirstQueuedThread()); |
209 |
> |
sync.release(1); |
210 |
> |
Thread.sleep(SHORT_DELAY_MS); |
211 |
> |
assertNull(sync.getFirstQueuedThread()); |
212 |
> |
t1.join(); |
213 |
> |
t2.join(); |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
/** |
218 |
|
* hasContended reports false if no thread has ever blocked, else true |
219 |
|
*/ |
220 |
< |
public void testHasContended() { |
220 |
> |
public void testHasContended() throws InterruptedException { |
221 |
|
final Mutex sync = new Mutex(); |
222 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
223 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
224 |
< |
try { |
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(); |
257 |
< |
t2.join(); |
258 |
< |
} catch (Exception e) { |
259 |
< |
unexpectedException(); |
260 |
< |
} |
224 |
> |
assertFalse(sync.hasContended()); |
225 |
> |
sync.acquire(1); |
226 |
> |
t1.start(); |
227 |
> |
Thread.sleep(SHORT_DELAY_MS); |
228 |
> |
assertTrue(sync.hasContended()); |
229 |
> |
t2.start(); |
230 |
> |
Thread.sleep(SHORT_DELAY_MS); |
231 |
> |
assertTrue(sync.hasContended()); |
232 |
> |
t1.interrupt(); |
233 |
> |
Thread.sleep(SHORT_DELAY_MS); |
234 |
> |
assertTrue(sync.hasContended()); |
235 |
> |
sync.release(1); |
236 |
> |
Thread.sleep(SHORT_DELAY_MS); |
237 |
> |
assertTrue(sync.hasContended()); |
238 |
> |
t1.join(); |
239 |
> |
t2.join(); |
240 |
|
} |
241 |
|
|
242 |
|
/** |
243 |
|
* getQueuedThreads includes waiting threads |
244 |
|
*/ |
245 |
< |
public void testGetQueuedThreads() { |
245 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
246 |
|
final Mutex sync = new Mutex(); |
247 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
248 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
249 |
< |
try { |
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(); |
289 |
< |
t2.join(); |
290 |
< |
} catch (Exception e) { |
291 |
< |
unexpectedException(); |
292 |
< |
} |
249 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
250 |
> |
sync.acquire(1); |
251 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
252 |
> |
t1.start(); |
253 |
> |
Thread.sleep(SHORT_DELAY_MS); |
254 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
255 |
> |
t2.start(); |
256 |
> |
Thread.sleep(SHORT_DELAY_MS); |
257 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
258 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
259 |
> |
t1.interrupt(); |
260 |
> |
Thread.sleep(SHORT_DELAY_MS); |
261 |
> |
assertFalse(sync.getQueuedThreads().contains(t1)); |
262 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
263 |
> |
sync.release(1); |
264 |
> |
Thread.sleep(SHORT_DELAY_MS); |
265 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
266 |
> |
t1.join(); |
267 |
> |
t2.join(); |
268 |
|
} |
269 |
|
|
270 |
|
/** |
271 |
|
* getExclusiveQueuedThreads includes waiting threads |
272 |
|
*/ |
273 |
< |
public void testGetExclusiveQueuedThreads() { |
273 |
> |
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
274 |
|
final Mutex sync = new Mutex(); |
275 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
276 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
277 |
< |
try { |
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(); |
321 |
< |
t2.join(); |
322 |
< |
} catch (Exception e) { |
323 |
< |
unexpectedException(); |
324 |
< |
} |
277 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
278 |
> |
sync.acquire(1); |
279 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
280 |
> |
t1.start(); |
281 |
> |
Thread.sleep(SHORT_DELAY_MS); |
282 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
283 |
> |
t2.start(); |
284 |
> |
Thread.sleep(SHORT_DELAY_MS); |
285 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
286 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
287 |
> |
t1.interrupt(); |
288 |
> |
Thread.sleep(SHORT_DELAY_MS); |
289 |
> |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
290 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
291 |
> |
sync.release(1); |
292 |
> |
Thread.sleep(SHORT_DELAY_MS); |
293 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
294 |
> |
t1.join(); |
295 |
> |
t2.join(); |
296 |
|
} |
297 |
|
|
298 |
|
/** |
299 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
300 |
|
*/ |
301 |
< |
public void testGetSharedQueuedThreads() { |
301 |
> |
public void testGetSharedQueuedThreads() throws InterruptedException { |
302 |
|
final Mutex sync = new Mutex(); |
303 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
304 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
305 |
< |
try { |
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(); |
351 |
< |
t2.join(); |
352 |
< |
} catch (Exception e) { |
353 |
< |
unexpectedException(); |
354 |
< |
} |
305 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
306 |
> |
sync.acquire(1); |
307 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
308 |
> |
t1.start(); |
309 |
> |
Thread.sleep(SHORT_DELAY_MS); |
310 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
311 |
> |
t2.start(); |
312 |
> |
Thread.sleep(SHORT_DELAY_MS); |
313 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
314 |
> |
t1.interrupt(); |
315 |
> |
Thread.sleep(SHORT_DELAY_MS); |
316 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
317 |
> |
sync.release(1); |
318 |
> |
Thread.sleep(SHORT_DELAY_MS); |
319 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
320 |
> |
t1.join(); |
321 |
> |
t2.join(); |
322 |
|
} |
323 |
|
|
324 |
|
/** |
325 |
|
* tryAcquireNanos is interruptible. |
326 |
|
*/ |
327 |
< |
public void testInterruptedException2() { |
327 |
> |
public void testInterruptedException2() throws InterruptedException { |
328 |
|
final Mutex sync = new Mutex(); |
329 |
|
sync.acquire(1); |
330 |
< |
Thread t = new Thread(new Runnable() { |
331 |
< |
public void run() { |
332 |
< |
try { |
333 |
< |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
334 |
< |
threadShouldThrow(); |
335 |
< |
} catch (InterruptedException success) {} |
336 |
< |
} |
337 |
< |
}); |
371 |
< |
try { |
372 |
< |
t.start(); |
373 |
< |
t.interrupt(); |
374 |
< |
} catch (Exception e) { |
375 |
< |
unexpectedException(); |
376 |
< |
} |
330 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
331 |
> |
public void realRun() throws InterruptedException { |
332 |
> |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
333 |
> |
}}); |
334 |
> |
|
335 |
> |
t.start(); |
336 |
> |
t.interrupt(); |
337 |
> |
t.join(); |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
|
/** |
342 |
|
* TryAcquire on exclusively held sync fails |
343 |
|
*/ |
344 |
< |
public void testTryAcquireWhenSynced() { |
344 |
> |
public void testTryAcquireWhenSynced() throws InterruptedException { |
345 |
|
final Mutex sync = new Mutex(); |
346 |
|
sync.acquire(1); |
347 |
< |
Thread t = new Thread(new Runnable() { |
348 |
< |
public void run() { |
349 |
< |
threadAssertFalse(sync.tryAcquire(1)); |
350 |
< |
} |
351 |
< |
}); |
352 |
< |
try { |
353 |
< |
t.start(); |
354 |
< |
t.join(); |
394 |
< |
sync.release(1); |
395 |
< |
} catch (Exception e) { |
396 |
< |
unexpectedException(); |
397 |
< |
} |
347 |
> |
Thread t = new Thread(new CheckedRunnable() { |
348 |
> |
public void realRun() { |
349 |
> |
threadAssertFalse(sync.tryAcquire(1)); |
350 |
> |
}}); |
351 |
> |
|
352 |
> |
t.start(); |
353 |
> |
t.join(); |
354 |
> |
sync.release(1); |
355 |
|
} |
356 |
|
|
357 |
|
/** |
358 |
|
* tryAcquireNanos on an exclusively held sync times out |
359 |
|
*/ |
360 |
< |
public void testAcquireNanos_Timeout() { |
360 |
> |
public void testAcquireNanos_Timeout() throws InterruptedException { |
361 |
|
final Mutex sync = new Mutex(); |
362 |
|
sync.acquire(1); |
363 |
< |
Thread t = new Thread(new Runnable() { |
364 |
< |
public void run() { |
365 |
< |
try { |
366 |
< |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
367 |
< |
} catch (Exception ex) { |
368 |
< |
threadUnexpectedException(); |
369 |
< |
} |
370 |
< |
} |
414 |
< |
}); |
415 |
< |
try { |
416 |
< |
t.start(); |
417 |
< |
t.join(); |
418 |
< |
sync.release(1); |
419 |
< |
} catch (Exception e) { |
420 |
< |
unexpectedException(); |
421 |
< |
} |
363 |
> |
Thread t = new Thread(new CheckedRunnable() { |
364 |
> |
public void realRun() throws InterruptedException { |
365 |
> |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
366 |
> |
}}); |
367 |
> |
|
368 |
> |
t.start(); |
369 |
> |
t.join(); |
370 |
> |
sync.release(1); |
371 |
|
} |
372 |
|
|
373 |
|
|
374 |
|
/** |
375 |
|
* getState is true when acquired and false when not |
376 |
|
*/ |
377 |
< |
public void testGetState() { |
377 |
> |
public void testGetState() throws InterruptedException { |
378 |
|
final Mutex sync = new Mutex(); |
379 |
|
sync.acquire(1); |
380 |
|
assertTrue(sync.isHeldExclusively()); |
381 |
|
sync.release(1); |
382 |
|
assertFalse(sync.isHeldExclusively()); |
383 |
< |
Thread t = new Thread(new Runnable() { |
384 |
< |
public void run() { |
385 |
< |
sync.acquire(1); |
386 |
< |
try { |
387 |
< |
Thread.sleep(SMALL_DELAY_MS); |
388 |
< |
} |
389 |
< |
catch (Exception e) { |
390 |
< |
threadUnexpectedException(); |
391 |
< |
} |
392 |
< |
sync.release(1); |
393 |
< |
} |
394 |
< |
}); |
446 |
< |
try { |
447 |
< |
t.start(); |
448 |
< |
Thread.sleep(SHORT_DELAY_MS); |
449 |
< |
assertTrue(sync.isHeldExclusively()); |
450 |
< |
t.join(); |
451 |
< |
assertFalse(sync.isHeldExclusively()); |
452 |
< |
} catch (Exception e) { |
453 |
< |
unexpectedException(); |
454 |
< |
} |
383 |
> |
Thread t = new Thread(new CheckedRunnable() { |
384 |
> |
public void realRun() throws InterruptedException { |
385 |
> |
sync.acquire(1); |
386 |
> |
Thread.sleep(SMALL_DELAY_MS); |
387 |
> |
sync.release(1); |
388 |
> |
}}); |
389 |
> |
|
390 |
> |
t.start(); |
391 |
> |
Thread.sleep(SHORT_DELAY_MS); |
392 |
> |
assertTrue(sync.isHeldExclusively()); |
393 |
> |
t.join(); |
394 |
> |
assertFalse(sync.isHeldExclusively()); |
395 |
|
} |
396 |
|
|
397 |
|
|
398 |
|
/** |
399 |
|
* acquireInterruptibly is interruptible. |
400 |
|
*/ |
401 |
< |
public void testAcquireInterruptibly1() { |
401 |
> |
public void testAcquireInterruptibly1() throws InterruptedException { |
402 |
|
final Mutex sync = new Mutex(); |
403 |
|
sync.acquire(1); |
404 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
405 |
< |
try { |
406 |
< |
t.start(); |
407 |
< |
Thread.sleep(SHORT_DELAY_MS); |
408 |
< |
t.interrupt(); |
409 |
< |
Thread.sleep(SHORT_DELAY_MS); |
410 |
< |
sync.release(1); |
471 |
< |
t.join(); |
472 |
< |
} catch (Exception e) { |
473 |
< |
unexpectedException(); |
474 |
< |
} |
405 |
> |
t.start(); |
406 |
> |
Thread.sleep(SHORT_DELAY_MS); |
407 |
> |
t.interrupt(); |
408 |
> |
Thread.sleep(SHORT_DELAY_MS); |
409 |
> |
sync.release(1); |
410 |
> |
t.join(); |
411 |
|
} |
412 |
|
|
413 |
|
/** |
414 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
415 |
|
*/ |
416 |
< |
public void testAcquireInterruptibly2() { |
416 |
> |
public void testAcquireInterruptibly2() throws InterruptedException { |
417 |
|
final Mutex sync = new Mutex(); |
418 |
< |
try { |
483 |
< |
sync.acquireInterruptibly(1); |
484 |
< |
} catch (Exception e) { |
485 |
< |
unexpectedException(); |
486 |
< |
} |
418 |
> |
sync.acquireInterruptibly(1); |
419 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
420 |
< |
try { |
421 |
< |
t.start(); |
422 |
< |
t.interrupt(); |
423 |
< |
assertTrue(sync.isHeldExclusively()); |
492 |
< |
t.join(); |
493 |
< |
} catch (Exception e) { |
494 |
< |
unexpectedException(); |
495 |
< |
} |
420 |
> |
t.start(); |
421 |
> |
t.interrupt(); |
422 |
> |
assertTrue(sync.isHeldExclusively()); |
423 |
> |
t.join(); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
437 |
|
/** |
438 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
439 |
|
*/ |
440 |
< |
public void testAwait_IllegalMonitor() { |
440 |
> |
public void testAwait_IllegalMonitor() throws InterruptedException { |
441 |
|
final Mutex sync = new Mutex(); |
442 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
443 |
|
try { |
444 |
|
c.await(); |
445 |
|
shouldThrow(); |
446 |
< |
} |
519 |
< |
catch (IllegalMonitorStateException success) { |
520 |
< |
} |
521 |
< |
catch (Exception ex) { |
522 |
< |
unexpectedException(); |
523 |
< |
} |
446 |
> |
} catch (IllegalMonitorStateException success) {} |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
451 |
|
*/ |
452 |
< |
public void testSignal_IllegalMonitor() { |
452 |
> |
public void testSignal_IllegalMonitor() throws InterruptedException { |
453 |
|
final Mutex sync = new Mutex(); |
454 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
455 |
|
try { |
456 |
|
c.signal(); |
457 |
|
shouldThrow(); |
458 |
< |
} |
536 |
< |
catch (IllegalMonitorStateException success) { |
537 |
< |
} |
538 |
< |
catch (Exception ex) { |
539 |
< |
unexpectedException(); |
540 |
< |
} |
458 |
> |
} catch (IllegalMonitorStateException success) {} |
459 |
|
} |
460 |
|
|
461 |
|
/** |
462 |
|
* awaitNanos without a signal times out |
463 |
|
*/ |
464 |
< |
public void testAwaitNanos_Timeout() { |
464 |
> |
public void testAwaitNanos_Timeout() throws InterruptedException { |
465 |
|
final Mutex sync = new Mutex(); |
466 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
467 |
< |
try { |
468 |
< |
sync.acquire(1); |
469 |
< |
long t = c.awaitNanos(100); |
470 |
< |
assertTrue(t <= 0); |
553 |
< |
sync.release(1); |
554 |
< |
} |
555 |
< |
catch (Exception ex) { |
556 |
< |
unexpectedException(); |
557 |
< |
} |
467 |
> |
sync.acquire(1); |
468 |
> |
long t = c.awaitNanos(100); |
469 |
> |
assertTrue(t <= 0); |
470 |
> |
sync.release(1); |
471 |
|
} |
472 |
|
|
473 |
|
/** |
474 |
|
* Timed await without a signal times out |
475 |
|
*/ |
476 |
< |
public void testAwait_Timeout() { |
476 |
> |
public void testAwait_Timeout() throws InterruptedException { |
477 |
|
final Mutex sync = new Mutex(); |
478 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
479 |
< |
try { |
480 |
< |
sync.acquire(1); |
481 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
569 |
< |
sync.release(1); |
570 |
< |
} |
571 |
< |
catch (Exception ex) { |
572 |
< |
unexpectedException(); |
573 |
< |
} |
479 |
> |
sync.acquire(1); |
480 |
> |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
481 |
> |
sync.release(1); |
482 |
|
} |
483 |
|
|
484 |
|
/** |
485 |
|
* awaitUntil without a signal times out |
486 |
|
*/ |
487 |
< |
public void testAwaitUntil_Timeout() { |
487 |
> |
public void testAwaitUntil_Timeout() throws InterruptedException { |
488 |
|
final Mutex sync = new Mutex(); |
489 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
490 |
< |
try { |
491 |
< |
sync.acquire(1); |
492 |
< |
java.util.Date d = new java.util.Date(); |
493 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
586 |
< |
sync.release(1); |
587 |
< |
} |
588 |
< |
catch (Exception ex) { |
589 |
< |
unexpectedException(); |
590 |
< |
} |
490 |
> |
sync.acquire(1); |
491 |
> |
java.util.Date d = new java.util.Date(); |
492 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
493 |
> |
sync.release(1); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
497 |
|
* await returns when signalled |
498 |
|
*/ |
499 |
< |
public void testAwait() { |
499 |
> |
public void testAwait() throws InterruptedException { |
500 |
|
final Mutex sync = new Mutex(); |
501 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
502 |
< |
Thread t = new Thread(new Runnable() { |
503 |
< |
public void run() { |
504 |
< |
try { |
505 |
< |
sync.acquire(1); |
506 |
< |
c.await(); |
507 |
< |
sync.release(1); |
605 |
< |
} |
606 |
< |
catch (InterruptedException e) { |
607 |
< |
threadUnexpectedException(); |
608 |
< |
} |
609 |
< |
} |
610 |
< |
}); |
502 |
> |
Thread t = new Thread(new CheckedRunnable() { |
503 |
> |
public void realRun() throws InterruptedException { |
504 |
> |
sync.acquire(1); |
505 |
> |
c.await(); |
506 |
> |
sync.release(1); |
507 |
> |
}}); |
508 |
|
|
509 |
< |
try { |
510 |
< |
t.start(); |
511 |
< |
Thread.sleep(SHORT_DELAY_MS); |
512 |
< |
sync.acquire(1); |
513 |
< |
c.signal(); |
514 |
< |
sync.release(1); |
515 |
< |
t.join(SHORT_DELAY_MS); |
619 |
< |
assertFalse(t.isAlive()); |
620 |
< |
} |
621 |
< |
catch (Exception ex) { |
622 |
< |
unexpectedException(); |
623 |
< |
} |
509 |
> |
t.start(); |
510 |
> |
Thread.sleep(SHORT_DELAY_MS); |
511 |
> |
sync.acquire(1); |
512 |
> |
c.signal(); |
513 |
> |
sync.release(1); |
514 |
> |
t.join(SHORT_DELAY_MS); |
515 |
> |
assertFalse(t.isAlive()); |
516 |
|
} |
517 |
|
|
518 |
|
|
525 |
|
try { |
526 |
|
sync.hasWaiters(null); |
527 |
|
shouldThrow(); |
528 |
< |
} catch (NullPointerException success) { |
637 |
< |
} catch (Exception ex) { |
638 |
< |
unexpectedException(); |
639 |
< |
} |
528 |
> |
} catch (NullPointerException success) {} |
529 |
|
} |
530 |
|
|
531 |
|
/** |
536 |
|
try { |
537 |
|
sync.getWaitQueueLength(null); |
538 |
|
shouldThrow(); |
539 |
< |
} catch (NullPointerException success) { |
651 |
< |
} catch (Exception ex) { |
652 |
< |
unexpectedException(); |
653 |
< |
} |
539 |
> |
} catch (NullPointerException success) {} |
540 |
|
} |
541 |
|
|
542 |
|
|
548 |
|
try { |
549 |
|
sync.getWaitingThreads(null); |
550 |
|
shouldThrow(); |
551 |
< |
} catch (NullPointerException success) { |
666 |
< |
} catch (Exception ex) { |
667 |
< |
unexpectedException(); |
668 |
< |
} |
551 |
> |
} catch (NullPointerException success) {} |
552 |
|
} |
553 |
|
|
554 |
|
|
557 |
|
*/ |
558 |
|
public void testHasWaitersIAE() { |
559 |
|
final Mutex sync = new Mutex(); |
560 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
560 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
561 |
|
final Mutex sync2 = new Mutex(); |
562 |
|
try { |
563 |
|
sync2.hasWaiters(c); |
564 |
|
shouldThrow(); |
565 |
< |
} catch (IllegalArgumentException success) { |
683 |
< |
} catch (Exception ex) { |
684 |
< |
unexpectedException(); |
685 |
< |
} |
565 |
> |
} catch (IllegalArgumentException success) {} |
566 |
|
} |
567 |
|
|
568 |
|
/** |
570 |
|
*/ |
571 |
|
public void testHasWaitersIMSE() { |
572 |
|
final Mutex sync = new Mutex(); |
573 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
573 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
574 |
|
try { |
575 |
|
sync.hasWaiters(c); |
576 |
|
shouldThrow(); |
577 |
< |
} catch (IllegalMonitorStateException success) { |
698 |
< |
} catch (Exception ex) { |
699 |
< |
unexpectedException(); |
700 |
< |
} |
577 |
> |
} catch (IllegalMonitorStateException success) {} |
578 |
|
} |
579 |
|
|
580 |
|
|
583 |
|
*/ |
584 |
|
public void testGetWaitQueueLengthIAE() { |
585 |
|
final Mutex sync = new Mutex(); |
586 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
586 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
587 |
|
final Mutex sync2 = new Mutex(); |
588 |
|
try { |
589 |
|
sync2.getWaitQueueLength(c); |
590 |
|
shouldThrow(); |
591 |
< |
} catch (IllegalArgumentException success) { |
715 |
< |
} catch (Exception ex) { |
716 |
< |
unexpectedException(); |
717 |
< |
} |
591 |
> |
} catch (IllegalArgumentException success) {} |
592 |
|
} |
593 |
|
|
594 |
|
/** |
596 |
|
*/ |
597 |
|
public void testGetWaitQueueLengthIMSE() { |
598 |
|
final Mutex sync = new Mutex(); |
599 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
599 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
600 |
|
try { |
601 |
|
sync.getWaitQueueLength(c); |
602 |
|
shouldThrow(); |
603 |
< |
} catch (IllegalMonitorStateException success) { |
730 |
< |
} catch (Exception ex) { |
731 |
< |
unexpectedException(); |
732 |
< |
} |
603 |
> |
} catch (IllegalMonitorStateException success) {} |
604 |
|
} |
605 |
|
|
606 |
|
|
609 |
|
*/ |
610 |
|
public void testGetWaitingThreadsIAE() { |
611 |
|
final Mutex sync = new Mutex(); |
612 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
612 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
613 |
|
final Mutex sync2 = new Mutex(); |
614 |
|
try { |
615 |
|
sync2.getWaitingThreads(c); |
616 |
|
shouldThrow(); |
617 |
< |
} catch (IllegalArgumentException success) { |
747 |
< |
} catch (Exception ex) { |
748 |
< |
unexpectedException(); |
749 |
< |
} |
617 |
> |
} catch (IllegalArgumentException success) {} |
618 |
|
} |
619 |
|
|
620 |
|
/** |
622 |
|
*/ |
623 |
|
public void testGetWaitingThreadsIMSE() { |
624 |
|
final Mutex sync = new Mutex(); |
625 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
625 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
626 |
|
try { |
627 |
|
sync.getWaitingThreads(c); |
628 |
|
shouldThrow(); |
629 |
< |
} catch (IllegalMonitorStateException success) { |
762 |
< |
} catch (Exception ex) { |
763 |
< |
unexpectedException(); |
764 |
< |
} |
629 |
> |
} catch (IllegalMonitorStateException success) {} |
630 |
|
} |
631 |
|
|
632 |
|
|
634 |
|
/** |
635 |
|
* hasWaiters returns true when a thread is waiting, else false |
636 |
|
*/ |
637 |
< |
public void testHasWaiters() { |
637 |
> |
public void testHasWaiters() throws InterruptedException { |
638 |
|
final Mutex sync = new Mutex(); |
639 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
640 |
< |
Thread t = new Thread(new Runnable() { |
641 |
< |
public void run() { |
642 |
< |
try { |
643 |
< |
sync.acquire(1); |
644 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
645 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
646 |
< |
c.await(); |
647 |
< |
sync.release(1); |
783 |
< |
} |
784 |
< |
catch (InterruptedException e) { |
785 |
< |
threadUnexpectedException(); |
786 |
< |
} |
787 |
< |
} |
788 |
< |
}); |
640 |
> |
Thread t = new Thread(new CheckedRunnable() { |
641 |
> |
public void realRun() throws InterruptedException { |
642 |
> |
sync.acquire(1); |
643 |
> |
threadAssertFalse(sync.hasWaiters(c)); |
644 |
> |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
645 |
> |
c.await(); |
646 |
> |
sync.release(1); |
647 |
> |
}}); |
648 |
|
|
649 |
< |
try { |
650 |
< |
t.start(); |
651 |
< |
Thread.sleep(SHORT_DELAY_MS); |
652 |
< |
sync.acquire(1); |
653 |
< |
assertTrue(sync.hasWaiters(c)); |
654 |
< |
assertEquals(1, sync.getWaitQueueLength(c)); |
655 |
< |
c.signal(); |
656 |
< |
sync.release(1); |
657 |
< |
Thread.sleep(SHORT_DELAY_MS); |
658 |
< |
sync.acquire(1); |
659 |
< |
assertFalse(sync.hasWaiters(c)); |
660 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
661 |
< |
sync.release(1); |
662 |
< |
t.join(SHORT_DELAY_MS); |
804 |
< |
assertFalse(t.isAlive()); |
805 |
< |
} |
806 |
< |
catch (Exception ex) { |
807 |
< |
unexpectedException(); |
808 |
< |
} |
649 |
> |
t.start(); |
650 |
> |
Thread.sleep(SHORT_DELAY_MS); |
651 |
> |
sync.acquire(1); |
652 |
> |
assertTrue(sync.hasWaiters(c)); |
653 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
654 |
> |
c.signal(); |
655 |
> |
sync.release(1); |
656 |
> |
Thread.sleep(SHORT_DELAY_MS); |
657 |
> |
sync.acquire(1); |
658 |
> |
assertFalse(sync.hasWaiters(c)); |
659 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
660 |
> |
sync.release(1); |
661 |
> |
t.join(SHORT_DELAY_MS); |
662 |
> |
assertFalse(t.isAlive()); |
663 |
|
} |
664 |
|
|
665 |
|
/** |
666 |
|
* getWaitQueueLength returns number of waiting threads |
667 |
|
*/ |
668 |
< |
public void testGetWaitQueueLength() { |
668 |
> |
public void testGetWaitQueueLength() throws InterruptedException { |
669 |
|
final Mutex sync = new Mutex(); |
670 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
671 |
< |
Thread t1 = new Thread(new Runnable() { |
672 |
< |
public void run() { |
673 |
< |
try { |
674 |
< |
sync.acquire(1); |
675 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
676 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
677 |
< |
c.await(); |
678 |
< |
sync.release(1); |
679 |
< |
} |
680 |
< |
catch (InterruptedException e) { |
681 |
< |
threadUnexpectedException(); |
682 |
< |
} |
683 |
< |
} |
684 |
< |
}); |
685 |
< |
|
686 |
< |
Thread t2 = new Thread(new Runnable() { |
687 |
< |
public void run() { |
688 |
< |
try { |
689 |
< |
sync.acquire(1); |
690 |
< |
threadAssertTrue(sync.hasWaiters(c)); |
691 |
< |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
692 |
< |
c.await(); |
693 |
< |
sync.release(1); |
694 |
< |
} |
695 |
< |
catch (InterruptedException e) { |
696 |
< |
threadUnexpectedException(); |
697 |
< |
} |
698 |
< |
} |
699 |
< |
}); |
700 |
< |
|
701 |
< |
try { |
702 |
< |
t1.start(); |
703 |
< |
Thread.sleep(SHORT_DELAY_MS); |
704 |
< |
t2.start(); |
705 |
< |
Thread.sleep(SHORT_DELAY_MS); |
706 |
< |
sync.acquire(1); |
853 |
< |
assertTrue(sync.hasWaiters(c)); |
854 |
< |
assertEquals(2, sync.getWaitQueueLength(c)); |
855 |
< |
c.signalAll(); |
856 |
< |
sync.release(1); |
857 |
< |
Thread.sleep(SHORT_DELAY_MS); |
858 |
< |
sync.acquire(1); |
859 |
< |
assertFalse(sync.hasWaiters(c)); |
860 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
861 |
< |
sync.release(1); |
862 |
< |
t1.join(SHORT_DELAY_MS); |
863 |
< |
t2.join(SHORT_DELAY_MS); |
864 |
< |
assertFalse(t1.isAlive()); |
865 |
< |
assertFalse(t2.isAlive()); |
866 |
< |
} |
867 |
< |
catch (Exception ex) { |
868 |
< |
unexpectedException(); |
869 |
< |
} |
671 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
672 |
> |
public void realRun() throws InterruptedException { |
673 |
> |
sync.acquire(1); |
674 |
> |
threadAssertFalse(sync.hasWaiters(c)); |
675 |
> |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
676 |
> |
c.await(); |
677 |
> |
sync.release(1); |
678 |
> |
}}); |
679 |
> |
|
680 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
681 |
> |
public void realRun() throws InterruptedException { |
682 |
> |
sync.acquire(1); |
683 |
> |
threadAssertTrue(sync.hasWaiters(c)); |
684 |
> |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
685 |
> |
c.await(); |
686 |
> |
sync.release(1); |
687 |
> |
}}); |
688 |
> |
|
689 |
> |
t1.start(); |
690 |
> |
Thread.sleep(SHORT_DELAY_MS); |
691 |
> |
t2.start(); |
692 |
> |
Thread.sleep(SHORT_DELAY_MS); |
693 |
> |
sync.acquire(1); |
694 |
> |
assertTrue(sync.hasWaiters(c)); |
695 |
> |
assertEquals(2, sync.getWaitQueueLength(c)); |
696 |
> |
c.signalAll(); |
697 |
> |
sync.release(1); |
698 |
> |
Thread.sleep(SHORT_DELAY_MS); |
699 |
> |
sync.acquire(1); |
700 |
> |
assertFalse(sync.hasWaiters(c)); |
701 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
702 |
> |
sync.release(1); |
703 |
> |
t1.join(SHORT_DELAY_MS); |
704 |
> |
t2.join(SHORT_DELAY_MS); |
705 |
> |
assertFalse(t1.isAlive()); |
706 |
> |
assertFalse(t2.isAlive()); |
707 |
|
} |
708 |
|
|
709 |
|
/** |
710 |
|
* getWaitingThreads returns only and all waiting threads |
711 |
|
*/ |
712 |
< |
public void testGetWaitingThreads() { |
712 |
> |
public void testGetWaitingThreads() throws InterruptedException { |
713 |
|
final Mutex sync = new Mutex(); |
714 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
715 |
< |
Thread t1 = new Thread(new Runnable() { |
716 |
< |
public void run() { |
717 |
< |
try { |
718 |
< |
sync.acquire(1); |
719 |
< |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
720 |
< |
c.await(); |
721 |
< |
sync.release(1); |
722 |
< |
} |
723 |
< |
catch (InterruptedException e) { |
724 |
< |
threadUnexpectedException(); |
725 |
< |
} |
726 |
< |
} |
727 |
< |
}); |
728 |
< |
|
729 |
< |
Thread t2 = new Thread(new Runnable() { |
893 |
< |
public void run() { |
894 |
< |
try { |
895 |
< |
sync.acquire(1); |
896 |
< |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
897 |
< |
c.await(); |
898 |
< |
sync.release(1); |
899 |
< |
} |
900 |
< |
catch (InterruptedException e) { |
901 |
< |
threadUnexpectedException(); |
902 |
< |
} |
903 |
< |
} |
904 |
< |
}); |
715 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
716 |
> |
public void realRun() throws InterruptedException { |
717 |
> |
sync.acquire(1); |
718 |
> |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
719 |
> |
c.await(); |
720 |
> |
sync.release(1); |
721 |
> |
}}); |
722 |
> |
|
723 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
724 |
> |
public void realRun() throws InterruptedException { |
725 |
> |
sync.acquire(1); |
726 |
> |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
727 |
> |
c.await(); |
728 |
> |
sync.release(1); |
729 |
> |
}}); |
730 |
|
|
906 |
– |
try { |
731 |
|
sync.acquire(1); |
732 |
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
733 |
|
sync.release(1); |
750 |
|
t2.join(SHORT_DELAY_MS); |
751 |
|
assertFalse(t1.isAlive()); |
752 |
|
assertFalse(t2.isAlive()); |
929 |
– |
} |
930 |
– |
catch (Exception ex) { |
931 |
– |
unexpectedException(); |
932 |
– |
} |
753 |
|
} |
754 |
|
|
755 |
|
|
757 |
|
/** |
758 |
|
* awaitUninterruptibly doesn't abort on interrupt |
759 |
|
*/ |
760 |
< |
public void testAwaitUninterruptibly() { |
760 |
> |
public void testAwaitUninterruptibly() throws InterruptedException { |
761 |
|
final Mutex sync = new Mutex(); |
762 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
763 |
< |
Thread t = new Thread(new Runnable() { |
764 |
< |
public void run() { |
765 |
< |
sync.acquire(1); |
766 |
< |
c.awaitUninterruptibly(); |
767 |
< |
sync.release(1); |
768 |
< |
} |
949 |
< |
}); |
763 |
> |
Thread t = new Thread(new CheckedRunnable() { |
764 |
> |
public void realRun() { |
765 |
> |
sync.acquire(1); |
766 |
> |
c.awaitUninterruptibly(); |
767 |
> |
sync.release(1); |
768 |
> |
}}); |
769 |
|
|
770 |
< |
try { |
771 |
< |
t.start(); |
772 |
< |
Thread.sleep(SHORT_DELAY_MS); |
773 |
< |
t.interrupt(); |
774 |
< |
sync.acquire(1); |
775 |
< |
c.signal(); |
776 |
< |
sync.release(1); |
777 |
< |
t.join(SHORT_DELAY_MS); |
959 |
< |
assertFalse(t.isAlive()); |
960 |
< |
} |
961 |
< |
catch (Exception ex) { |
962 |
< |
unexpectedException(); |
963 |
< |
} |
770 |
> |
t.start(); |
771 |
> |
Thread.sleep(SHORT_DELAY_MS); |
772 |
> |
t.interrupt(); |
773 |
> |
sync.acquire(1); |
774 |
> |
c.signal(); |
775 |
> |
sync.release(1); |
776 |
> |
t.join(SHORT_DELAY_MS); |
777 |
> |
assertFalse(t.isAlive()); |
778 |
|
} |
779 |
|
|
780 |
|
/** |
781 |
|
* await is interruptible |
782 |
|
*/ |
783 |
< |
public void testAwait_Interrupt() { |
783 |
> |
public void testAwait_Interrupt() throws InterruptedException { |
784 |
|
final Mutex sync = new Mutex(); |
785 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
786 |
< |
Thread t = new Thread(new Runnable() { |
787 |
< |
public void run() { |
788 |
< |
try { |
789 |
< |
sync.acquire(1); |
790 |
< |
c.await(); |
791 |
< |
sync.release(1); |
978 |
< |
threadShouldThrow(); |
979 |
< |
} |
980 |
< |
catch (InterruptedException success) { |
981 |
< |
} |
982 |
< |
} |
983 |
< |
}); |
786 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
787 |
> |
public void realRun() throws InterruptedException { |
788 |
> |
sync.acquire(1); |
789 |
> |
c.await(); |
790 |
> |
sync.release(1); |
791 |
> |
}}); |
792 |
|
|
793 |
< |
try { |
794 |
< |
t.start(); |
795 |
< |
Thread.sleep(SHORT_DELAY_MS); |
796 |
< |
t.interrupt(); |
797 |
< |
t.join(SHORT_DELAY_MS); |
990 |
< |
assertFalse(t.isAlive()); |
991 |
< |
} |
992 |
< |
catch (Exception ex) { |
993 |
< |
unexpectedException(); |
994 |
< |
} |
793 |
> |
t.start(); |
794 |
> |
Thread.sleep(SHORT_DELAY_MS); |
795 |
> |
t.interrupt(); |
796 |
> |
t.join(SHORT_DELAY_MS); |
797 |
> |
assertFalse(t.isAlive()); |
798 |
|
} |
799 |
|
|
800 |
|
/** |
801 |
|
* awaitNanos is interruptible |
802 |
|
*/ |
803 |
< |
public void testAwaitNanos_Interrupt() { |
803 |
> |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
804 |
|
final Mutex sync = new Mutex(); |
805 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
806 |
< |
Thread t = new Thread(new Runnable() { |
807 |
< |
public void run() { |
808 |
< |
try { |
809 |
< |
sync.acquire(1); |
810 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
811 |
< |
sync.release(1); |
1009 |
< |
threadShouldThrow(); |
1010 |
< |
} |
1011 |
< |
catch (InterruptedException success) { |
1012 |
< |
} |
1013 |
< |
} |
1014 |
< |
}); |
806 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
807 |
> |
public void realRun() throws InterruptedException { |
808 |
> |
sync.acquire(1); |
809 |
> |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
810 |
> |
sync.release(1); |
811 |
> |
}}); |
812 |
|
|
813 |
< |
try { |
814 |
< |
t.start(); |
815 |
< |
Thread.sleep(SHORT_DELAY_MS); |
816 |
< |
t.interrupt(); |
817 |
< |
t.join(SHORT_DELAY_MS); |
1021 |
< |
assertFalse(t.isAlive()); |
1022 |
< |
} |
1023 |
< |
catch (Exception ex) { |
1024 |
< |
unexpectedException(); |
1025 |
< |
} |
813 |
> |
t.start(); |
814 |
> |
Thread.sleep(SHORT_DELAY_MS); |
815 |
> |
t.interrupt(); |
816 |
> |
t.join(SHORT_DELAY_MS); |
817 |
> |
assertFalse(t.isAlive()); |
818 |
|
} |
819 |
|
|
820 |
|
/** |
821 |
|
* awaitUntil is interruptible |
822 |
|
*/ |
823 |
< |
public void testAwaitUntil_Interrupt() { |
823 |
> |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
824 |
|
final Mutex sync = new Mutex(); |
825 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
826 |
< |
Thread t = new Thread(new Runnable() { |
827 |
< |
public void run() { |
828 |
< |
try { |
829 |
< |
sync.acquire(1); |
830 |
< |
java.util.Date d = new java.util.Date(); |
831 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
832 |
< |
sync.release(1); |
1041 |
< |
threadShouldThrow(); |
1042 |
< |
} |
1043 |
< |
catch (InterruptedException success) { |
1044 |
< |
} |
1045 |
< |
} |
1046 |
< |
}); |
826 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
827 |
> |
public void realRun() throws InterruptedException { |
828 |
> |
sync.acquire(1); |
829 |
> |
java.util.Date d = new java.util.Date(); |
830 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
831 |
> |
sync.release(1); |
832 |
> |
}}); |
833 |
|
|
834 |
< |
try { |
835 |
< |
t.start(); |
836 |
< |
Thread.sleep(SHORT_DELAY_MS); |
837 |
< |
t.interrupt(); |
838 |
< |
t.join(SHORT_DELAY_MS); |
1053 |
< |
assertFalse(t.isAlive()); |
1054 |
< |
} |
1055 |
< |
catch (Exception ex) { |
1056 |
< |
unexpectedException(); |
1057 |
< |
} |
834 |
> |
t.start(); |
835 |
> |
Thread.sleep(SHORT_DELAY_MS); |
836 |
> |
t.interrupt(); |
837 |
> |
t.join(SHORT_DELAY_MS); |
838 |
> |
assertFalse(t.isAlive()); |
839 |
|
} |
840 |
|
|
841 |
|
/** |
842 |
|
* signalAll wakes up all threads |
843 |
|
*/ |
844 |
< |
public void testSignalAll() { |
844 |
> |
public void testSignalAll() throws InterruptedException { |
845 |
|
final Mutex sync = new Mutex(); |
846 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
847 |
< |
Thread t1 = new Thread(new Runnable() { |
848 |
< |
public void run() { |
849 |
< |
try { |
850 |
< |
sync.acquire(1); |
851 |
< |
c.await(); |
852 |
< |
sync.release(1); |
853 |
< |
} |
854 |
< |
catch (InterruptedException e) { |
855 |
< |
threadUnexpectedException(); |
856 |
< |
} |
857 |
< |
} |
858 |
< |
}); |
859 |
< |
|
860 |
< |
Thread t2 = new Thread(new Runnable() { |
861 |
< |
public void run() { |
862 |
< |
try { |
863 |
< |
sync.acquire(1); |
864 |
< |
c.await(); |
865 |
< |
sync.release(1); |
866 |
< |
} |
867 |
< |
catch (InterruptedException e) { |
868 |
< |
threadUnexpectedException(); |
869 |
< |
} |
870 |
< |
} |
1090 |
< |
}); |
1091 |
< |
|
1092 |
< |
try { |
1093 |
< |
t1.start(); |
1094 |
< |
t2.start(); |
1095 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1096 |
< |
sync.acquire(1); |
1097 |
< |
c.signalAll(); |
1098 |
< |
sync.release(1); |
1099 |
< |
t1.join(SHORT_DELAY_MS); |
1100 |
< |
t2.join(SHORT_DELAY_MS); |
1101 |
< |
assertFalse(t1.isAlive()); |
1102 |
< |
assertFalse(t2.isAlive()); |
1103 |
< |
} |
1104 |
< |
catch (Exception ex) { |
1105 |
< |
unexpectedException(); |
1106 |
< |
} |
847 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
848 |
> |
public void realRun() throws InterruptedException { |
849 |
> |
sync.acquire(1); |
850 |
> |
c.await(); |
851 |
> |
sync.release(1); |
852 |
> |
}}); |
853 |
> |
|
854 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
855 |
> |
public void realRun() throws InterruptedException { |
856 |
> |
sync.acquire(1); |
857 |
> |
c.await(); |
858 |
> |
sync.release(1); |
859 |
> |
}}); |
860 |
> |
|
861 |
> |
t1.start(); |
862 |
> |
t2.start(); |
863 |
> |
Thread.sleep(SHORT_DELAY_MS); |
864 |
> |
sync.acquire(1); |
865 |
> |
c.signalAll(); |
866 |
> |
sync.release(1); |
867 |
> |
t1.join(SHORT_DELAY_MS); |
868 |
> |
t2.join(SHORT_DELAY_MS); |
869 |
> |
assertFalse(t1.isAlive()); |
870 |
> |
assertFalse(t2.isAlive()); |
871 |
|
} |
872 |
|
|
873 |
|
|
886 |
|
/** |
887 |
|
* A serialized AQS deserializes with current state |
888 |
|
*/ |
889 |
< |
public void testSerialization() { |
889 |
> |
public void testSerialization() throws Exception { |
890 |
|
Mutex l = new Mutex(); |
891 |
|
l.acquire(1); |
892 |
|
assertTrue(l.isHeldExclusively()); |
893 |
|
|
894 |
< |
try { |
895 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
896 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
897 |
< |
out.writeObject(l); |
898 |
< |
out.close(); |
899 |
< |
|
900 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
901 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
902 |
< |
Mutex r = (Mutex) in.readObject(); |
1139 |
< |
assertTrue(r.isHeldExclusively()); |
1140 |
< |
} catch (Exception e) { |
1141 |
< |
e.printStackTrace(); |
1142 |
< |
unexpectedException(); |
1143 |
< |
} |
894 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
895 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
896 |
> |
out.writeObject(l); |
897 |
> |
out.close(); |
898 |
> |
|
899 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
900 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
901 |
> |
Mutex r = (Mutex) in.readObject(); |
902 |
> |
assertTrue(r.isHeldExclusively()); |
903 |
|
} |
904 |
|
|
905 |
|
|
928 |
|
/** |
929 |
|
* acquireSharedInterruptibly returns after release, but not before |
930 |
|
*/ |
931 |
< |
public void testAcquireSharedInterruptibly() { |
931 |
> |
public void testAcquireSharedInterruptibly() throws InterruptedException { |
932 |
|
final BooleanLatch l = new BooleanLatch(); |
933 |
|
|
934 |
< |
Thread t = new Thread(new Runnable() { |
935 |
< |
public void run() { |
936 |
< |
try { |
937 |
< |
threadAssertFalse(l.isSignalled()); |
938 |
< |
l.acquireSharedInterruptibly(0); |
939 |
< |
threadAssertTrue(l.isSignalled()); |
940 |
< |
} catch (InterruptedException e) { |
941 |
< |
threadUnexpectedException(); |
942 |
< |
} |
943 |
< |
} |
944 |
< |
}); |
945 |
< |
try { |
946 |
< |
t.start(); |
1188 |
< |
assertFalse(l.isSignalled()); |
1189 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1190 |
< |
l.releaseShared(0); |
1191 |
< |
assertTrue(l.isSignalled()); |
1192 |
< |
t.join(); |
1193 |
< |
} catch (InterruptedException e) { |
1194 |
< |
unexpectedException(); |
1195 |
< |
} |
934 |
> |
Thread t = new Thread(new CheckedRunnable() { |
935 |
> |
public void realRun() throws InterruptedException { |
936 |
> |
threadAssertFalse(l.isSignalled()); |
937 |
> |
l.acquireSharedInterruptibly(0); |
938 |
> |
threadAssertTrue(l.isSignalled()); |
939 |
> |
}}); |
940 |
> |
|
941 |
> |
t.start(); |
942 |
> |
assertFalse(l.isSignalled()); |
943 |
> |
Thread.sleep(SHORT_DELAY_MS); |
944 |
> |
l.releaseShared(0); |
945 |
> |
assertTrue(l.isSignalled()); |
946 |
> |
t.join(); |
947 |
|
} |
948 |
|
|
949 |
|
|
950 |
|
/** |
951 |
|
* acquireSharedTimed returns after release |
952 |
|
*/ |
953 |
< |
public void testAsquireSharedTimed() { |
953 |
> |
public void testAsquireSharedTimed() throws InterruptedException { |
954 |
|
final BooleanLatch l = new BooleanLatch(); |
955 |
|
|
956 |
< |
Thread t = new Thread(new Runnable() { |
957 |
< |
public void run() { |
958 |
< |
try { |
959 |
< |
threadAssertFalse(l.isSignalled()); |
960 |
< |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
961 |
< |
threadAssertTrue(l.isSignalled()); |
962 |
< |
|
963 |
< |
} catch (InterruptedException e) { |
964 |
< |
threadUnexpectedException(); |
965 |
< |
} |
966 |
< |
} |
967 |
< |
}); |
968 |
< |
try { |
1218 |
< |
t.start(); |
1219 |
< |
assertFalse(l.isSignalled()); |
1220 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1221 |
< |
l.releaseShared(0); |
1222 |
< |
assertTrue(l.isSignalled()); |
1223 |
< |
t.join(); |
1224 |
< |
} catch (InterruptedException e) { |
1225 |
< |
unexpectedException(); |
1226 |
< |
} |
956 |
> |
Thread t = new Thread(new CheckedRunnable() { |
957 |
> |
public void realRun() throws InterruptedException { |
958 |
> |
threadAssertFalse(l.isSignalled()); |
959 |
> |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
960 |
> |
threadAssertTrue(l.isSignalled()); |
961 |
> |
}}); |
962 |
> |
|
963 |
> |
t.start(); |
964 |
> |
assertFalse(l.isSignalled()); |
965 |
> |
Thread.sleep(SHORT_DELAY_MS); |
966 |
> |
l.releaseShared(0); |
967 |
> |
assertTrue(l.isSignalled()); |
968 |
> |
t.join(); |
969 |
|
} |
970 |
|
|
971 |
|
/** |
972 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
973 |
|
*/ |
974 |
< |
public void testAcquireSharedInterruptibly_InterruptedException() { |
974 |
> |
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
975 |
|
final BooleanLatch l = new BooleanLatch(); |
976 |
< |
Thread t = new Thread(new Runnable() { |
977 |
< |
public void run() { |
978 |
< |
try { |
979 |
< |
threadAssertFalse(l.isSignalled()); |
980 |
< |
l.acquireSharedInterruptibly(0); |
981 |
< |
threadShouldThrow(); |
1240 |
< |
} catch (InterruptedException success) {} |
1241 |
< |
} |
1242 |
< |
}); |
976 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
977 |
> |
public void realRun() throws InterruptedException { |
978 |
> |
threadAssertFalse(l.isSignalled()); |
979 |
> |
l.acquireSharedInterruptibly(0); |
980 |
> |
}}); |
981 |
> |
|
982 |
|
t.start(); |
983 |
< |
try { |
984 |
< |
assertFalse(l.isSignalled()); |
985 |
< |
t.interrupt(); |
1247 |
< |
t.join(); |
1248 |
< |
} catch (InterruptedException e) { |
1249 |
< |
unexpectedException(); |
1250 |
< |
} |
983 |
> |
assertFalse(l.isSignalled()); |
984 |
> |
t.interrupt(); |
985 |
> |
t.join(); |
986 |
|
} |
987 |
|
|
988 |
|
/** |
989 |
|
* acquireSharedTimed throws IE if interrupted before released |
990 |
|
*/ |
991 |
< |
public void testAcquireSharedNanos_InterruptedException() { |
991 |
> |
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
992 |
|
final BooleanLatch l = new BooleanLatch(); |
993 |
< |
Thread t = new Thread(new Runnable() { |
994 |
< |
public void run() { |
995 |
< |
try { |
996 |
< |
threadAssertFalse(l.isSignalled()); |
997 |
< |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
998 |
< |
threadShouldThrow(); |
1264 |
< |
} catch (InterruptedException success) {} |
1265 |
< |
} |
1266 |
< |
}); |
993 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
994 |
> |
public void realRun() throws InterruptedException { |
995 |
> |
threadAssertFalse(l.isSignalled()); |
996 |
> |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
997 |
> |
}}); |
998 |
> |
|
999 |
|
t.start(); |
1000 |
< |
try { |
1001 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1002 |
< |
assertFalse(l.isSignalled()); |
1003 |
< |
t.interrupt(); |
1272 |
< |
t.join(); |
1273 |
< |
} catch (InterruptedException e) { |
1274 |
< |
unexpectedException(); |
1275 |
< |
} |
1000 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1001 |
> |
assertFalse(l.isSignalled()); |
1002 |
> |
t.interrupt(); |
1003 |
> |
t.join(); |
1004 |
|
} |
1005 |
|
|
1006 |
|
/** |
1007 |
|
* acquireSharedTimed times out if not released before timeout |
1008 |
|
*/ |
1009 |
< |
public void testAcquireSharedNanos_Timeout() { |
1009 |
> |
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1010 |
|
final BooleanLatch l = new BooleanLatch(); |
1011 |
< |
Thread t = new Thread(new Runnable() { |
1012 |
< |
public void run() { |
1013 |
< |
try { |
1014 |
< |
threadAssertFalse(l.isSignalled()); |
1015 |
< |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1016 |
< |
} catch (InterruptedException ie) { |
1289 |
< |
threadUnexpectedException(); |
1290 |
< |
} |
1291 |
< |
} |
1292 |
< |
}); |
1011 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1012 |
> |
public void realRun() throws InterruptedException { |
1013 |
> |
threadAssertFalse(l.isSignalled()); |
1014 |
> |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1015 |
> |
}}); |
1016 |
> |
|
1017 |
|
t.start(); |
1018 |
< |
try { |
1019 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1020 |
< |
assertFalse(l.isSignalled()); |
1297 |
< |
t.join(); |
1298 |
< |
} catch (InterruptedException e) { |
1299 |
< |
unexpectedException(); |
1300 |
< |
} |
1018 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1019 |
> |
assertFalse(l.isSignalled()); |
1020 |
> |
t.join(); |
1021 |
|
} |
1022 |
|
|
1303 |
– |
|
1023 |
|
} |