23 |
|
/** |
24 |
|
* A runnable calling lockInterruptibly |
25 |
|
*/ |
26 |
< |
class InterruptibleLockRunnable implements Runnable { |
26 |
> |
class InterruptibleLockRunnable extends CheckedRunnable { |
27 |
|
final ReentrantLock lock; |
28 |
|
InterruptibleLockRunnable(ReentrantLock l) { lock = l; } |
29 |
< |
public void run() { |
30 |
< |
try { |
31 |
< |
lock.lockInterruptibly(); |
32 |
< |
} catch (InterruptedException success) {} |
29 |
> |
public void realRun() throws InterruptedException { |
30 |
> |
lock.lockInterruptibly(); |
31 |
|
} |
32 |
|
} |
33 |
|
|
36 |
|
* A runnable calling lockInterruptibly that expects to be |
37 |
|
* interrupted |
38 |
|
*/ |
39 |
< |
class InterruptedLockRunnable implements Runnable { |
39 |
> |
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
40 |
|
final ReentrantLock lock; |
41 |
|
InterruptedLockRunnable(ReentrantLock l) { lock = l; } |
42 |
< |
public void run() { |
43 |
< |
try { |
46 |
< |
lock.lockInterruptibly(); |
47 |
< |
threadShouldThrow(); |
48 |
< |
} catch (InterruptedException success) {} |
42 |
> |
public void realRun() throws InterruptedException { |
43 |
> |
lock.lockInterruptibly(); |
44 |
|
} |
45 |
|
} |
46 |
|
|
63 |
|
* Constructor sets given fairness |
64 |
|
*/ |
65 |
|
public void testConstructor() { |
66 |
< |
ReentrantLock rl = new ReentrantLock(); |
67 |
< |
assertFalse(rl.isFair()); |
68 |
< |
ReentrantLock r2 = new ReentrantLock(true); |
74 |
< |
assertTrue(r2.isFair()); |
66 |
> |
assertFalse(new ReentrantLock().isFair()); |
67 |
> |
assertFalse(new ReentrantLock(false).isFair()); |
68 |
> |
assertTrue(new ReentrantLock(true).isFair()); |
69 |
|
} |
70 |
|
|
71 |
|
/** |
76 |
|
rl.lock(); |
77 |
|
assertTrue(rl.isLocked()); |
78 |
|
rl.unlock(); |
79 |
+ |
assertFalse(rl.isLocked()); |
80 |
|
} |
81 |
|
|
82 |
|
/** |
97 |
|
try { |
98 |
|
rl.unlock(); |
99 |
|
shouldThrow(); |
105 |
– |
|
100 |
|
} catch (IllegalMonitorStateException success) {} |
101 |
|
} |
102 |
|
|
114 |
|
/** |
115 |
|
* hasQueuedThreads reports whether there are waiting threads |
116 |
|
*/ |
117 |
< |
public void testhasQueuedThreads() { |
117 |
> |
public void testhasQueuedThreads() throws InterruptedException { |
118 |
|
final ReentrantLock lock = new ReentrantLock(); |
119 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
120 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
121 |
< |
try { |
122 |
< |
assertFalse(lock.hasQueuedThreads()); |
123 |
< |
lock.lock(); |
124 |
< |
t1.start(); |
125 |
< |
Thread.sleep(SHORT_DELAY_MS); |
126 |
< |
assertTrue(lock.hasQueuedThreads()); |
127 |
< |
t2.start(); |
128 |
< |
Thread.sleep(SHORT_DELAY_MS); |
129 |
< |
assertTrue(lock.hasQueuedThreads()); |
130 |
< |
t1.interrupt(); |
131 |
< |
Thread.sleep(SHORT_DELAY_MS); |
132 |
< |
assertTrue(lock.hasQueuedThreads()); |
133 |
< |
lock.unlock(); |
134 |
< |
Thread.sleep(SHORT_DELAY_MS); |
135 |
< |
assertFalse(lock.hasQueuedThreads()); |
136 |
< |
t1.join(); |
143 |
< |
t2.join(); |
144 |
< |
} catch (Exception e) { |
145 |
< |
unexpectedException(); |
146 |
< |
} |
121 |
> |
assertFalse(lock.hasQueuedThreads()); |
122 |
> |
lock.lock(); |
123 |
> |
t1.start(); |
124 |
> |
Thread.sleep(SHORT_DELAY_MS); |
125 |
> |
assertTrue(lock.hasQueuedThreads()); |
126 |
> |
t2.start(); |
127 |
> |
Thread.sleep(SHORT_DELAY_MS); |
128 |
> |
assertTrue(lock.hasQueuedThreads()); |
129 |
> |
t1.interrupt(); |
130 |
> |
Thread.sleep(SHORT_DELAY_MS); |
131 |
> |
assertTrue(lock.hasQueuedThreads()); |
132 |
> |
lock.unlock(); |
133 |
> |
Thread.sleep(SHORT_DELAY_MS); |
134 |
> |
assertFalse(lock.hasQueuedThreads()); |
135 |
> |
t1.join(); |
136 |
> |
t2.join(); |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
|
* getQueueLength reports number of waiting threads |
141 |
|
*/ |
142 |
< |
public void testGetQueueLength() { |
142 |
> |
public void testGetQueueLength() throws InterruptedException { |
143 |
|
final ReentrantLock lock = new ReentrantLock(); |
144 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
145 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
146 |
< |
try { |
147 |
< |
assertEquals(0, lock.getQueueLength()); |
148 |
< |
lock.lock(); |
149 |
< |
t1.start(); |
150 |
< |
Thread.sleep(SHORT_DELAY_MS); |
151 |
< |
assertEquals(1, lock.getQueueLength()); |
152 |
< |
t2.start(); |
153 |
< |
Thread.sleep(SHORT_DELAY_MS); |
154 |
< |
assertEquals(2, lock.getQueueLength()); |
155 |
< |
t1.interrupt(); |
156 |
< |
Thread.sleep(SHORT_DELAY_MS); |
157 |
< |
assertEquals(1, lock.getQueueLength()); |
158 |
< |
lock.unlock(); |
159 |
< |
Thread.sleep(SHORT_DELAY_MS); |
160 |
< |
assertEquals(0, lock.getQueueLength()); |
161 |
< |
t1.join(); |
172 |
< |
t2.join(); |
173 |
< |
} catch (Exception e) { |
174 |
< |
unexpectedException(); |
175 |
< |
} |
146 |
> |
assertEquals(0, lock.getQueueLength()); |
147 |
> |
lock.lock(); |
148 |
> |
t1.start(); |
149 |
> |
Thread.sleep(SHORT_DELAY_MS); |
150 |
> |
assertEquals(1, lock.getQueueLength()); |
151 |
> |
t2.start(); |
152 |
> |
Thread.sleep(SHORT_DELAY_MS); |
153 |
> |
assertEquals(2, lock.getQueueLength()); |
154 |
> |
t1.interrupt(); |
155 |
> |
Thread.sleep(SHORT_DELAY_MS); |
156 |
> |
assertEquals(1, lock.getQueueLength()); |
157 |
> |
lock.unlock(); |
158 |
> |
Thread.sleep(SHORT_DELAY_MS); |
159 |
> |
assertEquals(0, lock.getQueueLength()); |
160 |
> |
t1.join(); |
161 |
> |
t2.join(); |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
|
* getQueueLength reports number of waiting threads |
166 |
|
*/ |
167 |
< |
public void testGetQueueLength_fair() { |
167 |
> |
public void testGetQueueLength_fair() throws InterruptedException { |
168 |
|
final ReentrantLock lock = new ReentrantLock(true); |
169 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
170 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
171 |
< |
try { |
172 |
< |
assertEquals(0, lock.getQueueLength()); |
173 |
< |
lock.lock(); |
174 |
< |
t1.start(); |
175 |
< |
Thread.sleep(SHORT_DELAY_MS); |
176 |
< |
assertEquals(1, lock.getQueueLength()); |
177 |
< |
t2.start(); |
178 |
< |
Thread.sleep(SHORT_DELAY_MS); |
179 |
< |
assertEquals(2, lock.getQueueLength()); |
180 |
< |
t1.interrupt(); |
181 |
< |
Thread.sleep(SHORT_DELAY_MS); |
182 |
< |
assertEquals(1, lock.getQueueLength()); |
183 |
< |
lock.unlock(); |
184 |
< |
Thread.sleep(SHORT_DELAY_MS); |
185 |
< |
assertEquals(0, lock.getQueueLength()); |
186 |
< |
t1.join(); |
201 |
< |
t2.join(); |
202 |
< |
} catch (Exception e) { |
203 |
< |
unexpectedException(); |
204 |
< |
} |
171 |
> |
assertEquals(0, lock.getQueueLength()); |
172 |
> |
lock.lock(); |
173 |
> |
t1.start(); |
174 |
> |
Thread.sleep(SHORT_DELAY_MS); |
175 |
> |
assertEquals(1, lock.getQueueLength()); |
176 |
> |
t2.start(); |
177 |
> |
Thread.sleep(SHORT_DELAY_MS); |
178 |
> |
assertEquals(2, lock.getQueueLength()); |
179 |
> |
t1.interrupt(); |
180 |
> |
Thread.sleep(SHORT_DELAY_MS); |
181 |
> |
assertEquals(1, lock.getQueueLength()); |
182 |
> |
lock.unlock(); |
183 |
> |
Thread.sleep(SHORT_DELAY_MS); |
184 |
> |
assertEquals(0, lock.getQueueLength()); |
185 |
> |
t1.join(); |
186 |
> |
t2.join(); |
187 |
|
} |
188 |
|
|
189 |
|
/** |
194 |
|
try { |
195 |
|
sync.hasQueuedThread(null); |
196 |
|
shouldThrow(); |
197 |
< |
} catch (NullPointerException success) { |
216 |
< |
} |
197 |
> |
} catch (NullPointerException success) {} |
198 |
|
} |
199 |
|
|
200 |
|
/** |
201 |
|
* hasQueuedThread reports whether a thread is queued. |
202 |
|
*/ |
203 |
< |
public void testHasQueuedThread() { |
203 |
> |
public void testHasQueuedThread() throws InterruptedException { |
204 |
|
final ReentrantLock sync = new ReentrantLock(); |
205 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
206 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
207 |
< |
try { |
208 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
209 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
210 |
< |
sync.lock(); |
211 |
< |
t1.start(); |
212 |
< |
Thread.sleep(SHORT_DELAY_MS); |
213 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
214 |
< |
t2.start(); |
215 |
< |
Thread.sleep(SHORT_DELAY_MS); |
216 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
217 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
218 |
< |
t1.interrupt(); |
219 |
< |
Thread.sleep(SHORT_DELAY_MS); |
220 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
221 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
222 |
< |
sync.unlock(); |
223 |
< |
Thread.sleep(SHORT_DELAY_MS); |
224 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
225 |
< |
Thread.sleep(SHORT_DELAY_MS); |
226 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
227 |
< |
t1.join(); |
247 |
< |
t2.join(); |
248 |
< |
} catch (Exception e) { |
249 |
< |
unexpectedException(); |
250 |
< |
} |
207 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
208 |
> |
assertFalse(sync.hasQueuedThread(t2)); |
209 |
> |
sync.lock(); |
210 |
> |
t1.start(); |
211 |
> |
Thread.sleep(SHORT_DELAY_MS); |
212 |
> |
assertTrue(sync.hasQueuedThread(t1)); |
213 |
> |
t2.start(); |
214 |
> |
Thread.sleep(SHORT_DELAY_MS); |
215 |
> |
assertTrue(sync.hasQueuedThread(t1)); |
216 |
> |
assertTrue(sync.hasQueuedThread(t2)); |
217 |
> |
t1.interrupt(); |
218 |
> |
Thread.sleep(SHORT_DELAY_MS); |
219 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
220 |
> |
assertTrue(sync.hasQueuedThread(t2)); |
221 |
> |
sync.unlock(); |
222 |
> |
Thread.sleep(SHORT_DELAY_MS); |
223 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
224 |
> |
Thread.sleep(SHORT_DELAY_MS); |
225 |
> |
assertFalse(sync.hasQueuedThread(t2)); |
226 |
> |
t1.join(); |
227 |
> |
t2.join(); |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
/** |
232 |
|
* getQueuedThreads includes waiting threads |
233 |
|
*/ |
234 |
< |
public void testGetQueuedThreads() { |
234 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
235 |
|
final PublicReentrantLock lock = new PublicReentrantLock(); |
236 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
237 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
238 |
< |
try { |
239 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
240 |
< |
lock.lock(); |
241 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
242 |
< |
t1.start(); |
243 |
< |
Thread.sleep(SHORT_DELAY_MS); |
244 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
245 |
< |
t2.start(); |
246 |
< |
Thread.sleep(SHORT_DELAY_MS); |
247 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
248 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
249 |
< |
t1.interrupt(); |
250 |
< |
Thread.sleep(SHORT_DELAY_MS); |
251 |
< |
assertFalse(lock.getQueuedThreads().contains(t1)); |
252 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
253 |
< |
lock.unlock(); |
254 |
< |
Thread.sleep(SHORT_DELAY_MS); |
255 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
256 |
< |
t1.join(); |
280 |
< |
t2.join(); |
281 |
< |
} catch (Exception e) { |
282 |
< |
unexpectedException(); |
283 |
< |
} |
238 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
239 |
> |
lock.lock(); |
240 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
241 |
> |
t1.start(); |
242 |
> |
Thread.sleep(SHORT_DELAY_MS); |
243 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
244 |
> |
t2.start(); |
245 |
> |
Thread.sleep(SHORT_DELAY_MS); |
246 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
247 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
248 |
> |
t1.interrupt(); |
249 |
> |
Thread.sleep(SHORT_DELAY_MS); |
250 |
> |
assertFalse(lock.getQueuedThreads().contains(t1)); |
251 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
252 |
> |
lock.unlock(); |
253 |
> |
Thread.sleep(SHORT_DELAY_MS); |
254 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
255 |
> |
t1.join(); |
256 |
> |
t2.join(); |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
/** |
261 |
|
* timed tryLock is interruptible. |
262 |
|
*/ |
263 |
< |
public void testInterruptedException2() { |
263 |
> |
public void testInterruptedException2() throws InterruptedException { |
264 |
|
final ReentrantLock lock = new ReentrantLock(); |
265 |
|
lock.lock(); |
266 |
< |
Thread t = new Thread(new Runnable() { |
267 |
< |
public void run() { |
268 |
< |
try { |
269 |
< |
lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); |
270 |
< |
threadShouldThrow(); |
271 |
< |
} catch (InterruptedException success) {} |
272 |
< |
} |
273 |
< |
}); |
301 |
< |
try { |
302 |
< |
t.start(); |
303 |
< |
t.interrupt(); |
304 |
< |
} catch (Exception e) { |
305 |
< |
unexpectedException(); |
306 |
< |
} |
266 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
267 |
> |
public void realRun() throws InterruptedException { |
268 |
> |
lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); |
269 |
> |
}}); |
270 |
> |
|
271 |
> |
t.start(); |
272 |
> |
t.interrupt(); |
273 |
> |
t.join(); |
274 |
|
} |
275 |
|
|
276 |
|
|
277 |
|
/** |
278 |
|
* TryLock on a locked lock fails |
279 |
|
*/ |
280 |
< |
public void testTryLockWhenLocked() { |
280 |
> |
public void testTryLockWhenLocked() throws InterruptedException { |
281 |
|
final ReentrantLock lock = new ReentrantLock(); |
282 |
|
lock.lock(); |
283 |
< |
Thread t = new Thread(new Runnable() { |
284 |
< |
public void run() { |
285 |
< |
threadAssertFalse(lock.tryLock()); |
286 |
< |
} |
287 |
< |
}); |
288 |
< |
try { |
289 |
< |
t.start(); |
290 |
< |
t.join(); |
324 |
< |
lock.unlock(); |
325 |
< |
} catch (Exception e) { |
326 |
< |
unexpectedException(); |
327 |
< |
} |
283 |
> |
Thread t = new Thread(new CheckedRunnable() { |
284 |
> |
public void realRun() { |
285 |
> |
threadAssertFalse(lock.tryLock()); |
286 |
> |
}}); |
287 |
> |
|
288 |
> |
t.start(); |
289 |
> |
t.join(); |
290 |
> |
lock.unlock(); |
291 |
|
} |
292 |
|
|
293 |
|
/** |
294 |
|
* Timed tryLock on a locked lock times out |
295 |
|
*/ |
296 |
< |
public void testTryLock_Timeout() { |
296 |
> |
public void testTryLock_Timeout() throws InterruptedException { |
297 |
|
final ReentrantLock lock = new ReentrantLock(); |
298 |
|
lock.lock(); |
299 |
< |
Thread t = new Thread(new Runnable() { |
300 |
< |
public void run() { |
301 |
< |
try { |
302 |
< |
threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
303 |
< |
} catch (Exception ex) { |
304 |
< |
threadUnexpectedException(); |
305 |
< |
} |
306 |
< |
} |
344 |
< |
}); |
345 |
< |
try { |
346 |
< |
t.start(); |
347 |
< |
t.join(); |
348 |
< |
lock.unlock(); |
349 |
< |
} catch (Exception e) { |
350 |
< |
unexpectedException(); |
351 |
< |
} |
299 |
> |
Thread t = new Thread(new CheckedRunnable() { |
300 |
> |
public void realRun() throws InterruptedException { |
301 |
> |
threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
302 |
> |
}}); |
303 |
> |
|
304 |
> |
t.start(); |
305 |
> |
t.join(); |
306 |
> |
lock.unlock(); |
307 |
|
} |
308 |
|
|
309 |
|
/** |
313 |
|
ReentrantLock lock = new ReentrantLock(); |
314 |
|
for (int i = 1; i <= SIZE; i++) { |
315 |
|
lock.lock(); |
316 |
< |
assertEquals(i,lock.getHoldCount()); |
316 |
> |
assertEquals(i, lock.getHoldCount()); |
317 |
|
} |
318 |
|
for (int i = SIZE; i > 0; i--) { |
319 |
|
lock.unlock(); |
320 |
< |
assertEquals(i-1,lock.getHoldCount()); |
320 |
> |
assertEquals(i-1, lock.getHoldCount()); |
321 |
|
} |
322 |
|
} |
323 |
|
|
325 |
|
/** |
326 |
|
* isLocked is true when locked and false when not |
327 |
|
*/ |
328 |
< |
public void testIsLocked() { |
328 |
> |
public void testIsLocked() throws InterruptedException { |
329 |
|
final ReentrantLock lock = new ReentrantLock(); |
330 |
|
lock.lock(); |
331 |
|
assertTrue(lock.isLocked()); |
332 |
|
lock.unlock(); |
333 |
|
assertFalse(lock.isLocked()); |
334 |
< |
Thread t = new Thread(new Runnable() { |
335 |
< |
public void run() { |
336 |
< |
lock.lock(); |
337 |
< |
try { |
338 |
< |
Thread.sleep(SMALL_DELAY_MS); |
339 |
< |
} |
340 |
< |
catch (Exception e) { |
341 |
< |
threadUnexpectedException(); |
342 |
< |
} |
343 |
< |
lock.unlock(); |
344 |
< |
} |
345 |
< |
}); |
391 |
< |
try { |
392 |
< |
t.start(); |
393 |
< |
Thread.sleep(SHORT_DELAY_MS); |
394 |
< |
assertTrue(lock.isLocked()); |
395 |
< |
t.join(); |
396 |
< |
assertFalse(lock.isLocked()); |
397 |
< |
} catch (Exception e) { |
398 |
< |
unexpectedException(); |
399 |
< |
} |
334 |
> |
Thread t = new Thread(new CheckedRunnable() { |
335 |
> |
public void realRun() throws InterruptedException { |
336 |
> |
lock.lock(); |
337 |
> |
Thread.sleep(SMALL_DELAY_MS); |
338 |
> |
lock.unlock(); |
339 |
> |
}}); |
340 |
> |
|
341 |
> |
t.start(); |
342 |
> |
Thread.sleep(SHORT_DELAY_MS); |
343 |
> |
assertTrue(lock.isLocked()); |
344 |
> |
t.join(); |
345 |
> |
assertFalse(lock.isLocked()); |
346 |
|
} |
347 |
|
|
348 |
|
|
349 |
|
/** |
350 |
|
* lockInterruptibly is interruptible. |
351 |
|
*/ |
352 |
< |
public void testLockInterruptibly1() { |
352 |
> |
public void testLockInterruptibly1() throws InterruptedException { |
353 |
|
final ReentrantLock lock = new ReentrantLock(); |
354 |
|
lock.lock(); |
355 |
|
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
356 |
< |
try { |
357 |
< |
t.start(); |
358 |
< |
Thread.sleep(SHORT_DELAY_MS); |
359 |
< |
t.interrupt(); |
360 |
< |
Thread.sleep(SHORT_DELAY_MS); |
361 |
< |
lock.unlock(); |
416 |
< |
t.join(); |
417 |
< |
} catch (Exception e) { |
418 |
< |
unexpectedException(); |
419 |
< |
} |
356 |
> |
t.start(); |
357 |
> |
Thread.sleep(SHORT_DELAY_MS); |
358 |
> |
t.interrupt(); |
359 |
> |
Thread.sleep(SHORT_DELAY_MS); |
360 |
> |
lock.unlock(); |
361 |
> |
t.join(); |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
|
* lockInterruptibly succeeds when unlocked, else is interruptible |
366 |
|
*/ |
367 |
< |
public void testLockInterruptibly2() { |
367 |
> |
public void testLockInterruptibly2() throws InterruptedException { |
368 |
|
final ReentrantLock lock = new ReentrantLock(); |
369 |
< |
try { |
428 |
< |
lock.lockInterruptibly(); |
429 |
< |
} catch (Exception e) { |
430 |
< |
unexpectedException(); |
431 |
< |
} |
369 |
> |
lock.lockInterruptibly(); |
370 |
|
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
371 |
< |
try { |
372 |
< |
t.start(); |
373 |
< |
t.interrupt(); |
374 |
< |
assertTrue(lock.isLocked()); |
375 |
< |
assertTrue(lock.isHeldByCurrentThread()); |
438 |
< |
t.join(); |
439 |
< |
} catch (Exception e) { |
440 |
< |
unexpectedException(); |
441 |
< |
} |
371 |
> |
t.start(); |
372 |
> |
t.interrupt(); |
373 |
> |
assertTrue(lock.isLocked()); |
374 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
375 |
> |
t.join(); |
376 |
|
} |
377 |
|
|
378 |
|
/** |
379 |
|
* Calling await without holding lock throws IllegalMonitorStateException |
380 |
|
*/ |
381 |
< |
public void testAwait_IllegalMonitor() { |
381 |
> |
public void testAwait_IllegalMonitor() throws InterruptedException { |
382 |
|
final ReentrantLock lock = new ReentrantLock(); |
383 |
|
final Condition c = lock.newCondition(); |
384 |
|
try { |
385 |
|
c.await(); |
386 |
|
shouldThrow(); |
387 |
< |
} |
454 |
< |
catch (IllegalMonitorStateException success) { |
455 |
< |
} |
456 |
< |
catch (Exception ex) { |
457 |
< |
unexpectedException(); |
458 |
< |
} |
387 |
> |
} catch (IllegalMonitorStateException success) {} |
388 |
|
} |
389 |
|
|
390 |
|
/** |
396 |
|
try { |
397 |
|
c.signal(); |
398 |
|
shouldThrow(); |
399 |
< |
} |
471 |
< |
catch (IllegalMonitorStateException success) { |
472 |
< |
} |
473 |
< |
catch (Exception ex) { |
474 |
< |
unexpectedException(); |
475 |
< |
} |
399 |
> |
} catch (IllegalMonitorStateException success) {} |
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
|
* awaitNanos without a signal times out |
404 |
|
*/ |
405 |
< |
public void testAwaitNanos_Timeout() { |
405 |
> |
public void testAwaitNanos_Timeout() throws InterruptedException { |
406 |
|
final ReentrantLock lock = new ReentrantLock(); |
407 |
|
final Condition c = lock.newCondition(); |
408 |
< |
try { |
409 |
< |
lock.lock(); |
410 |
< |
long t = c.awaitNanos(100); |
411 |
< |
assertTrue(t <= 0); |
488 |
< |
lock.unlock(); |
489 |
< |
} |
490 |
< |
catch (Exception ex) { |
491 |
< |
unexpectedException(); |
492 |
< |
} |
408 |
> |
lock.lock(); |
409 |
> |
long t = c.awaitNanos(100); |
410 |
> |
assertTrue(t <= 0); |
411 |
> |
lock.unlock(); |
412 |
|
} |
413 |
|
|
414 |
|
/** |
415 |
|
* timed await without a signal times out |
416 |
|
*/ |
417 |
< |
public void testAwait_Timeout() { |
417 |
> |
public void testAwait_Timeout() throws InterruptedException { |
418 |
|
final ReentrantLock lock = new ReentrantLock(); |
419 |
|
final Condition c = lock.newCondition(); |
420 |
< |
try { |
421 |
< |
lock.lock(); |
422 |
< |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
504 |
< |
lock.unlock(); |
505 |
< |
} |
506 |
< |
catch (Exception ex) { |
507 |
< |
unexpectedException(); |
508 |
< |
} |
420 |
> |
lock.lock(); |
421 |
> |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
422 |
> |
lock.unlock(); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
|
* awaitUntil without a signal times out |
427 |
|
*/ |
428 |
< |
public void testAwaitUntil_Timeout() { |
428 |
> |
public void testAwaitUntil_Timeout() throws InterruptedException { |
429 |
|
final ReentrantLock lock = new ReentrantLock(); |
430 |
|
final Condition c = lock.newCondition(); |
431 |
< |
try { |
432 |
< |
lock.lock(); |
433 |
< |
java.util.Date d = new java.util.Date(); |
434 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10)); |
521 |
< |
lock.unlock(); |
522 |
< |
} |
523 |
< |
catch (Exception ex) { |
524 |
< |
unexpectedException(); |
525 |
< |
} |
431 |
> |
lock.lock(); |
432 |
> |
java.util.Date d = new java.util.Date(); |
433 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
434 |
> |
lock.unlock(); |
435 |
|
} |
436 |
|
|
437 |
|
/** |
438 |
|
* await returns when signalled |
439 |
|
*/ |
440 |
< |
public void testAwait() { |
440 |
> |
public void testAwait() throws InterruptedException { |
441 |
|
final ReentrantLock lock = new ReentrantLock(); |
442 |
|
final Condition c = lock.newCondition(); |
443 |
< |
Thread t = new Thread(new Runnable() { |
444 |
< |
public void run() { |
445 |
< |
try { |
446 |
< |
lock.lock(); |
447 |
< |
c.await(); |
448 |
< |
lock.unlock(); |
540 |
< |
} |
541 |
< |
catch (InterruptedException e) { |
542 |
< |
threadUnexpectedException(); |
543 |
< |
} |
544 |
< |
} |
545 |
< |
}); |
443 |
> |
Thread t = new Thread(new CheckedRunnable() { |
444 |
> |
public void realRun() throws InterruptedException { |
445 |
> |
lock.lock(); |
446 |
> |
c.await(); |
447 |
> |
lock.unlock(); |
448 |
> |
}}); |
449 |
|
|
450 |
< |
try { |
451 |
< |
t.start(); |
452 |
< |
Thread.sleep(SHORT_DELAY_MS); |
453 |
< |
lock.lock(); |
454 |
< |
c.signal(); |
455 |
< |
lock.unlock(); |
456 |
< |
t.join(SHORT_DELAY_MS); |
554 |
< |
assertFalse(t.isAlive()); |
555 |
< |
} |
556 |
< |
catch (Exception ex) { |
557 |
< |
unexpectedException(); |
558 |
< |
} |
450 |
> |
t.start(); |
451 |
> |
Thread.sleep(SHORT_DELAY_MS); |
452 |
> |
lock.lock(); |
453 |
> |
c.signal(); |
454 |
> |
lock.unlock(); |
455 |
> |
t.join(SHORT_DELAY_MS); |
456 |
> |
assertFalse(t.isAlive()); |
457 |
|
} |
458 |
|
|
459 |
|
/** |
464 |
|
try { |
465 |
|
lock.hasWaiters(null); |
466 |
|
shouldThrow(); |
467 |
< |
} catch (NullPointerException success) { |
570 |
< |
} catch (Exception ex) { |
571 |
< |
unexpectedException(); |
572 |
< |
} |
467 |
> |
} catch (NullPointerException success) {} |
468 |
|
} |
469 |
|
|
470 |
|
/** |
475 |
|
try { |
476 |
|
lock.getWaitQueueLength(null); |
477 |
|
shouldThrow(); |
478 |
< |
} catch (NullPointerException success) { |
584 |
< |
} catch (Exception ex) { |
585 |
< |
unexpectedException(); |
586 |
< |
} |
478 |
> |
} catch (NullPointerException success) {} |
479 |
|
} |
480 |
|
|
481 |
|
|
487 |
|
try { |
488 |
|
lock.getWaitingThreads(null); |
489 |
|
shouldThrow(); |
490 |
< |
} catch (NullPointerException success) { |
599 |
< |
} catch (Exception ex) { |
600 |
< |
unexpectedException(); |
601 |
< |
} |
490 |
> |
} catch (NullPointerException success) {} |
491 |
|
} |
492 |
|
|
493 |
|
|
496 |
|
*/ |
497 |
|
public void testHasWaitersIAE() { |
498 |
|
final ReentrantLock lock = new ReentrantLock(); |
499 |
< |
final Condition c = (lock.newCondition()); |
499 |
> |
final Condition c = lock.newCondition(); |
500 |
|
final ReentrantLock lock2 = new ReentrantLock(); |
501 |
|
try { |
502 |
|
lock2.hasWaiters(c); |
503 |
|
shouldThrow(); |
504 |
< |
} catch (IllegalArgumentException success) { |
616 |
< |
} catch (Exception ex) { |
617 |
< |
unexpectedException(); |
618 |
< |
} |
504 |
> |
} catch (IllegalArgumentException success) {} |
505 |
|
} |
506 |
|
|
507 |
|
/** |
509 |
|
*/ |
510 |
|
public void testHasWaitersIMSE() { |
511 |
|
final ReentrantLock lock = new ReentrantLock(); |
512 |
< |
final Condition c = (lock.newCondition()); |
512 |
> |
final Condition c = lock.newCondition(); |
513 |
|
try { |
514 |
|
lock.hasWaiters(c); |
515 |
|
shouldThrow(); |
516 |
< |
} catch (IllegalMonitorStateException success) { |
631 |
< |
} catch (Exception ex) { |
632 |
< |
unexpectedException(); |
633 |
< |
} |
516 |
> |
} catch (IllegalMonitorStateException success) {} |
517 |
|
} |
518 |
|
|
519 |
|
|
522 |
|
*/ |
523 |
|
public void testGetWaitQueueLengthIAE() { |
524 |
|
final ReentrantLock lock = new ReentrantLock(); |
525 |
< |
final Condition c = (lock.newCondition()); |
525 |
> |
final Condition c = lock.newCondition(); |
526 |
|
final ReentrantLock lock2 = new ReentrantLock(); |
527 |
|
try { |
528 |
|
lock2.getWaitQueueLength(c); |
529 |
|
shouldThrow(); |
530 |
< |
} catch (IllegalArgumentException success) { |
648 |
< |
} catch (Exception ex) { |
649 |
< |
unexpectedException(); |
650 |
< |
} |
530 |
> |
} catch (IllegalArgumentException success) {} |
531 |
|
} |
532 |
|
|
533 |
|
/** |
535 |
|
*/ |
536 |
|
public void testGetWaitQueueLengthIMSE() { |
537 |
|
final ReentrantLock lock = new ReentrantLock(); |
538 |
< |
final Condition c = (lock.newCondition()); |
538 |
> |
final Condition c = lock.newCondition(); |
539 |
|
try { |
540 |
|
lock.getWaitQueueLength(c); |
541 |
|
shouldThrow(); |
542 |
< |
} catch (IllegalMonitorStateException success) { |
663 |
< |
} catch (Exception ex) { |
664 |
< |
unexpectedException(); |
665 |
< |
} |
542 |
> |
} catch (IllegalMonitorStateException success) {} |
543 |
|
} |
544 |
|
|
545 |
|
|
548 |
|
*/ |
549 |
|
public void testGetWaitingThreadsIAE() { |
550 |
|
final PublicReentrantLock lock = new PublicReentrantLock(); |
551 |
< |
final Condition c = (lock.newCondition()); |
551 |
> |
final Condition c = lock.newCondition(); |
552 |
|
final PublicReentrantLock lock2 = new PublicReentrantLock(); |
553 |
|
try { |
554 |
|
lock2.getWaitingThreads(c); |
555 |
|
shouldThrow(); |
556 |
< |
} catch (IllegalArgumentException success) { |
680 |
< |
} catch (Exception ex) { |
681 |
< |
unexpectedException(); |
682 |
< |
} |
556 |
> |
} catch (IllegalArgumentException success) {} |
557 |
|
} |
558 |
|
|
559 |
|
/** |
561 |
|
*/ |
562 |
|
public void testGetWaitingThreadsIMSE() { |
563 |
|
final PublicReentrantLock lock = new PublicReentrantLock(); |
564 |
< |
final Condition c = (lock.newCondition()); |
564 |
> |
final Condition c = lock.newCondition(); |
565 |
|
try { |
566 |
|
lock.getWaitingThreads(c); |
567 |
|
shouldThrow(); |
568 |
< |
} catch (IllegalMonitorStateException success) { |
695 |
< |
} catch (Exception ex) { |
696 |
< |
unexpectedException(); |
697 |
< |
} |
568 |
> |
} catch (IllegalMonitorStateException success) {} |
569 |
|
} |
570 |
|
|
571 |
|
|
701 |
– |
|
572 |
|
/** |
573 |
|
* hasWaiters returns true when a thread is waiting, else false |
574 |
|
*/ |
575 |
< |
public void testHasWaiters() { |
575 |
> |
public void testHasWaiters() throws InterruptedException { |
576 |
|
final ReentrantLock lock = new ReentrantLock(); |
577 |
|
final Condition c = lock.newCondition(); |
578 |
< |
Thread t = new Thread(new Runnable() { |
579 |
< |
public void run() { |
580 |
< |
try { |
581 |
< |
lock.lock(); |
582 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
583 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
584 |
< |
c.await(); |
585 |
< |
lock.unlock(); |
716 |
< |
} |
717 |
< |
catch (InterruptedException e) { |
718 |
< |
threadUnexpectedException(); |
719 |
< |
} |
720 |
< |
} |
721 |
< |
}); |
578 |
> |
Thread t = new Thread(new CheckedRunnable() { |
579 |
> |
public void realRun() throws InterruptedException { |
580 |
> |
lock.lock(); |
581 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
582 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
583 |
> |
c.await(); |
584 |
> |
lock.unlock(); |
585 |
> |
}}); |
586 |
|
|
587 |
< |
try { |
588 |
< |
t.start(); |
589 |
< |
Thread.sleep(SHORT_DELAY_MS); |
590 |
< |
lock.lock(); |
591 |
< |
assertTrue(lock.hasWaiters(c)); |
592 |
< |
assertEquals(1, lock.getWaitQueueLength(c)); |
593 |
< |
c.signal(); |
594 |
< |
lock.unlock(); |
595 |
< |
Thread.sleep(SHORT_DELAY_MS); |
596 |
< |
lock.lock(); |
597 |
< |
assertFalse(lock.hasWaiters(c)); |
598 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
599 |
< |
lock.unlock(); |
600 |
< |
t.join(SHORT_DELAY_MS); |
737 |
< |
assertFalse(t.isAlive()); |
738 |
< |
} |
739 |
< |
catch (Exception ex) { |
740 |
< |
unexpectedException(); |
741 |
< |
} |
587 |
> |
t.start(); |
588 |
> |
Thread.sleep(SHORT_DELAY_MS); |
589 |
> |
lock.lock(); |
590 |
> |
assertTrue(lock.hasWaiters(c)); |
591 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
592 |
> |
c.signal(); |
593 |
> |
lock.unlock(); |
594 |
> |
Thread.sleep(SHORT_DELAY_MS); |
595 |
> |
lock.lock(); |
596 |
> |
assertFalse(lock.hasWaiters(c)); |
597 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
598 |
> |
lock.unlock(); |
599 |
> |
t.join(SHORT_DELAY_MS); |
600 |
> |
assertFalse(t.isAlive()); |
601 |
|
} |
602 |
|
|
603 |
|
/** |
604 |
|
* getWaitQueueLength returns number of waiting threads |
605 |
|
*/ |
606 |
< |
public void testGetWaitQueueLength() { |
606 |
> |
public void testGetWaitQueueLength() throws InterruptedException { |
607 |
|
final ReentrantLock lock = new ReentrantLock(); |
608 |
|
final Condition c = lock.newCondition(); |
609 |
< |
Thread t1 = new Thread(new Runnable() { |
610 |
< |
public void run() { |
611 |
< |
try { |
612 |
< |
lock.lock(); |
613 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
614 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
615 |
< |
c.await(); |
616 |
< |
lock.unlock(); |
758 |
< |
} |
759 |
< |
catch (InterruptedException e) { |
760 |
< |
threadUnexpectedException(); |
761 |
< |
} |
762 |
< |
} |
763 |
< |
}); |
764 |
< |
|
765 |
< |
Thread t2 = new Thread(new Runnable() { |
766 |
< |
public void run() { |
767 |
< |
try { |
768 |
< |
lock.lock(); |
769 |
< |
threadAssertTrue(lock.hasWaiters(c)); |
770 |
< |
threadAssertEquals(1, lock.getWaitQueueLength(c)); |
771 |
< |
c.await(); |
772 |
< |
lock.unlock(); |
773 |
< |
} |
774 |
< |
catch (InterruptedException e) { |
775 |
< |
threadUnexpectedException(); |
776 |
< |
} |
777 |
< |
} |
778 |
< |
}); |
609 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
610 |
> |
public void realRun() throws InterruptedException { |
611 |
> |
lock.lock(); |
612 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
613 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
614 |
> |
c.await(); |
615 |
> |
lock.unlock(); |
616 |
> |
}}); |
617 |
|
|
618 |
< |
try { |
619 |
< |
t1.start(); |
620 |
< |
Thread.sleep(SHORT_DELAY_MS); |
621 |
< |
t2.start(); |
622 |
< |
Thread.sleep(SHORT_DELAY_MS); |
623 |
< |
lock.lock(); |
624 |
< |
assertTrue(lock.hasWaiters(c)); |
625 |
< |
assertEquals(2, lock.getWaitQueueLength(c)); |
626 |
< |
c.signalAll(); |
627 |
< |
lock.unlock(); |
628 |
< |
Thread.sleep(SHORT_DELAY_MS); |
629 |
< |
lock.lock(); |
630 |
< |
assertFalse(lock.hasWaiters(c)); |
631 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
632 |
< |
lock.unlock(); |
633 |
< |
t1.join(SHORT_DELAY_MS); |
634 |
< |
t2.join(SHORT_DELAY_MS); |
635 |
< |
assertFalse(t1.isAlive()); |
636 |
< |
assertFalse(t2.isAlive()); |
637 |
< |
} |
638 |
< |
catch (Exception ex) { |
639 |
< |
unexpectedException(); |
640 |
< |
} |
618 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
619 |
> |
public void realRun() throws InterruptedException { |
620 |
> |
lock.lock(); |
621 |
> |
threadAssertTrue(lock.hasWaiters(c)); |
622 |
> |
threadAssertEquals(1, lock.getWaitQueueLength(c)); |
623 |
> |
c.await(); |
624 |
> |
lock.unlock(); |
625 |
> |
}}); |
626 |
> |
|
627 |
> |
t1.start(); |
628 |
> |
Thread.sleep(SHORT_DELAY_MS); |
629 |
> |
t2.start(); |
630 |
> |
Thread.sleep(SHORT_DELAY_MS); |
631 |
> |
lock.lock(); |
632 |
> |
assertTrue(lock.hasWaiters(c)); |
633 |
> |
assertEquals(2, lock.getWaitQueueLength(c)); |
634 |
> |
c.signalAll(); |
635 |
> |
lock.unlock(); |
636 |
> |
Thread.sleep(SHORT_DELAY_MS); |
637 |
> |
lock.lock(); |
638 |
> |
assertFalse(lock.hasWaiters(c)); |
639 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
640 |
> |
lock.unlock(); |
641 |
> |
t1.join(SHORT_DELAY_MS); |
642 |
> |
t2.join(SHORT_DELAY_MS); |
643 |
> |
assertFalse(t1.isAlive()); |
644 |
> |
assertFalse(t2.isAlive()); |
645 |
|
} |
646 |
|
|
647 |
|
/** |
648 |
|
* getWaitingThreads returns only and all waiting threads |
649 |
|
*/ |
650 |
< |
public void testGetWaitingThreads() { |
650 |
> |
public void testGetWaitingThreads() throws InterruptedException { |
651 |
|
final PublicReentrantLock lock = new PublicReentrantLock(); |
652 |
|
final Condition c = lock.newCondition(); |
653 |
< |
Thread t1 = new Thread(new Runnable() { |
654 |
< |
public void run() { |
655 |
< |
try { |
656 |
< |
lock.lock(); |
657 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
658 |
< |
c.await(); |
659 |
< |
lock.unlock(); |
818 |
< |
} |
819 |
< |
catch (InterruptedException e) { |
820 |
< |
threadUnexpectedException(); |
821 |
< |
} |
822 |
< |
} |
823 |
< |
}); |
824 |
< |
|
825 |
< |
Thread t2 = new Thread(new Runnable() { |
826 |
< |
public void run() { |
827 |
< |
try { |
828 |
< |
lock.lock(); |
829 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
830 |
< |
c.await(); |
831 |
< |
lock.unlock(); |
832 |
< |
} |
833 |
< |
catch (InterruptedException e) { |
834 |
< |
threadUnexpectedException(); |
835 |
< |
} |
836 |
< |
} |
837 |
< |
}); |
653 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
654 |
> |
public void realRun() throws InterruptedException { |
655 |
> |
lock.lock(); |
656 |
> |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
657 |
> |
c.await(); |
658 |
> |
lock.unlock(); |
659 |
> |
}}); |
660 |
|
|
661 |
< |
try { |
662 |
< |
lock.lock(); |
663 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
664 |
< |
lock.unlock(); |
665 |
< |
t1.start(); |
666 |
< |
Thread.sleep(SHORT_DELAY_MS); |
667 |
< |
t2.start(); |
668 |
< |
Thread.sleep(SHORT_DELAY_MS); |
669 |
< |
lock.lock(); |
670 |
< |
assertTrue(lock.hasWaiters(c)); |
671 |
< |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
672 |
< |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
673 |
< |
c.signalAll(); |
674 |
< |
lock.unlock(); |
675 |
< |
Thread.sleep(SHORT_DELAY_MS); |
676 |
< |
lock.lock(); |
677 |
< |
assertFalse(lock.hasWaiters(c)); |
678 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
679 |
< |
lock.unlock(); |
680 |
< |
t1.join(SHORT_DELAY_MS); |
681 |
< |
t2.join(SHORT_DELAY_MS); |
682 |
< |
assertFalse(t1.isAlive()); |
683 |
< |
assertFalse(t2.isAlive()); |
684 |
< |
} |
685 |
< |
catch (Exception ex) { |
686 |
< |
unexpectedException(); |
687 |
< |
} |
661 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
662 |
> |
public void realRun() throws InterruptedException { |
663 |
> |
lock.lock(); |
664 |
> |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
665 |
> |
c.await(); |
666 |
> |
lock.unlock(); |
667 |
> |
}}); |
668 |
> |
|
669 |
> |
lock.lock(); |
670 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
671 |
> |
lock.unlock(); |
672 |
> |
t1.start(); |
673 |
> |
Thread.sleep(SHORT_DELAY_MS); |
674 |
> |
t2.start(); |
675 |
> |
Thread.sleep(SHORT_DELAY_MS); |
676 |
> |
lock.lock(); |
677 |
> |
assertTrue(lock.hasWaiters(c)); |
678 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
679 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
680 |
> |
c.signalAll(); |
681 |
> |
lock.unlock(); |
682 |
> |
Thread.sleep(SHORT_DELAY_MS); |
683 |
> |
lock.lock(); |
684 |
> |
assertFalse(lock.hasWaiters(c)); |
685 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
686 |
> |
lock.unlock(); |
687 |
> |
t1.join(SHORT_DELAY_MS); |
688 |
> |
t2.join(SHORT_DELAY_MS); |
689 |
> |
assertFalse(t1.isAlive()); |
690 |
> |
assertFalse(t2.isAlive()); |
691 |
|
} |
692 |
|
|
693 |
|
/** A helper class for uninterruptible wait tests */ |
694 |
< |
class UninterruptableThread extends Thread { |
694 |
> |
class UninterruptibleThread extends Thread { |
695 |
|
private ReentrantLock lock; |
696 |
|
private Condition c; |
697 |
|
|
699 |
|
public volatile boolean interrupted = false; |
700 |
|
public volatile boolean lockStarted = false; |
701 |
|
|
702 |
< |
public UninterruptableThread(ReentrantLock lock, Condition c) { |
702 |
> |
public UninterruptibleThread(ReentrantLock lock, Condition c) { |
703 |
|
this.lock = lock; |
704 |
|
this.c = c; |
705 |
|
} |
720 |
|
/** |
721 |
|
* awaitUninterruptibly doesn't abort on interrupt |
722 |
|
*/ |
723 |
< |
public void testAwaitUninterruptibly() { |
723 |
> |
public void testAwaitUninterruptibly() throws InterruptedException { |
724 |
|
final ReentrantLock lock = new ReentrantLock(); |
725 |
|
final Condition c = lock.newCondition(); |
726 |
< |
UninterruptableThread thread = new UninterruptableThread(lock, c); |
902 |
< |
|
903 |
< |
try { |
904 |
< |
thread.start(); |
726 |
> |
UninterruptibleThread thread = new UninterruptibleThread(lock, c); |
727 |
|
|
728 |
< |
while (!thread.lockStarted) { |
907 |
< |
Thread.sleep(100); |
908 |
< |
} |
728 |
> |
thread.start(); |
729 |
|
|
730 |
< |
lock.lock(); |
731 |
< |
try { |
732 |
< |
thread.interrupt(); |
913 |
< |
thread.canAwake = true; |
914 |
< |
c.signal(); |
915 |
< |
} finally { |
916 |
< |
lock.unlock(); |
917 |
< |
} |
730 |
> |
while (!thread.lockStarted) { |
731 |
> |
Thread.sleep(100); |
732 |
> |
} |
733 |
|
|
734 |
< |
thread.join(); |
735 |
< |
assertTrue(thread.interrupted); |
736 |
< |
assertFalse(thread.isAlive()); |
737 |
< |
} catch (Exception ex) { |
738 |
< |
unexpectedException(); |
734 |
> |
lock.lock(); |
735 |
> |
try { |
736 |
> |
thread.interrupt(); |
737 |
> |
thread.canAwake = true; |
738 |
> |
c.signal(); |
739 |
> |
} finally { |
740 |
> |
lock.unlock(); |
741 |
|
} |
742 |
+ |
|
743 |
+ |
thread.join(); |
744 |
+ |
assertTrue(thread.interrupted); |
745 |
+ |
assertFalse(thread.isAlive()); |
746 |
|
} |
747 |
|
|
748 |
|
/** |
749 |
|
* await is interruptible |
750 |
|
*/ |
751 |
< |
public void testAwait_Interrupt() { |
751 |
> |
public void testAwait_Interrupt() throws InterruptedException { |
752 |
|
final ReentrantLock lock = new ReentrantLock(); |
753 |
|
final Condition c = lock.newCondition(); |
754 |
< |
Thread t = new Thread(new Runnable() { |
755 |
< |
public void run() { |
756 |
< |
try { |
757 |
< |
lock.lock(); |
758 |
< |
c.await(); |
759 |
< |
lock.unlock(); |
760 |
< |
threadShouldThrow(); |
761 |
< |
} |
762 |
< |
catch (InterruptedException success) { |
763 |
< |
} |
764 |
< |
} |
944 |
< |
}); |
945 |
< |
|
946 |
< |
try { |
947 |
< |
t.start(); |
948 |
< |
Thread.sleep(SHORT_DELAY_MS); |
949 |
< |
t.interrupt(); |
950 |
< |
t.join(SHORT_DELAY_MS); |
951 |
< |
assertFalse(t.isAlive()); |
952 |
< |
} |
953 |
< |
catch (Exception ex) { |
954 |
< |
unexpectedException(); |
955 |
< |
} |
754 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
755 |
> |
public void realRun() throws InterruptedException { |
756 |
> |
lock.lock(); |
757 |
> |
c.await(); |
758 |
> |
}}); |
759 |
> |
|
760 |
> |
t.start(); |
761 |
> |
Thread.sleep(SHORT_DELAY_MS); |
762 |
> |
t.interrupt(); |
763 |
> |
t.join(SHORT_DELAY_MS); |
764 |
> |
assertFalse(t.isAlive()); |
765 |
|
} |
766 |
|
|
767 |
|
/** |
768 |
|
* awaitNanos is interruptible |
769 |
|
*/ |
770 |
< |
public void testAwaitNanos_Interrupt() { |
770 |
> |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
771 |
|
final ReentrantLock lock = new ReentrantLock(); |
772 |
|
final Condition c = lock.newCondition(); |
773 |
< |
Thread t = new Thread(new Runnable() { |
774 |
< |
public void run() { |
775 |
< |
try { |
776 |
< |
lock.lock(); |
777 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
778 |
< |
lock.unlock(); |
779 |
< |
threadShouldThrow(); |
780 |
< |
} |
781 |
< |
catch (InterruptedException success) { |
782 |
< |
} |
783 |
< |
} |
975 |
< |
}); |
976 |
< |
|
977 |
< |
try { |
978 |
< |
t.start(); |
979 |
< |
Thread.sleep(SHORT_DELAY_MS); |
980 |
< |
t.interrupt(); |
981 |
< |
t.join(SHORT_DELAY_MS); |
982 |
< |
assertFalse(t.isAlive()); |
983 |
< |
} |
984 |
< |
catch (Exception ex) { |
985 |
< |
unexpectedException(); |
986 |
< |
} |
773 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
774 |
> |
public void realRun() throws InterruptedException { |
775 |
> |
lock.lock(); |
776 |
> |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
777 |
> |
}}); |
778 |
> |
|
779 |
> |
t.start(); |
780 |
> |
Thread.sleep(SHORT_DELAY_MS); |
781 |
> |
t.interrupt(); |
782 |
> |
t.join(SHORT_DELAY_MS); |
783 |
> |
assertFalse(t.isAlive()); |
784 |
|
} |
785 |
|
|
786 |
|
/** |
787 |
|
* awaitUntil is interruptible |
788 |
|
*/ |
789 |
< |
public void testAwaitUntil_Interrupt() { |
789 |
> |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
790 |
|
final ReentrantLock lock = new ReentrantLock(); |
791 |
|
final Condition c = lock.newCondition(); |
792 |
< |
Thread t = new Thread(new Runnable() { |
793 |
< |
public void run() { |
794 |
< |
try { |
795 |
< |
lock.lock(); |
796 |
< |
java.util.Date d = new java.util.Date(); |
797 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
798 |
< |
lock.unlock(); |
799 |
< |
threadShouldThrow(); |
800 |
< |
} |
801 |
< |
catch (InterruptedException success) { |
802 |
< |
} |
803 |
< |
} |
1007 |
< |
}); |
1008 |
< |
|
1009 |
< |
try { |
1010 |
< |
t.start(); |
1011 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1012 |
< |
t.interrupt(); |
1013 |
< |
t.join(SHORT_DELAY_MS); |
1014 |
< |
assertFalse(t.isAlive()); |
1015 |
< |
} |
1016 |
< |
catch (Exception ex) { |
1017 |
< |
unexpectedException(); |
1018 |
< |
} |
792 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
793 |
> |
public void realRun() throws InterruptedException { |
794 |
> |
lock.lock(); |
795 |
> |
java.util.Date d = new java.util.Date(); |
796 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
797 |
> |
}}); |
798 |
> |
|
799 |
> |
t.start(); |
800 |
> |
Thread.sleep(SHORT_DELAY_MS); |
801 |
> |
t.interrupt(); |
802 |
> |
t.join(SHORT_DELAY_MS); |
803 |
> |
assertFalse(t.isAlive()); |
804 |
|
} |
805 |
|
|
806 |
|
/** |
807 |
|
* signalAll wakes up all threads |
808 |
|
*/ |
809 |
< |
public void testSignalAll() { |
809 |
> |
public void testSignalAll() throws InterruptedException { |
810 |
|
final ReentrantLock lock = new ReentrantLock(); |
811 |
|
final Condition c = lock.newCondition(); |
812 |
< |
Thread t1 = new Thread(new Runnable() { |
813 |
< |
public void run() { |
814 |
< |
try { |
815 |
< |
lock.lock(); |
816 |
< |
c.await(); |
817 |
< |
lock.unlock(); |
1033 |
< |
} |
1034 |
< |
catch (InterruptedException e) { |
1035 |
< |
threadUnexpectedException(); |
1036 |
< |
} |
1037 |
< |
} |
1038 |
< |
}); |
1039 |
< |
|
1040 |
< |
Thread t2 = new Thread(new Runnable() { |
1041 |
< |
public void run() { |
1042 |
< |
try { |
1043 |
< |
lock.lock(); |
1044 |
< |
c.await(); |
1045 |
< |
lock.unlock(); |
1046 |
< |
} |
1047 |
< |
catch (InterruptedException e) { |
1048 |
< |
threadUnexpectedException(); |
1049 |
< |
} |
1050 |
< |
} |
1051 |
< |
}); |
812 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
813 |
> |
public void realRun() throws InterruptedException { |
814 |
> |
lock.lock(); |
815 |
> |
c.await(); |
816 |
> |
lock.unlock(); |
817 |
> |
}}); |
818 |
|
|
819 |
< |
try { |
820 |
< |
t1.start(); |
821 |
< |
t2.start(); |
822 |
< |
Thread.sleep(SHORT_DELAY_MS); |
823 |
< |
lock.lock(); |
824 |
< |
c.signalAll(); |
825 |
< |
lock.unlock(); |
826 |
< |
t1.join(SHORT_DELAY_MS); |
827 |
< |
t2.join(SHORT_DELAY_MS); |
828 |
< |
assertFalse(t1.isAlive()); |
829 |
< |
assertFalse(t2.isAlive()); |
830 |
< |
} |
831 |
< |
catch (Exception ex) { |
832 |
< |
unexpectedException(); |
833 |
< |
} |
819 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
820 |
> |
public void realRun() throws InterruptedException { |
821 |
> |
lock.lock(); |
822 |
> |
c.await(); |
823 |
> |
lock.unlock(); |
824 |
> |
}}); |
825 |
> |
|
826 |
> |
t1.start(); |
827 |
> |
t2.start(); |
828 |
> |
Thread.sleep(SHORT_DELAY_MS); |
829 |
> |
lock.lock(); |
830 |
> |
c.signalAll(); |
831 |
> |
lock.unlock(); |
832 |
> |
t1.join(SHORT_DELAY_MS); |
833 |
> |
t2.join(SHORT_DELAY_MS); |
834 |
> |
assertFalse(t1.isAlive()); |
835 |
> |
assertFalse(t2.isAlive()); |
836 |
|
} |
837 |
|
|
838 |
|
/** |
839 |
|
* await after multiple reentrant locking preserves lock count |
840 |
|
*/ |
841 |
< |
public void testAwaitLockCount() { |
841 |
> |
public void testAwaitLockCount() throws InterruptedException { |
842 |
|
final ReentrantLock lock = new ReentrantLock(); |
843 |
|
final Condition c = lock.newCondition(); |
844 |
< |
Thread t1 = new Thread(new Runnable() { |
845 |
< |
public void run() { |
846 |
< |
try { |
847 |
< |
lock.lock(); |
848 |
< |
threadAssertEquals(1, lock.getHoldCount()); |
849 |
< |
c.await(); |
850 |
< |
threadAssertEquals(1, lock.getHoldCount()); |
851 |
< |
lock.unlock(); |
1084 |
< |
} |
1085 |
< |
catch (InterruptedException e) { |
1086 |
< |
threadUnexpectedException(); |
1087 |
< |
} |
1088 |
< |
} |
1089 |
< |
}); |
1090 |
< |
|
1091 |
< |
Thread t2 = new Thread(new Runnable() { |
1092 |
< |
public void run() { |
1093 |
< |
try { |
1094 |
< |
lock.lock(); |
1095 |
< |
lock.lock(); |
1096 |
< |
threadAssertEquals(2, lock.getHoldCount()); |
1097 |
< |
c.await(); |
1098 |
< |
threadAssertEquals(2, lock.getHoldCount()); |
1099 |
< |
lock.unlock(); |
1100 |
< |
lock.unlock(); |
1101 |
< |
} |
1102 |
< |
catch (InterruptedException e) { |
1103 |
< |
threadUnexpectedException(); |
1104 |
< |
} |
1105 |
< |
} |
1106 |
< |
}); |
844 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
845 |
> |
public void realRun() throws InterruptedException { |
846 |
> |
lock.lock(); |
847 |
> |
threadAssertEquals(1, lock.getHoldCount()); |
848 |
> |
c.await(); |
849 |
> |
threadAssertEquals(1, lock.getHoldCount()); |
850 |
> |
lock.unlock(); |
851 |
> |
}}); |
852 |
|
|
853 |
< |
try { |
854 |
< |
t1.start(); |
855 |
< |
t2.start(); |
856 |
< |
Thread.sleep(SHORT_DELAY_MS); |
857 |
< |
lock.lock(); |
858 |
< |
c.signalAll(); |
859 |
< |
lock.unlock(); |
860 |
< |
t1.join(SHORT_DELAY_MS); |
861 |
< |
t2.join(SHORT_DELAY_MS); |
862 |
< |
assertFalse(t1.isAlive()); |
863 |
< |
assertFalse(t2.isAlive()); |
864 |
< |
} |
865 |
< |
catch (Exception ex) { |
866 |
< |
unexpectedException(); |
867 |
< |
} |
853 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
854 |
> |
public void realRun() throws InterruptedException { |
855 |
> |
lock.lock(); |
856 |
> |
lock.lock(); |
857 |
> |
threadAssertEquals(2, lock.getHoldCount()); |
858 |
> |
c.await(); |
859 |
> |
threadAssertEquals(2, lock.getHoldCount()); |
860 |
> |
lock.unlock(); |
861 |
> |
lock.unlock(); |
862 |
> |
}}); |
863 |
> |
|
864 |
> |
t1.start(); |
865 |
> |
t2.start(); |
866 |
> |
Thread.sleep(SHORT_DELAY_MS); |
867 |
> |
lock.lock(); |
868 |
> |
c.signalAll(); |
869 |
> |
lock.unlock(); |
870 |
> |
t1.join(SHORT_DELAY_MS); |
871 |
> |
t2.join(SHORT_DELAY_MS); |
872 |
> |
assertFalse(t1.isAlive()); |
873 |
> |
assertFalse(t2.isAlive()); |
874 |
|
} |
875 |
|
|
876 |
|
/** |
877 |
|
* A serialized lock deserializes as unlocked |
878 |
|
*/ |
879 |
< |
public void testSerialization() { |
879 |
> |
public void testSerialization() throws Exception { |
880 |
|
ReentrantLock l = new ReentrantLock(); |
881 |
|
l.lock(); |
882 |
|
l.unlock(); |
883 |
|
|
884 |
< |
try { |
885 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
886 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
887 |
< |
out.writeObject(l); |
888 |
< |
out.close(); |
889 |
< |
|
890 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
891 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
892 |
< |
ReentrantLock r = (ReentrantLock) in.readObject(); |
893 |
< |
r.lock(); |
894 |
< |
r.unlock(); |
895 |
< |
} catch (Exception e) { |
896 |
< |
e.printStackTrace(); |
1146 |
< |
unexpectedException(); |
1147 |
< |
} |
884 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
885 |
> |
ObjectOutputStream out = |
886 |
> |
new ObjectOutputStream(new BufferedOutputStream(bout)); |
887 |
> |
out.writeObject(l); |
888 |
> |
out.close(); |
889 |
> |
|
890 |
> |
ByteArrayInputStream bin = |
891 |
> |
new ByteArrayInputStream(bout.toByteArray()); |
892 |
> |
ObjectInputStream in = |
893 |
> |
new ObjectInputStream(new BufferedInputStream(bin)); |
894 |
> |
ReentrantLock r = (ReentrantLock) in.readObject(); |
895 |
> |
r.lock(); |
896 |
> |
r.unlock(); |
897 |
|
} |
898 |
|
|
899 |
|
/** |