1 |
dl |
1.1 |
/* |
2 |
dl |
1.12 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
jsr166 |
1.26 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.concurrent.locks.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
jsr166 |
1.31 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
dl |
1.5 |
import java.util.*; |
14 |
dl |
1.3 |
import java.io.*; |
15 |
dl |
1.1 |
|
16 |
dl |
1.4 |
public class ReentrantLockTest extends JSR166TestCase { |
17 |
dl |
1.1 |
public static void main(String[] args) { |
18 |
jsr166 |
1.36 |
junit.textui.TestRunner.run(suite()); |
19 |
dl |
1.1 |
} |
20 |
|
|
public static Test suite() { |
21 |
jsr166 |
1.30 |
return new TestSuite(ReentrantLockTest.class); |
22 |
dl |
1.1 |
} |
23 |
|
|
|
24 |
dl |
1.6 |
/** |
25 |
|
|
* A runnable calling lockInterruptibly |
26 |
|
|
*/ |
27 |
jsr166 |
1.29 |
class InterruptibleLockRunnable extends CheckedRunnable { |
28 |
dl |
1.5 |
final ReentrantLock lock; |
29 |
|
|
InterruptibleLockRunnable(ReentrantLock l) { lock = l; } |
30 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
31 |
|
|
lock.lockInterruptibly(); |
32 |
dl |
1.5 |
} |
33 |
|
|
} |
34 |
|
|
|
35 |
dl |
1.6 |
|
36 |
|
|
/** |
37 |
|
|
* A runnable calling lockInterruptibly that expects to be |
38 |
|
|
* interrupted |
39 |
|
|
*/ |
40 |
jsr166 |
1.29 |
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
41 |
dl |
1.5 |
final ReentrantLock lock; |
42 |
|
|
InterruptedLockRunnable(ReentrantLock l) { lock = l; } |
43 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
44 |
|
|
lock.lockInterruptibly(); |
45 |
dl |
1.5 |
} |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
/** |
49 |
dl |
1.6 |
* Subclass to expose protected methods |
50 |
dl |
1.5 |
*/ |
51 |
dl |
1.6 |
static class PublicReentrantLock extends ReentrantLock { |
52 |
|
|
PublicReentrantLock() { super(); } |
53 |
jsr166 |
1.26 |
public Collection<Thread> getQueuedThreads() { |
54 |
|
|
return super.getQueuedThreads(); |
55 |
dl |
1.5 |
} |
56 |
jsr166 |
1.26 |
public Collection<Thread> getWaitingThreads(Condition c) { |
57 |
|
|
return super.getWaitingThreads(c); |
58 |
dl |
1.13 |
} |
59 |
dl |
1.5 |
} |
60 |
|
|
|
61 |
dl |
1.8 |
/** |
62 |
dl |
1.9 |
* Constructor sets given fairness |
63 |
|
|
*/ |
64 |
jsr166 |
1.26 |
public void testConstructor() { |
65 |
jsr166 |
1.29 |
assertFalse(new ReentrantLock().isFair()); |
66 |
|
|
assertFalse(new ReentrantLock(false).isFair()); |
67 |
|
|
assertTrue(new ReentrantLock(true).isFair()); |
68 |
dl |
1.9 |
} |
69 |
|
|
|
70 |
|
|
/** |
71 |
dl |
1.6 |
* locking an unlocked lock succeeds |
72 |
|
|
*/ |
73 |
jsr166 |
1.26 |
public void testLock() { |
74 |
jsr166 |
1.30 |
ReentrantLock rl = new ReentrantLock(); |
75 |
dl |
1.6 |
rl.lock(); |
76 |
|
|
assertTrue(rl.isLocked()); |
77 |
|
|
rl.unlock(); |
78 |
jsr166 |
1.29 |
assertFalse(rl.isLocked()); |
79 |
dl |
1.6 |
} |
80 |
|
|
|
81 |
dl |
1.8 |
/** |
82 |
dl |
1.6 |
* locking an unlocked fair lock succeeds |
83 |
|
|
*/ |
84 |
jsr166 |
1.26 |
public void testFairLock() { |
85 |
jsr166 |
1.30 |
ReentrantLock rl = new ReentrantLock(true); |
86 |
dl |
1.6 |
rl.lock(); |
87 |
|
|
assertTrue(rl.isLocked()); |
88 |
|
|
rl.unlock(); |
89 |
|
|
} |
90 |
|
|
|
91 |
dl |
1.8 |
/** |
92 |
dl |
1.5 |
* Unlocking an unlocked lock throws IllegalMonitorStateException |
93 |
dl |
1.1 |
*/ |
94 |
jsr166 |
1.26 |
public void testUnlock_IllegalMonitorStateException() { |
95 |
jsr166 |
1.30 |
ReentrantLock rl = new ReentrantLock(); |
96 |
|
|
try { |
97 |
|
|
rl.unlock(); |
98 |
|
|
shouldThrow(); |
99 |
|
|
} catch (IllegalMonitorStateException success) {} |
100 |
dl |
1.5 |
} |
101 |
dl |
1.1 |
|
102 |
dl |
1.8 |
/** |
103 |
dl |
1.15 |
* tryLock on an unlocked lock succeeds |
104 |
dl |
1.1 |
*/ |
105 |
jsr166 |
1.26 |
public void testTryLock() { |
106 |
jsr166 |
1.30 |
ReentrantLock rl = new ReentrantLock(); |
107 |
dl |
1.6 |
assertTrue(rl.tryLock()); |
108 |
|
|
assertTrue(rl.isLocked()); |
109 |
|
|
rl.unlock(); |
110 |
|
|
} |
111 |
|
|
|
112 |
dl |
1.1 |
|
113 |
dl |
1.8 |
/** |
114 |
dl |
1.13 |
* hasQueuedThreads reports whether there are waiting threads |
115 |
|
|
*/ |
116 |
jsr166 |
1.29 |
public void testhasQueuedThreads() throws InterruptedException { |
117 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
118 |
dl |
1.13 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
119 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
120 |
jsr166 |
1.29 |
assertFalse(lock.hasQueuedThreads()); |
121 |
|
|
lock.lock(); |
122 |
|
|
t1.start(); |
123 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
124 |
|
|
assertTrue(lock.hasQueuedThreads()); |
125 |
|
|
t2.start(); |
126 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
127 |
|
|
assertTrue(lock.hasQueuedThreads()); |
128 |
|
|
t1.interrupt(); |
129 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
130 |
|
|
assertTrue(lock.hasQueuedThreads()); |
131 |
|
|
lock.unlock(); |
132 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
133 |
|
|
assertFalse(lock.hasQueuedThreads()); |
134 |
|
|
t1.join(); |
135 |
|
|
t2.join(); |
136 |
jsr166 |
1.26 |
} |
137 |
dl |
1.13 |
|
138 |
|
|
/** |
139 |
dl |
1.7 |
* getQueueLength reports number of waiting threads |
140 |
dl |
1.1 |
*/ |
141 |
jsr166 |
1.29 |
public void testGetQueueLength() throws InterruptedException { |
142 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
143 |
dl |
1.5 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
144 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
145 |
jsr166 |
1.29 |
assertEquals(0, lock.getQueueLength()); |
146 |
|
|
lock.lock(); |
147 |
|
|
t1.start(); |
148 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
149 |
|
|
assertEquals(1, lock.getQueueLength()); |
150 |
|
|
t2.start(); |
151 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
152 |
|
|
assertEquals(2, lock.getQueueLength()); |
153 |
|
|
t1.interrupt(); |
154 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
155 |
|
|
assertEquals(1, lock.getQueueLength()); |
156 |
|
|
lock.unlock(); |
157 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
158 |
|
|
assertEquals(0, lock.getQueueLength()); |
159 |
|
|
t1.join(); |
160 |
|
|
t2.join(); |
161 |
jsr166 |
1.26 |
} |
162 |
dl |
1.5 |
|
163 |
dl |
1.8 |
/** |
164 |
dl |
1.16 |
* getQueueLength reports number of waiting threads |
165 |
|
|
*/ |
166 |
jsr166 |
1.29 |
public void testGetQueueLength_fair() throws InterruptedException { |
167 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(true); |
168 |
dl |
1.16 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
169 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
170 |
jsr166 |
1.29 |
assertEquals(0, lock.getQueueLength()); |
171 |
|
|
lock.lock(); |
172 |
|
|
t1.start(); |
173 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
174 |
|
|
assertEquals(1, lock.getQueueLength()); |
175 |
|
|
t2.start(); |
176 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
177 |
|
|
assertEquals(2, lock.getQueueLength()); |
178 |
|
|
t1.interrupt(); |
179 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
180 |
|
|
assertEquals(1, lock.getQueueLength()); |
181 |
|
|
lock.unlock(); |
182 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
183 |
|
|
assertEquals(0, lock.getQueueLength()); |
184 |
|
|
t1.join(); |
185 |
|
|
t2.join(); |
186 |
jsr166 |
1.26 |
} |
187 |
dl |
1.16 |
|
188 |
|
|
/** |
189 |
dl |
1.19 |
* hasQueuedThread(null) throws NPE |
190 |
|
|
*/ |
191 |
jsr166 |
1.26 |
public void testHasQueuedThreadNPE() { |
192 |
jsr166 |
1.30 |
final ReentrantLock sync = new ReentrantLock(); |
193 |
dl |
1.19 |
try { |
194 |
|
|
sync.hasQueuedThread(null); |
195 |
|
|
shouldThrow(); |
196 |
jsr166 |
1.29 |
} catch (NullPointerException success) {} |
197 |
dl |
1.19 |
} |
198 |
|
|
|
199 |
|
|
/** |
200 |
|
|
* hasQueuedThread reports whether a thread is queued. |
201 |
|
|
*/ |
202 |
jsr166 |
1.29 |
public void testHasQueuedThread() throws InterruptedException { |
203 |
jsr166 |
1.30 |
final ReentrantLock sync = new ReentrantLock(); |
204 |
dl |
1.19 |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
205 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
206 |
jsr166 |
1.29 |
assertFalse(sync.hasQueuedThread(t1)); |
207 |
|
|
assertFalse(sync.hasQueuedThread(t2)); |
208 |
|
|
sync.lock(); |
209 |
|
|
t1.start(); |
210 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
211 |
|
|
assertTrue(sync.hasQueuedThread(t1)); |
212 |
|
|
t2.start(); |
213 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
214 |
|
|
assertTrue(sync.hasQueuedThread(t1)); |
215 |
|
|
assertTrue(sync.hasQueuedThread(t2)); |
216 |
|
|
t1.interrupt(); |
217 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
218 |
|
|
assertFalse(sync.hasQueuedThread(t1)); |
219 |
|
|
assertTrue(sync.hasQueuedThread(t2)); |
220 |
|
|
sync.unlock(); |
221 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
222 |
|
|
assertFalse(sync.hasQueuedThread(t1)); |
223 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
224 |
|
|
assertFalse(sync.hasQueuedThread(t2)); |
225 |
|
|
t1.join(); |
226 |
|
|
t2.join(); |
227 |
jsr166 |
1.26 |
} |
228 |
dl |
1.19 |
|
229 |
|
|
|
230 |
|
|
/** |
231 |
dl |
1.5 |
* getQueuedThreads includes waiting threads |
232 |
|
|
*/ |
233 |
jsr166 |
1.29 |
public void testGetQueuedThreads() throws InterruptedException { |
234 |
jsr166 |
1.30 |
final PublicReentrantLock lock = new PublicReentrantLock(); |
235 |
dl |
1.5 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
236 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
237 |
jsr166 |
1.29 |
assertTrue(lock.getQueuedThreads().isEmpty()); |
238 |
|
|
lock.lock(); |
239 |
|
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
240 |
|
|
t1.start(); |
241 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
242 |
|
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
243 |
|
|
t2.start(); |
244 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
245 |
|
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
246 |
|
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
247 |
|
|
t1.interrupt(); |
248 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
249 |
|
|
assertFalse(lock.getQueuedThreads().contains(t1)); |
250 |
|
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
251 |
|
|
lock.unlock(); |
252 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
253 |
|
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
254 |
|
|
t1.join(); |
255 |
|
|
t2.join(); |
256 |
jsr166 |
1.26 |
} |
257 |
dl |
1.5 |
|
258 |
dl |
1.1 |
|
259 |
dl |
1.8 |
/** |
260 |
dl |
1.15 |
* timed tryLock is interruptible. |
261 |
dl |
1.5 |
*/ |
262 |
jsr166 |
1.29 |
public void testInterruptedException2() throws InterruptedException { |
263 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
264 |
|
|
lock.lock(); |
265 |
|
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
266 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
267 |
jsr166 |
1.31 |
lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS); |
268 |
jsr166 |
1.29 |
}}); |
269 |
|
|
|
270 |
|
|
t.start(); |
271 |
jsr166 |
1.32 |
Thread.sleep(SHORT_DELAY_MS); |
272 |
jsr166 |
1.29 |
t.interrupt(); |
273 |
|
|
t.join(); |
274 |
dl |
1.1 |
} |
275 |
|
|
|
276 |
dl |
1.3 |
|
277 |
dl |
1.5 |
/** |
278 |
dl |
1.15 |
* TryLock on a locked lock fails |
279 |
dl |
1.5 |
*/ |
280 |
jsr166 |
1.29 |
public void testTryLockWhenLocked() throws InterruptedException { |
281 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
282 |
|
|
lock.lock(); |
283 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
284 |
jsr166 |
1.29 |
public void realRun() { |
285 |
jsr166 |
1.37 |
assertFalse(lock.tryLock()); |
286 |
jsr166 |
1.29 |
}}); |
287 |
|
|
|
288 |
|
|
t.start(); |
289 |
|
|
t.join(); |
290 |
|
|
lock.unlock(); |
291 |
jsr166 |
1.26 |
} |
292 |
dl |
1.3 |
|
293 |
dl |
1.5 |
/** |
294 |
dl |
1.15 |
* Timed tryLock on a locked lock times out |
295 |
dl |
1.5 |
*/ |
296 |
jsr166 |
1.29 |
public void testTryLock_Timeout() throws InterruptedException { |
297 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
298 |
|
|
lock.lock(); |
299 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
300 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
301 |
jsr166 |
1.37 |
assertFalse(lock.tryLock(1, MILLISECONDS)); |
302 |
jsr166 |
1.29 |
}}); |
303 |
|
|
|
304 |
|
|
t.start(); |
305 |
|
|
t.join(); |
306 |
|
|
lock.unlock(); |
307 |
jsr166 |
1.26 |
} |
308 |
|
|
|
309 |
dl |
1.5 |
/** |
310 |
|
|
* getHoldCount returns number of recursive holds |
311 |
|
|
*/ |
312 |
dl |
1.1 |
public void testGetHoldCount() { |
313 |
jsr166 |
1.30 |
ReentrantLock lock = new ReentrantLock(); |
314 |
|
|
for (int i = 1; i <= SIZE; i++) { |
315 |
|
|
lock.lock(); |
316 |
|
|
assertEquals(i, lock.getHoldCount()); |
317 |
|
|
} |
318 |
|
|
for (int i = SIZE; i > 0; i--) { |
319 |
|
|
lock.unlock(); |
320 |
|
|
assertEquals(i-1, lock.getHoldCount()); |
321 |
|
|
} |
322 |
dl |
1.1 |
} |
323 |
jsr166 |
1.26 |
|
324 |
|
|
|
325 |
dl |
1.5 |
/** |
326 |
|
|
* isLocked is true when locked and false when not |
327 |
|
|
*/ |
328 |
jsr166 |
1.29 |
public void testIsLocked() throws InterruptedException { |
329 |
jsr166 |
1.30 |
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 CheckedRunnable() { |
335 |
jsr166 |
1.29 |
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 |
dl |
1.1 |
} |
347 |
|
|
|
348 |
|
|
|
349 |
dl |
1.8 |
/** |
350 |
dl |
1.6 |
* lockInterruptibly is interruptible. |
351 |
|
|
*/ |
352 |
jsr166 |
1.29 |
public void testLockInterruptibly1() throws InterruptedException { |
353 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
354 |
|
|
lock.lock(); |
355 |
|
|
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
356 |
jsr166 |
1.29 |
t.start(); |
357 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
358 |
|
|
t.interrupt(); |
359 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
360 |
|
|
lock.unlock(); |
361 |
|
|
t.join(); |
362 |
jsr166 |
1.26 |
} |
363 |
dl |
1.6 |
|
364 |
dl |
1.5 |
/** |
365 |
|
|
* lockInterruptibly succeeds when unlocked, else is interruptible |
366 |
|
|
*/ |
367 |
jsr166 |
1.29 |
public void testLockInterruptibly2() throws InterruptedException { |
368 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
369 |
jsr166 |
1.29 |
lock.lockInterruptibly(); |
370 |
jsr166 |
1.30 |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
371 |
jsr166 |
1.29 |
t.start(); |
372 |
jsr166 |
1.32 |
Thread.sleep(SHORT_DELAY_MS); |
373 |
jsr166 |
1.29 |
t.interrupt(); |
374 |
|
|
assertTrue(lock.isLocked()); |
375 |
|
|
assertTrue(lock.isHeldByCurrentThread()); |
376 |
|
|
t.join(); |
377 |
dl |
1.1 |
} |
378 |
dl |
1.2 |
|
379 |
dl |
1.6 |
/** |
380 |
|
|
* Calling await without holding lock throws IllegalMonitorStateException |
381 |
|
|
*/ |
382 |
jsr166 |
1.29 |
public void testAwait_IllegalMonitor() throws InterruptedException { |
383 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
384 |
dl |
1.2 |
final Condition c = lock.newCondition(); |
385 |
|
|
try { |
386 |
|
|
c.await(); |
387 |
dl |
1.6 |
shouldThrow(); |
388 |
jsr166 |
1.29 |
} catch (IllegalMonitorStateException success) {} |
389 |
dl |
1.2 |
} |
390 |
|
|
|
391 |
dl |
1.6 |
/** |
392 |
|
|
* Calling signal without holding lock throws IllegalMonitorStateException |
393 |
|
|
*/ |
394 |
dl |
1.2 |
public void testSignal_IllegalMonitor() { |
395 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
396 |
dl |
1.2 |
final Condition c = lock.newCondition(); |
397 |
|
|
try { |
398 |
|
|
c.signal(); |
399 |
dl |
1.6 |
shouldThrow(); |
400 |
jsr166 |
1.29 |
} catch (IllegalMonitorStateException success) {} |
401 |
dl |
1.2 |
} |
402 |
|
|
|
403 |
dl |
1.6 |
/** |
404 |
|
|
* awaitNanos without a signal times out |
405 |
|
|
*/ |
406 |
jsr166 |
1.29 |
public void testAwaitNanos_Timeout() throws InterruptedException { |
407 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
408 |
dl |
1.2 |
final Condition c = lock.newCondition(); |
409 |
jsr166 |
1.29 |
lock.lock(); |
410 |
|
|
long t = c.awaitNanos(100); |
411 |
|
|
assertTrue(t <= 0); |
412 |
|
|
lock.unlock(); |
413 |
dl |
1.2 |
} |
414 |
|
|
|
415 |
dl |
1.6 |
/** |
416 |
jsr166 |
1.38 |
* timed await without a signal times out |
417 |
dl |
1.6 |
*/ |
418 |
jsr166 |
1.29 |
public void testAwait_Timeout() throws InterruptedException { |
419 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
420 |
dl |
1.2 |
final Condition c = lock.newCondition(); |
421 |
jsr166 |
1.29 |
lock.lock(); |
422 |
jsr166 |
1.31 |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
423 |
jsr166 |
1.29 |
lock.unlock(); |
424 |
dl |
1.2 |
} |
425 |
|
|
|
426 |
dl |
1.6 |
/** |
427 |
|
|
* awaitUntil without a signal times out |
428 |
|
|
*/ |
429 |
jsr166 |
1.29 |
public void testAwaitUntil_Timeout() throws InterruptedException { |
430 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
431 |
dl |
1.2 |
final Condition c = lock.newCondition(); |
432 |
jsr166 |
1.29 |
lock.lock(); |
433 |
|
|
java.util.Date d = new java.util.Date(); |
434 |
|
|
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
435 |
|
|
lock.unlock(); |
436 |
dl |
1.2 |
} |
437 |
|
|
|
438 |
dl |
1.6 |
/** |
439 |
|
|
* await returns when signalled |
440 |
|
|
*/ |
441 |
jsr166 |
1.29 |
public void testAwait() throws InterruptedException { |
442 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
443 |
dl |
1.13 |
final Condition c = lock.newCondition(); |
444 |
jsr166 |
1.30 |
Thread t = new Thread(new CheckedRunnable() { |
445 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
446 |
|
|
lock.lock(); |
447 |
|
|
c.await(); |
448 |
|
|
lock.unlock(); |
449 |
|
|
}}); |
450 |
dl |
1.5 |
|
451 |
jsr166 |
1.29 |
t.start(); |
452 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
453 |
|
|
lock.lock(); |
454 |
|
|
c.signal(); |
455 |
|
|
lock.unlock(); |
456 |
|
|
t.join(SHORT_DELAY_MS); |
457 |
|
|
assertFalse(t.isAlive()); |
458 |
dl |
1.5 |
} |
459 |
|
|
|
460 |
dl |
1.6 |
/** |
461 |
dl |
1.14 |
* hasWaiters throws NPE if null |
462 |
|
|
*/ |
463 |
|
|
public void testHasWaitersNPE() { |
464 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
465 |
dl |
1.14 |
try { |
466 |
|
|
lock.hasWaiters(null); |
467 |
|
|
shouldThrow(); |
468 |
jsr166 |
1.29 |
} catch (NullPointerException success) {} |
469 |
dl |
1.14 |
} |
470 |
|
|
|
471 |
|
|
/** |
472 |
|
|
* getWaitQueueLength throws NPE if null |
473 |
|
|
*/ |
474 |
|
|
public void testGetWaitQueueLengthNPE() { |
475 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
476 |
dl |
1.14 |
try { |
477 |
|
|
lock.getWaitQueueLength(null); |
478 |
|
|
shouldThrow(); |
479 |
jsr166 |
1.29 |
} catch (NullPointerException success) {} |
480 |
dl |
1.14 |
} |
481 |
|
|
|
482 |
|
|
|
483 |
|
|
/** |
484 |
|
|
* getWaitingThreads throws NPE if null |
485 |
|
|
*/ |
486 |
|
|
public void testGetWaitingThreadsNPE() { |
487 |
jsr166 |
1.30 |
final PublicReentrantLock lock = new PublicReentrantLock(); |
488 |
dl |
1.14 |
try { |
489 |
|
|
lock.getWaitingThreads(null); |
490 |
|
|
shouldThrow(); |
491 |
jsr166 |
1.29 |
} catch (NullPointerException success) {} |
492 |
dl |
1.14 |
} |
493 |
|
|
|
494 |
|
|
|
495 |
|
|
/** |
496 |
dl |
1.13 |
* hasWaiters throws IAE if not owned |
497 |
|
|
*/ |
498 |
|
|
public void testHasWaitersIAE() { |
499 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
500 |
jsr166 |
1.29 |
final Condition c = lock.newCondition(); |
501 |
jsr166 |
1.30 |
final ReentrantLock lock2 = new ReentrantLock(); |
502 |
dl |
1.13 |
try { |
503 |
|
|
lock2.hasWaiters(c); |
504 |
|
|
shouldThrow(); |
505 |
jsr166 |
1.29 |
} catch (IllegalArgumentException success) {} |
506 |
dl |
1.13 |
} |
507 |
|
|
|
508 |
|
|
/** |
509 |
|
|
* hasWaiters throws IMSE if not locked |
510 |
|
|
*/ |
511 |
|
|
public void testHasWaitersIMSE() { |
512 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
513 |
jsr166 |
1.29 |
final Condition c = lock.newCondition(); |
514 |
dl |
1.13 |
try { |
515 |
|
|
lock.hasWaiters(c); |
516 |
|
|
shouldThrow(); |
517 |
jsr166 |
1.29 |
} catch (IllegalMonitorStateException success) {} |
518 |
dl |
1.13 |
} |
519 |
|
|
|
520 |
|
|
|
521 |
|
|
/** |
522 |
|
|
* getWaitQueueLength throws IAE if not owned |
523 |
|
|
*/ |
524 |
|
|
public void testGetWaitQueueLengthIAE() { |
525 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
526 |
jsr166 |
1.29 |
final Condition c = lock.newCondition(); |
527 |
jsr166 |
1.30 |
final ReentrantLock lock2 = new ReentrantLock(); |
528 |
dl |
1.13 |
try { |
529 |
|
|
lock2.getWaitQueueLength(c); |
530 |
|
|
shouldThrow(); |
531 |
jsr166 |
1.29 |
} catch (IllegalArgumentException success) {} |
532 |
dl |
1.13 |
} |
533 |
|
|
|
534 |
|
|
/** |
535 |
|
|
* getWaitQueueLength throws IMSE if not locked |
536 |
|
|
*/ |
537 |
|
|
public void testGetWaitQueueLengthIMSE() { |
538 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
539 |
jsr166 |
1.29 |
final Condition c = lock.newCondition(); |
540 |
dl |
1.13 |
try { |
541 |
|
|
lock.getWaitQueueLength(c); |
542 |
|
|
shouldThrow(); |
543 |
jsr166 |
1.29 |
} catch (IllegalMonitorStateException success) {} |
544 |
dl |
1.13 |
} |
545 |
|
|
|
546 |
|
|
|
547 |
|
|
/** |
548 |
|
|
* getWaitingThreads throws IAE if not owned |
549 |
|
|
*/ |
550 |
|
|
public void testGetWaitingThreadsIAE() { |
551 |
jsr166 |
1.30 |
final PublicReentrantLock lock = new PublicReentrantLock(); |
552 |
jsr166 |
1.29 |
final Condition c = lock.newCondition(); |
553 |
jsr166 |
1.30 |
final PublicReentrantLock lock2 = new PublicReentrantLock(); |
554 |
dl |
1.13 |
try { |
555 |
|
|
lock2.getWaitingThreads(c); |
556 |
|
|
shouldThrow(); |
557 |
jsr166 |
1.29 |
} catch (IllegalArgumentException success) {} |
558 |
dl |
1.13 |
} |
559 |
|
|
|
560 |
|
|
/** |
561 |
|
|
* getWaitingThreads throws IMSE if not locked |
562 |
|
|
*/ |
563 |
|
|
public void testGetWaitingThreadsIMSE() { |
564 |
jsr166 |
1.30 |
final PublicReentrantLock lock = new PublicReentrantLock(); |
565 |
jsr166 |
1.29 |
final Condition c = lock.newCondition(); |
566 |
dl |
1.13 |
try { |
567 |
|
|
lock.getWaitingThreads(c); |
568 |
|
|
shouldThrow(); |
569 |
jsr166 |
1.29 |
} catch (IllegalMonitorStateException success) {} |
570 |
dl |
1.13 |
} |
571 |
|
|
|
572 |
|
|
|
573 |
|
|
/** |
574 |
dl |
1.6 |
* hasWaiters returns true when a thread is waiting, else false |
575 |
|
|
*/ |
576 |
jsr166 |
1.29 |
public void testHasWaiters() throws InterruptedException { |
577 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
578 |
dl |
1.13 |
final Condition c = lock.newCondition(); |
579 |
jsr166 |
1.30 |
Thread t = new Thread(new CheckedRunnable() { |
580 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
581 |
|
|
lock.lock(); |
582 |
jsr166 |
1.37 |
assertFalse(lock.hasWaiters(c)); |
583 |
|
|
assertEquals(0, lock.getWaitQueueLength(c)); |
584 |
jsr166 |
1.29 |
c.await(); |
585 |
|
|
lock.unlock(); |
586 |
|
|
}}); |
587 |
dl |
1.2 |
|
588 |
jsr166 |
1.29 |
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); |
601 |
|
|
assertFalse(t.isAlive()); |
602 |
dl |
1.5 |
} |
603 |
|
|
|
604 |
dl |
1.6 |
/** |
605 |
|
|
* getWaitQueueLength returns number of waiting threads |
606 |
|
|
*/ |
607 |
jsr166 |
1.29 |
public void testGetWaitQueueLength() throws InterruptedException { |
608 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
609 |
dl |
1.13 |
final Condition c = lock.newCondition(); |
610 |
jsr166 |
1.30 |
Thread t1 = new Thread(new CheckedRunnable() { |
611 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
612 |
|
|
lock.lock(); |
613 |
jsr166 |
1.37 |
assertFalse(lock.hasWaiters(c)); |
614 |
|
|
assertEquals(0, lock.getWaitQueueLength(c)); |
615 |
jsr166 |
1.29 |
c.await(); |
616 |
|
|
lock.unlock(); |
617 |
|
|
}}); |
618 |
dl |
1.13 |
|
619 |
jsr166 |
1.30 |
Thread t2 = new Thread(new CheckedRunnable() { |
620 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
621 |
|
|
lock.lock(); |
622 |
jsr166 |
1.37 |
assertTrue(lock.hasWaiters(c)); |
623 |
|
|
assertEquals(1, lock.getWaitQueueLength(c)); |
624 |
jsr166 |
1.29 |
c.await(); |
625 |
|
|
lock.unlock(); |
626 |
|
|
}}); |
627 |
|
|
|
628 |
|
|
t1.start(); |
629 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
630 |
|
|
t2.start(); |
631 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
632 |
|
|
lock.lock(); |
633 |
|
|
assertTrue(lock.hasWaiters(c)); |
634 |
|
|
assertEquals(2, lock.getWaitQueueLength(c)); |
635 |
|
|
c.signalAll(); |
636 |
|
|
lock.unlock(); |
637 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
638 |
|
|
lock.lock(); |
639 |
|
|
assertFalse(lock.hasWaiters(c)); |
640 |
|
|
assertEquals(0, lock.getWaitQueueLength(c)); |
641 |
|
|
lock.unlock(); |
642 |
|
|
t1.join(SHORT_DELAY_MS); |
643 |
|
|
t2.join(SHORT_DELAY_MS); |
644 |
|
|
assertFalse(t1.isAlive()); |
645 |
|
|
assertFalse(t2.isAlive()); |
646 |
dl |
1.13 |
} |
647 |
|
|
|
648 |
|
|
/** |
649 |
|
|
* getWaitingThreads returns only and all waiting threads |
650 |
|
|
*/ |
651 |
jsr166 |
1.29 |
public void testGetWaitingThreads() throws InterruptedException { |
652 |
jsr166 |
1.30 |
final PublicReentrantLock lock = new PublicReentrantLock(); |
653 |
dl |
1.13 |
final Condition c = lock.newCondition(); |
654 |
jsr166 |
1.30 |
Thread t1 = new Thread(new CheckedRunnable() { |
655 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
656 |
|
|
lock.lock(); |
657 |
jsr166 |
1.37 |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
658 |
jsr166 |
1.29 |
c.await(); |
659 |
|
|
lock.unlock(); |
660 |
|
|
}}); |
661 |
|
|
|
662 |
jsr166 |
1.30 |
Thread t2 = new Thread(new CheckedRunnable() { |
663 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
664 |
|
|
lock.lock(); |
665 |
jsr166 |
1.37 |
assertFalse(lock.getWaitingThreads(c).isEmpty()); |
666 |
jsr166 |
1.29 |
c.await(); |
667 |
|
|
lock.unlock(); |
668 |
|
|
}}); |
669 |
dl |
1.5 |
|
670 |
jsr166 |
1.29 |
lock.lock(); |
671 |
|
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
672 |
|
|
lock.unlock(); |
673 |
|
|
t1.start(); |
674 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
675 |
|
|
t2.start(); |
676 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
677 |
|
|
lock.lock(); |
678 |
|
|
assertTrue(lock.hasWaiters(c)); |
679 |
|
|
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
680 |
|
|
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
681 |
|
|
c.signalAll(); |
682 |
|
|
lock.unlock(); |
683 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
684 |
|
|
lock.lock(); |
685 |
|
|
assertFalse(lock.hasWaiters(c)); |
686 |
|
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
687 |
|
|
lock.unlock(); |
688 |
|
|
t1.join(SHORT_DELAY_MS); |
689 |
|
|
t2.join(SHORT_DELAY_MS); |
690 |
|
|
assertFalse(t1.isAlive()); |
691 |
|
|
assertFalse(t2.isAlive()); |
692 |
dl |
1.2 |
} |
693 |
dl |
1.13 |
|
694 |
dl |
1.22 |
/** A helper class for uninterruptible wait tests */ |
695 |
jsr166 |
1.29 |
class UninterruptibleThread extends Thread { |
696 |
dl |
1.22 |
private ReentrantLock lock; |
697 |
|
|
private Condition c; |
698 |
jsr166 |
1.26 |
|
699 |
dl |
1.22 |
public volatile boolean canAwake = false; |
700 |
|
|
public volatile boolean interrupted = false; |
701 |
|
|
public volatile boolean lockStarted = false; |
702 |
jsr166 |
1.26 |
|
703 |
jsr166 |
1.29 |
public UninterruptibleThread(ReentrantLock lock, Condition c) { |
704 |
dl |
1.22 |
this.lock = lock; |
705 |
|
|
this.c = c; |
706 |
|
|
} |
707 |
jsr166 |
1.26 |
|
708 |
dl |
1.22 |
public synchronized void run() { |
709 |
|
|
lock.lock(); |
710 |
|
|
lockStarted = true; |
711 |
jsr166 |
1.26 |
|
712 |
dl |
1.22 |
while (!canAwake) { |
713 |
|
|
c.awaitUninterruptibly(); |
714 |
|
|
} |
715 |
jsr166 |
1.26 |
|
716 |
dl |
1.22 |
interrupted = isInterrupted(); |
717 |
|
|
lock.unlock(); |
718 |
|
|
} |
719 |
|
|
} |
720 |
dl |
1.2 |
|
721 |
dl |
1.6 |
/** |
722 |
|
|
* awaitUninterruptibly doesn't abort on interrupt |
723 |
|
|
*/ |
724 |
jsr166 |
1.29 |
public void testAwaitUninterruptibly() throws InterruptedException { |
725 |
dl |
1.22 |
final ReentrantLock lock = new ReentrantLock(); |
726 |
dl |
1.2 |
final Condition c = lock.newCondition(); |
727 |
jsr166 |
1.29 |
UninterruptibleThread thread = new UninterruptibleThread(lock, c); |
728 |
dl |
1.2 |
|
729 |
jsr166 |
1.29 |
thread.start(); |
730 |
dl |
1.22 |
|
731 |
jsr166 |
1.29 |
while (!thread.lockStarted) { |
732 |
|
|
Thread.sleep(100); |
733 |
|
|
} |
734 |
dl |
1.22 |
|
735 |
jsr166 |
1.29 |
lock.lock(); |
736 |
|
|
try { |
737 |
|
|
thread.interrupt(); |
738 |
|
|
thread.canAwake = true; |
739 |
|
|
c.signal(); |
740 |
|
|
} finally { |
741 |
|
|
lock.unlock(); |
742 |
|
|
} |
743 |
dl |
1.22 |
|
744 |
jsr166 |
1.29 |
thread.join(); |
745 |
|
|
assertTrue(thread.interrupted); |
746 |
|
|
assertFalse(thread.isAlive()); |
747 |
dl |
1.2 |
} |
748 |
|
|
|
749 |
dl |
1.6 |
/** |
750 |
|
|
* await is interruptible |
751 |
|
|
*/ |
752 |
jsr166 |
1.29 |
public void testAwait_Interrupt() throws InterruptedException { |
753 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
754 |
dl |
1.2 |
final Condition c = lock.newCondition(); |
755 |
jsr166 |
1.30 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
756 |
|
|
public void realRun() throws InterruptedException { |
757 |
jsr166 |
1.29 |
lock.lock(); |
758 |
|
|
c.await(); |
759 |
|
|
}}); |
760 |
|
|
|
761 |
|
|
t.start(); |
762 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
763 |
|
|
t.interrupt(); |
764 |
|
|
t.join(SHORT_DELAY_MS); |
765 |
|
|
assertFalse(t.isAlive()); |
766 |
dl |
1.2 |
} |
767 |
|
|
|
768 |
dl |
1.6 |
/** |
769 |
|
|
* awaitNanos is interruptible |
770 |
|
|
*/ |
771 |
jsr166 |
1.29 |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
772 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
773 |
dl |
1.2 |
final Condition c = lock.newCondition(); |
774 |
jsr166 |
1.30 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
775 |
|
|
public void realRun() throws InterruptedException { |
776 |
jsr166 |
1.29 |
lock.lock(); |
777 |
jsr166 |
1.33 |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
778 |
jsr166 |
1.29 |
}}); |
779 |
|
|
|
780 |
|
|
t.start(); |
781 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
782 |
|
|
t.interrupt(); |
783 |
|
|
t.join(SHORT_DELAY_MS); |
784 |
|
|
assertFalse(t.isAlive()); |
785 |
dl |
1.2 |
} |
786 |
|
|
|
787 |
dl |
1.6 |
/** |
788 |
|
|
* awaitUntil is interruptible |
789 |
|
|
*/ |
790 |
jsr166 |
1.29 |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
791 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
792 |
dl |
1.2 |
final Condition c = lock.newCondition(); |
793 |
jsr166 |
1.30 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
794 |
|
|
public void realRun() throws InterruptedException { |
795 |
jsr166 |
1.29 |
lock.lock(); |
796 |
|
|
java.util.Date d = new java.util.Date(); |
797 |
|
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
798 |
|
|
}}); |
799 |
|
|
|
800 |
|
|
t.start(); |
801 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
802 |
|
|
t.interrupt(); |
803 |
|
|
t.join(SHORT_DELAY_MS); |
804 |
|
|
assertFalse(t.isAlive()); |
805 |
dl |
1.2 |
} |
806 |
|
|
|
807 |
dl |
1.6 |
/** |
808 |
|
|
* signalAll wakes up all threads |
809 |
|
|
*/ |
810 |
jsr166 |
1.29 |
public void testSignalAll() throws InterruptedException { |
811 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
812 |
dl |
1.2 |
final Condition c = lock.newCondition(); |
813 |
jsr166 |
1.30 |
Thread t1 = new Thread(new CheckedRunnable() { |
814 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
815 |
|
|
lock.lock(); |
816 |
|
|
c.await(); |
817 |
|
|
lock.unlock(); |
818 |
|
|
}}); |
819 |
|
|
|
820 |
jsr166 |
1.30 |
Thread t2 = new Thread(new CheckedRunnable() { |
821 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
822 |
|
|
lock.lock(); |
823 |
|
|
c.await(); |
824 |
|
|
lock.unlock(); |
825 |
|
|
}}); |
826 |
dl |
1.2 |
|
827 |
jsr166 |
1.29 |
t1.start(); |
828 |
|
|
t2.start(); |
829 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
830 |
|
|
lock.lock(); |
831 |
|
|
c.signalAll(); |
832 |
|
|
lock.unlock(); |
833 |
|
|
t1.join(SHORT_DELAY_MS); |
834 |
|
|
t2.join(SHORT_DELAY_MS); |
835 |
|
|
assertFalse(t1.isAlive()); |
836 |
|
|
assertFalse(t2.isAlive()); |
837 |
dl |
1.3 |
} |
838 |
|
|
|
839 |
dl |
1.6 |
/** |
840 |
dl |
1.23 |
* await after multiple reentrant locking preserves lock count |
841 |
|
|
*/ |
842 |
jsr166 |
1.29 |
public void testAwaitLockCount() throws InterruptedException { |
843 |
jsr166 |
1.30 |
final ReentrantLock lock = new ReentrantLock(); |
844 |
dl |
1.23 |
final Condition c = lock.newCondition(); |
845 |
jsr166 |
1.30 |
Thread t1 = new Thread(new CheckedRunnable() { |
846 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
847 |
|
|
lock.lock(); |
848 |
jsr166 |
1.37 |
assertEquals(1, lock.getHoldCount()); |
849 |
jsr166 |
1.29 |
c.await(); |
850 |
jsr166 |
1.37 |
assertEquals(1, lock.getHoldCount()); |
851 |
jsr166 |
1.29 |
lock.unlock(); |
852 |
|
|
}}); |
853 |
dl |
1.23 |
|
854 |
jsr166 |
1.30 |
Thread t2 = new Thread(new CheckedRunnable() { |
855 |
jsr166 |
1.29 |
public void realRun() throws InterruptedException { |
856 |
|
|
lock.lock(); |
857 |
|
|
lock.lock(); |
858 |
jsr166 |
1.37 |
assertEquals(2, lock.getHoldCount()); |
859 |
jsr166 |
1.29 |
c.await(); |
860 |
jsr166 |
1.37 |
assertEquals(2, lock.getHoldCount()); |
861 |
jsr166 |
1.29 |
lock.unlock(); |
862 |
|
|
lock.unlock(); |
863 |
|
|
}}); |
864 |
|
|
|
865 |
|
|
t1.start(); |
866 |
|
|
t2.start(); |
867 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
868 |
|
|
lock.lock(); |
869 |
|
|
c.signalAll(); |
870 |
|
|
lock.unlock(); |
871 |
|
|
t1.join(SHORT_DELAY_MS); |
872 |
|
|
t2.join(SHORT_DELAY_MS); |
873 |
|
|
assertFalse(t1.isAlive()); |
874 |
|
|
assertFalse(t2.isAlive()); |
875 |
dl |
1.23 |
} |
876 |
|
|
|
877 |
|
|
/** |
878 |
dl |
1.6 |
* A serialized lock deserializes as unlocked |
879 |
|
|
*/ |
880 |
jsr166 |
1.29 |
public void testSerialization() throws Exception { |
881 |
dl |
1.3 |
ReentrantLock l = new ReentrantLock(); |
882 |
|
|
l.lock(); |
883 |
|
|
l.unlock(); |
884 |
|
|
|
885 |
jsr166 |
1.29 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
886 |
|
|
ObjectOutputStream out = |
887 |
|
|
new ObjectOutputStream(new BufferedOutputStream(bout)); |
888 |
|
|
out.writeObject(l); |
889 |
|
|
out.close(); |
890 |
jsr166 |
1.35 |
|
891 |
jsr166 |
1.29 |
ByteArrayInputStream bin = |
892 |
|
|
new ByteArrayInputStream(bout.toByteArray()); |
893 |
|
|
ObjectInputStream in = |
894 |
|
|
new ObjectInputStream(new BufferedInputStream(bin)); |
895 |
|
|
ReentrantLock r = (ReentrantLock) in.readObject(); |
896 |
|
|
r.lock(); |
897 |
|
|
r.unlock(); |
898 |
dl |
1.2 |
} |
899 |
dl |
1.1 |
|
900 |
dl |
1.18 |
/** |
901 |
|
|
* toString indicates current lock state |
902 |
|
|
*/ |
903 |
|
|
public void testToString() { |
904 |
|
|
ReentrantLock lock = new ReentrantLock(); |
905 |
|
|
String us = lock.toString(); |
906 |
|
|
assertTrue(us.indexOf("Unlocked") >= 0); |
907 |
|
|
lock.lock(); |
908 |
|
|
String ls = lock.toString(); |
909 |
|
|
assertTrue(ls.indexOf("Locked") >= 0); |
910 |
|
|
} |
911 |
|
|
|
912 |
dl |
1.1 |
} |