1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
|
|
* public domain. Use, modify, and redistribute this code in any way |
4 |
|
|
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
|
|
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import junit.framework.*; |
9 |
|
|
import java.util.concurrent.locks.*; |
10 |
|
|
import java.util.concurrent.*; |
11 |
|
|
|
12 |
|
|
public class ReentrantLockTest extends TestCase { |
13 |
|
|
static int HOLD_COUNT_TEST_LIMIT = 20; |
14 |
|
|
|
15 |
|
|
public static void main(String[] args) { |
16 |
|
|
junit.textui.TestRunner.run (suite()); |
17 |
|
|
} |
18 |
|
|
|
19 |
|
|
public static Test suite() { |
20 |
|
|
return new TestSuite(ReentrantLockTest.class); |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
private static long SHORT_DELAY_MS = 100; |
24 |
|
|
private static long MEDIUM_DELAY_MS = 1000; |
25 |
|
|
private static long LONG_DELAY_MS = 10000; |
26 |
|
|
|
27 |
|
|
/* |
28 |
|
|
* Unlocks an unlocked lock, throws Illegal Monitor State |
29 |
|
|
* |
30 |
|
|
*/ |
31 |
|
|
|
32 |
|
|
public void testIllegalMonitorStateException(){ |
33 |
|
|
ReentrantLock rl = new ReentrantLock(); |
34 |
|
|
try{ |
35 |
|
|
rl.unlock(); |
36 |
|
|
fail("Should of thown Illegal Monitor State Exception"); |
37 |
|
|
|
38 |
|
|
}catch(IllegalMonitorStateException sucess){} |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
/* |
44 |
|
|
* makes a lock, locks it, tries to aquire the lock in another thread |
45 |
|
|
* interrupts that thread and waits for an interrupted Exception to |
46 |
|
|
* be thrown. |
47 |
|
|
*/ |
48 |
|
|
|
49 |
|
|
public void testInterruptedException(){ |
50 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
51 |
|
|
lock.lock(); |
52 |
|
|
Thread t = new Thread(new Runnable() { |
53 |
|
|
public void run(){ |
54 |
|
|
try{ |
55 |
|
|
lock.lockInterruptibly(); |
56 |
|
|
fail("should throw"); |
57 |
|
|
}catch(InterruptedException sucess){} |
58 |
|
|
} |
59 |
|
|
}); |
60 |
|
|
t.start(); |
61 |
|
|
t.interrupt(); |
62 |
|
|
lock.unlock(); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
/* |
66 |
|
|
* tests for interrupted exception on a timed wait |
67 |
|
|
* |
68 |
|
|
*/ |
69 |
|
|
|
70 |
|
|
|
71 |
|
|
public void testInterruptedException2(){ |
72 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
73 |
|
|
lock.lock(); |
74 |
|
|
Thread t = new Thread(new Runnable() { |
75 |
|
|
public void run(){ |
76 |
|
|
try{ |
77 |
|
|
lock.tryLock(1000,TimeUnit.MILLISECONDS); |
78 |
|
|
fail("should throw"); |
79 |
|
|
}catch(InterruptedException sucess){} |
80 |
|
|
} |
81 |
|
|
}); |
82 |
|
|
t.start(); |
83 |
|
|
t.interrupt(); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
|
87 |
|
|
public void testGetHoldCount() { |
88 |
|
|
ReentrantLock lock = new ReentrantLock(); |
89 |
|
|
for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) { |
90 |
|
|
lock.lock(); |
91 |
|
|
assertEquals(i,lock.getHoldCount()); |
92 |
|
|
} |
93 |
|
|
for(int i = ReentrantLockTest.HOLD_COUNT_TEST_LIMIT; i > 0; i--) { |
94 |
|
|
lock.unlock(); |
95 |
|
|
assertEquals(i-1,lock.getHoldCount()); |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
public void testIsLocked() { |
103 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
104 |
|
|
lock.lock(); |
105 |
|
|
assertTrue(lock.isLocked()); |
106 |
|
|
lock.unlock(); |
107 |
|
|
assertFalse(lock.isLocked()); |
108 |
|
|
Thread t = new Thread(new Runnable() { |
109 |
|
|
public void run() { |
110 |
|
|
lock.lock(); |
111 |
|
|
try { |
112 |
|
|
Thread.sleep(SHORT_DELAY_MS * 2); |
113 |
|
|
} |
114 |
|
|
catch(Exception e) {} |
115 |
|
|
lock.unlock(); |
116 |
|
|
} |
117 |
|
|
}); |
118 |
|
|
try{ |
119 |
|
|
t.start(); |
120 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
121 |
|
|
assertTrue(lock.isLocked()); |
122 |
|
|
t.join(); |
123 |
|
|
assertFalse(lock.isLocked()); |
124 |
|
|
} catch(Exception e){ |
125 |
|
|
fail("unexpected exception"); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/* |
131 |
|
|
* current thread locks interruptibly the thread |
132 |
|
|
* another thread tries to aquire the lock and blocks |
133 |
|
|
* on the call. interrupt the attempted aquireLock |
134 |
|
|
* assert that the first lock() call actually locked the lock |
135 |
|
|
* assert that the current thread is the one holding the lock |
136 |
|
|
*/ |
137 |
|
|
|
138 |
|
|
public void testLockedInterruptibly() { |
139 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
140 |
|
|
try {lock.lockInterruptibly();} catch(Exception e) {} |
141 |
|
|
Thread t = new Thread(new Runnable() { |
142 |
|
|
public void run() { |
143 |
|
|
try { |
144 |
|
|
lock.lockInterruptibly(); |
145 |
|
|
fail("Failed to generate an Interrupted Exception"); |
146 |
|
|
} |
147 |
|
|
catch(InterruptedException e) {} |
148 |
|
|
} |
149 |
|
|
}); |
150 |
|
|
t.start(); |
151 |
|
|
t.interrupt(); |
152 |
|
|
assertTrue(lock.isLocked()); |
153 |
|
|
assertTrue(lock.isHeldByCurrentThread()); |
154 |
|
|
} |
155 |
dl |
1.2 |
|
156 |
|
|
public void testAwait_IllegalMonitor() { |
157 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
158 |
|
|
final Condition c = lock.newCondition(); |
159 |
|
|
try { |
160 |
|
|
c.await(); |
161 |
|
|
fail("should throw"); |
162 |
|
|
} |
163 |
|
|
catch (IllegalMonitorStateException success) { |
164 |
|
|
} |
165 |
|
|
catch (Exception ex) { |
166 |
|
|
fail("should throw IMSE"); |
167 |
|
|
} |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
public void testSignal_IllegalMonitor() { |
171 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
172 |
|
|
final Condition c = lock.newCondition(); |
173 |
|
|
try { |
174 |
|
|
c.signal(); |
175 |
|
|
fail("should throw"); |
176 |
|
|
} |
177 |
|
|
catch (IllegalMonitorStateException success) { |
178 |
|
|
} |
179 |
|
|
catch (Exception ex) { |
180 |
|
|
fail("should throw IMSE"); |
181 |
|
|
} |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
public void testAwaitNanos_Timeout() { |
185 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
186 |
|
|
final Condition c = lock.newCondition(); |
187 |
|
|
try { |
188 |
|
|
lock.lock(); |
189 |
|
|
long t = c.awaitNanos(100); |
190 |
|
|
assertTrue(t <= 0); |
191 |
|
|
lock.unlock(); |
192 |
|
|
} |
193 |
|
|
catch (Exception ex) { |
194 |
|
|
fail("unexpected exception"); |
195 |
|
|
} |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
public void testAwait_Timeout() { |
199 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
200 |
|
|
final Condition c = lock.newCondition(); |
201 |
|
|
try { |
202 |
|
|
lock.lock(); |
203 |
|
|
assertFalse(c.await(10, TimeUnit.MILLISECONDS)); |
204 |
|
|
lock.unlock(); |
205 |
|
|
} |
206 |
|
|
catch (Exception ex) { |
207 |
|
|
fail("unexpected exception"); |
208 |
|
|
} |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
public void testAwaitUntil_Timeout() { |
212 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
213 |
|
|
final Condition c = lock.newCondition(); |
214 |
|
|
try { |
215 |
|
|
lock.lock(); |
216 |
|
|
java.util.Date d = new java.util.Date(); |
217 |
|
|
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
218 |
|
|
lock.unlock(); |
219 |
|
|
} |
220 |
|
|
catch (Exception ex) { |
221 |
|
|
fail("unexpected exception"); |
222 |
|
|
} |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
public void testAwait() { |
226 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
227 |
|
|
final Condition c = lock.newCondition(); |
228 |
|
|
Thread t = new Thread(new Runnable() { |
229 |
|
|
public void run() { |
230 |
|
|
try { |
231 |
|
|
lock.lock(); |
232 |
|
|
c.await(); |
233 |
|
|
lock.unlock(); |
234 |
|
|
} |
235 |
|
|
catch(InterruptedException e) { |
236 |
|
|
fail("unexpected exception"); |
237 |
|
|
} |
238 |
|
|
} |
239 |
|
|
}); |
240 |
|
|
|
241 |
|
|
try { |
242 |
|
|
t.start(); |
243 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
244 |
|
|
lock.lock(); |
245 |
|
|
c.signal(); |
246 |
|
|
lock.unlock(); |
247 |
|
|
t.join(SHORT_DELAY_MS); |
248 |
|
|
assertFalse(t.isAlive()); |
249 |
|
|
} |
250 |
|
|
catch (Exception ex) { |
251 |
|
|
fail("unexpected exception"); |
252 |
|
|
} |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
public void testAwaitUninterruptibly() { |
256 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
257 |
|
|
final Condition c = lock.newCondition(); |
258 |
|
|
Thread t = new Thread(new Runnable() { |
259 |
|
|
public void run() { |
260 |
|
|
lock.lock(); |
261 |
|
|
c.awaitUninterruptibly(); |
262 |
|
|
lock.unlock(); |
263 |
|
|
} |
264 |
|
|
}); |
265 |
|
|
|
266 |
|
|
try { |
267 |
|
|
t.start(); |
268 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
269 |
|
|
t.interrupt(); |
270 |
|
|
lock.lock(); |
271 |
|
|
c.signal(); |
272 |
|
|
lock.unlock(); |
273 |
|
|
t.join(SHORT_DELAY_MS); |
274 |
|
|
assertFalse(t.isAlive()); |
275 |
|
|
} |
276 |
|
|
catch (Exception ex) { |
277 |
|
|
fail("unexpected exception"); |
278 |
|
|
} |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
public void testAwait_Interrupt() { |
282 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
283 |
|
|
final Condition c = lock.newCondition(); |
284 |
|
|
Thread t = new Thread(new Runnable() { |
285 |
|
|
public void run() { |
286 |
|
|
try { |
287 |
|
|
lock.lock(); |
288 |
|
|
c.await(); |
289 |
|
|
lock.unlock(); |
290 |
|
|
fail("should throw"); |
291 |
|
|
} |
292 |
|
|
catch(InterruptedException success) { |
293 |
|
|
} |
294 |
|
|
} |
295 |
|
|
}); |
296 |
|
|
|
297 |
|
|
try { |
298 |
|
|
t.start(); |
299 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
300 |
|
|
t.interrupt(); |
301 |
|
|
t.join(SHORT_DELAY_MS); |
302 |
|
|
assertFalse(t.isAlive()); |
303 |
|
|
} |
304 |
|
|
catch (Exception ex) { |
305 |
|
|
fail("unexpected exception"); |
306 |
|
|
} |
307 |
|
|
} |
308 |
|
|
|
309 |
|
|
public void testAwaitNanos_Interrupt() { |
310 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
311 |
|
|
final Condition c = lock.newCondition(); |
312 |
|
|
Thread t = new Thread(new Runnable() { |
313 |
|
|
public void run() { |
314 |
|
|
try { |
315 |
|
|
lock.lock(); |
316 |
|
|
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
317 |
|
|
lock.unlock(); |
318 |
|
|
fail("should throw"); |
319 |
|
|
} |
320 |
|
|
catch(InterruptedException success) { |
321 |
|
|
} |
322 |
|
|
} |
323 |
|
|
}); |
324 |
|
|
|
325 |
|
|
try { |
326 |
|
|
t.start(); |
327 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
328 |
|
|
t.interrupt(); |
329 |
|
|
t.join(SHORT_DELAY_MS); |
330 |
|
|
assertFalse(t.isAlive()); |
331 |
|
|
} |
332 |
|
|
catch (Exception ex) { |
333 |
|
|
fail("unexpected exception"); |
334 |
|
|
} |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
public void testAwaitUntil_Interrupt() { |
338 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
339 |
|
|
final Condition c = lock.newCondition(); |
340 |
|
|
Thread t = new Thread(new Runnable() { |
341 |
|
|
public void run() { |
342 |
|
|
try { |
343 |
|
|
lock.lock(); |
344 |
|
|
java.util.Date d = new java.util.Date(); |
345 |
|
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
346 |
|
|
lock.unlock(); |
347 |
|
|
fail("should throw"); |
348 |
|
|
} |
349 |
|
|
catch(InterruptedException success) { |
350 |
|
|
} |
351 |
|
|
} |
352 |
|
|
}); |
353 |
|
|
|
354 |
|
|
try { |
355 |
|
|
t.start(); |
356 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
357 |
|
|
t.interrupt(); |
358 |
|
|
t.join(SHORT_DELAY_MS); |
359 |
|
|
assertFalse(t.isAlive()); |
360 |
|
|
} |
361 |
|
|
catch (Exception ex) { |
362 |
|
|
fail("unexpected exception"); |
363 |
|
|
} |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
public void testSignalAll() { |
367 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
368 |
|
|
final Condition c = lock.newCondition(); |
369 |
|
|
Thread t1 = new Thread(new Runnable() { |
370 |
|
|
public void run() { |
371 |
|
|
try { |
372 |
|
|
lock.lock(); |
373 |
|
|
c.await(); |
374 |
|
|
lock.unlock(); |
375 |
|
|
} |
376 |
|
|
catch(InterruptedException e) { |
377 |
|
|
fail("unexpected exception"); |
378 |
|
|
} |
379 |
|
|
} |
380 |
|
|
}); |
381 |
|
|
|
382 |
|
|
Thread t2 = new Thread(new Runnable() { |
383 |
|
|
public void run() { |
384 |
|
|
try { |
385 |
|
|
lock.lock(); |
386 |
|
|
c.await(); |
387 |
|
|
lock.unlock(); |
388 |
|
|
} |
389 |
|
|
catch(InterruptedException e) { |
390 |
|
|
fail("unexpected exception"); |
391 |
|
|
} |
392 |
|
|
} |
393 |
|
|
}); |
394 |
|
|
|
395 |
|
|
try { |
396 |
|
|
t1.start(); |
397 |
|
|
t2.start(); |
398 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
399 |
|
|
lock.lock(); |
400 |
|
|
c.signalAll(); |
401 |
|
|
lock.unlock(); |
402 |
|
|
t1.join(SHORT_DELAY_MS); |
403 |
|
|
t2.join(SHORT_DELAY_MS); |
404 |
|
|
assertFalse(t1.isAlive()); |
405 |
|
|
assertFalse(t2.isAlive()); |
406 |
|
|
} |
407 |
|
|
catch (Exception ex) { |
408 |
|
|
fail("unexpected exception"); |
409 |
|
|
} |
410 |
|
|
} |
411 |
dl |
1.1 |
|
412 |
|
|
} |