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