2 |
|
* 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 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
14 |
|
|
15 |
|
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ReentrantReadWriteLockTest.class); |
23 |
|
/** |
24 |
|
* A runnable calling lockInterruptibly |
25 |
|
*/ |
26 |
< |
class InterruptibleLockRunnable implements Runnable { |
26 |
> |
class InterruptibleLockRunnable extends CheckedRunnable { |
27 |
|
final ReentrantReadWriteLock lock; |
28 |
|
InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
29 |
< |
public void run() { |
30 |
< |
try { |
31 |
< |
lock.writeLock().lockInterruptibly(); |
32 |
< |
} catch(InterruptedException success){} |
29 |
> |
public void realRun() throws InterruptedException { |
30 |
> |
lock.writeLock().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 ReentrantReadWriteLock lock; |
41 |
|
InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
42 |
< |
public void run() { |
43 |
< |
try { |
46 |
< |
lock.writeLock().lockInterruptibly(); |
47 |
< |
threadShouldThrow(); |
48 |
< |
} catch(InterruptedException success){} |
42 |
> |
public void realRun() throws InterruptedException { |
43 |
> |
lock.writeLock().lockInterruptibly(); |
44 |
|
} |
45 |
|
} |
46 |
|
|
49 |
|
*/ |
50 |
|
static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock { |
51 |
|
PublicReentrantReadWriteLock() { super(); } |
52 |
< |
public Collection<Thread> getQueuedThreads() { |
53 |
< |
return super.getQueuedThreads(); |
52 |
> |
public Collection<Thread> getQueuedThreads() { |
53 |
> |
return super.getQueuedThreads(); |
54 |
|
} |
55 |
< |
public Collection<Thread> getWaitingThreads(Condition c) { |
56 |
< |
return super.getWaitingThreads(c); |
55 |
> |
public Collection<Thread> getWaitingThreads(Condition c) { |
56 |
> |
return super.getWaitingThreads(c); |
57 |
|
} |
58 |
|
} |
59 |
|
|
60 |
|
/** |
61 |
|
* Constructor sets given fairness, and is in unlocked state |
62 |
|
*/ |
63 |
< |
public void testConstructor() { |
63 |
> |
public void testConstructor() { |
64 |
|
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
65 |
|
assertFalse(rl.isFair()); |
66 |
|
assertFalse(rl.isWriteLocked()); |
69 |
|
assertTrue(r2.isFair()); |
70 |
|
assertFalse(r2.isWriteLocked()); |
71 |
|
assertEquals(0, r2.getReadLockCount()); |
72 |
+ |
ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false); |
73 |
+ |
assertFalse(r3.isFair()); |
74 |
+ |
assertFalse(r3.isWriteLocked()); |
75 |
+ |
assertEquals(0, r3.getReadLockCount()); |
76 |
|
} |
77 |
|
|
78 |
|
/** |
79 |
|
* write-locking and read-locking an unlocked lock succeed |
80 |
|
*/ |
81 |
< |
public void testLock() { |
81 |
> |
public void testLock() { |
82 |
|
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
83 |
|
rl.writeLock().lock(); |
84 |
|
assertTrue(rl.isWriteLocked()); |
85 |
|
assertTrue(rl.isWriteLockedByCurrentThread()); |
86 |
+ |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
87 |
|
assertEquals(0, rl.getReadLockCount()); |
88 |
|
rl.writeLock().unlock(); |
89 |
|
assertFalse(rl.isWriteLocked()); |
90 |
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
91 |
+ |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
92 |
|
assertEquals(0, rl.getReadLockCount()); |
93 |
|
rl.readLock().lock(); |
94 |
|
assertFalse(rl.isWriteLocked()); |
104 |
|
/** |
105 |
|
* locking an unlocked fair lock succeeds |
106 |
|
*/ |
107 |
< |
public void testFairLock() { |
107 |
> |
public void testFairLock() { |
108 |
|
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true); |
109 |
|
rl.writeLock().lock(); |
110 |
|
assertTrue(rl.isWriteLocked()); |
111 |
|
assertTrue(rl.isWriteLockedByCurrentThread()); |
112 |
+ |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
113 |
|
assertEquals(0, rl.getReadLockCount()); |
114 |
|
rl.writeLock().unlock(); |
115 |
|
assertFalse(rl.isWriteLocked()); |
116 |
|
assertFalse(rl.isWriteLockedByCurrentThread()); |
117 |
+ |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
118 |
|
assertEquals(0, rl.getReadLockCount()); |
119 |
|
rl.readLock().lock(); |
120 |
|
assertFalse(rl.isWriteLocked()); |
129 |
|
/** |
130 |
|
* getWriteHoldCount returns number of recursive holds |
131 |
|
*/ |
132 |
< |
public void testGetHoldCount() { |
132 |
> |
public void testGetWriteHoldCount() { |
133 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
134 |
< |
for(int i = 1; i <= SIZE; i++) { |
134 |
> |
for (int i = 1; i <= SIZE; i++) { |
135 |
|
lock.writeLock().lock(); |
136 |
|
assertEquals(i,lock.getWriteHoldCount()); |
137 |
|
} |
138 |
< |
for(int i = SIZE; i > 0; i--) { |
138 |
> |
for (int i = SIZE; i > 0; i--) { |
139 |
|
lock.writeLock().unlock(); |
140 |
|
assertEquals(i-1,lock.getWriteHoldCount()); |
141 |
|
} |
142 |
|
} |
143 |
< |
|
143 |
> |
|
144 |
> |
/** |
145 |
> |
* WriteLock.getHoldCount returns number of recursive holds |
146 |
> |
*/ |
147 |
> |
public void testGetHoldCount() { |
148 |
> |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
149 |
> |
for (int i = 1; i <= SIZE; i++) { |
150 |
> |
lock.writeLock().lock(); |
151 |
> |
assertEquals(i,lock.writeLock().getHoldCount()); |
152 |
> |
} |
153 |
> |
for (int i = SIZE; i > 0; i--) { |
154 |
> |
lock.writeLock().unlock(); |
155 |
> |
assertEquals(i-1,lock.writeLock().getHoldCount()); |
156 |
> |
} |
157 |
> |
} |
158 |
> |
|
159 |
> |
/** |
160 |
> |
* getReadHoldCount returns number of recursive holds |
161 |
> |
*/ |
162 |
> |
public void testGetReadHoldCount() { |
163 |
> |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
164 |
> |
for (int i = 1; i <= SIZE; i++) { |
165 |
> |
lock.readLock().lock(); |
166 |
> |
assertEquals(i,lock.getReadHoldCount()); |
167 |
> |
} |
168 |
> |
for (int i = SIZE; i > 0; i--) { |
169 |
> |
lock.readLock().unlock(); |
170 |
> |
assertEquals(i-1,lock.getReadHoldCount()); |
171 |
> |
} |
172 |
> |
} |
173 |
> |
|
174 |
|
|
175 |
|
/** |
176 |
|
* write-unlocking an unlocked lock throws IllegalMonitorStateException |
177 |
|
*/ |
178 |
< |
public void testUnlock_IllegalMonitorStateException() { |
178 |
> |
public void testUnlock_IllegalMonitorStateException() { |
179 |
|
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
180 |
|
try { |
181 |
|
rl.writeLock().unlock(); |
182 |
|
shouldThrow(); |
183 |
< |
} catch(IllegalMonitorStateException success){} |
183 |
> |
} catch (IllegalMonitorStateException success) {} |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
/** |
188 |
|
* write-lockInterruptibly is interruptible |
189 |
|
*/ |
190 |
< |
public void testWriteLockInterruptibly_Interrupted() { |
190 |
> |
public void testWriteLockInterruptibly_Interrupted() throws Exception { |
191 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
192 |
< |
Thread t = new Thread(new Runnable() { |
193 |
< |
public void run() { |
194 |
< |
try { |
195 |
< |
lock.writeLock().lockInterruptibly(); |
196 |
< |
lock.writeLock().unlock(); |
197 |
< |
lock.writeLock().lockInterruptibly(); |
198 |
< |
lock.writeLock().unlock(); |
199 |
< |
} catch(InterruptedException success){} |
200 |
< |
} |
201 |
< |
}); |
202 |
< |
try { |
203 |
< |
lock.writeLock().lock(); |
204 |
< |
t.start(); |
205 |
< |
t.interrupt(); |
206 |
< |
lock.writeLock().unlock(); |
207 |
< |
t.join(); |
175 |
< |
} catch(Exception e){ |
176 |
< |
unexpectedException(); |
177 |
< |
} |
178 |
< |
} |
192 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
193 |
> |
public void realRun() throws InterruptedException { |
194 |
> |
lock.writeLock().lockInterruptibly(); |
195 |
> |
lock.writeLock().unlock(); |
196 |
> |
lock.writeLock().lockInterruptibly(); |
197 |
> |
lock.writeLock().unlock(); |
198 |
> |
}}); |
199 |
> |
|
200 |
> |
lock.writeLock().lock(); |
201 |
> |
t.start(); |
202 |
> |
Thread.sleep(SHORT_DELAY_MS); |
203 |
> |
t.interrupt(); |
204 |
> |
Thread.sleep(SHORT_DELAY_MS); |
205 |
> |
lock.writeLock().unlock(); |
206 |
> |
t.join(); |
207 |
> |
} |
208 |
|
|
209 |
|
/** |
210 |
|
* timed write-tryLock is interruptible |
211 |
|
*/ |
212 |
< |
public void testWriteTryLock_Interrupted() { |
212 |
> |
public void testWriteTryLock_Interrupted() throws InterruptedException { |
213 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
214 |
|
lock.writeLock().lock(); |
215 |
< |
Thread t = new Thread(new Runnable() { |
216 |
< |
public void run() { |
217 |
< |
try { |
218 |
< |
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
219 |
< |
} catch(InterruptedException success){} |
220 |
< |
} |
221 |
< |
}); |
222 |
< |
try { |
223 |
< |
t.start(); |
195 |
< |
t.interrupt(); |
196 |
< |
lock.writeLock().unlock(); |
197 |
< |
t.join(); |
198 |
< |
} catch(Exception e){ |
199 |
< |
unexpectedException(); |
200 |
< |
} |
215 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
216 |
> |
public void realRun() throws InterruptedException { |
217 |
> |
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
218 |
> |
}}); |
219 |
> |
|
220 |
> |
t.start(); |
221 |
> |
t.interrupt(); |
222 |
> |
lock.writeLock().unlock(); |
223 |
> |
t.join(); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
|
* read-lockInterruptibly is interruptible |
228 |
|
*/ |
229 |
< |
public void testReadLockInterruptibly_Interrupted() { |
229 |
> |
public void testReadLockInterruptibly_Interrupted() throws InterruptedException { |
230 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
231 |
|
lock.writeLock().lock(); |
232 |
< |
Thread t = new Thread(new Runnable() { |
233 |
< |
public void run() { |
234 |
< |
try { |
235 |
< |
lock.readLock().lockInterruptibly(); |
236 |
< |
} catch(InterruptedException success){} |
237 |
< |
} |
238 |
< |
}); |
239 |
< |
try { |
240 |
< |
t.start(); |
241 |
< |
t.interrupt(); |
242 |
< |
lock.writeLock().unlock(); |
243 |
< |
t.join(); |
221 |
< |
} catch(Exception e){ |
222 |
< |
unexpectedException(); |
223 |
< |
} |
224 |
< |
} |
232 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
233 |
> |
public void realRun() throws InterruptedException { |
234 |
> |
lock.readLock().lockInterruptibly(); |
235 |
> |
}}); |
236 |
> |
|
237 |
> |
t.start(); |
238 |
> |
Thread.sleep(SHORT_DELAY_MS); |
239 |
> |
t.interrupt(); |
240 |
> |
Thread.sleep(SHORT_DELAY_MS); |
241 |
> |
lock.writeLock().unlock(); |
242 |
> |
t.join(); |
243 |
> |
} |
244 |
|
|
245 |
|
/** |
246 |
|
* timed read-tryLock is interruptible |
247 |
|
*/ |
248 |
< |
public void testReadTryLock_Interrupted() { |
248 |
> |
public void testReadTryLock_Interrupted() throws InterruptedException { |
249 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
250 |
|
lock.writeLock().lock(); |
251 |
< |
Thread t = new Thread(new Runnable() { |
252 |
< |
public void run() { |
253 |
< |
try { |
254 |
< |
lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS); |
255 |
< |
threadShouldThrow(); |
256 |
< |
} catch(InterruptedException success){} |
257 |
< |
} |
258 |
< |
}); |
240 |
< |
try { |
241 |
< |
t.start(); |
242 |
< |
t.interrupt(); |
243 |
< |
t.join(); |
244 |
< |
} catch(Exception e){ |
245 |
< |
unexpectedException(); |
246 |
< |
} |
251 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
252 |
> |
public void realRun() throws InterruptedException { |
253 |
> |
lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS); |
254 |
> |
}}); |
255 |
> |
|
256 |
> |
t.start(); |
257 |
> |
t.interrupt(); |
258 |
> |
t.join(); |
259 |
|
} |
260 |
|
|
261 |
< |
|
261 |
> |
|
262 |
|
/** |
263 |
|
* write-tryLock fails if locked |
264 |
|
*/ |
265 |
< |
public void testWriteTryLockWhenLocked() { |
265 |
> |
public void testWriteTryLockWhenLocked() throws InterruptedException { |
266 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
267 |
|
lock.writeLock().lock(); |
268 |
|
Thread t = new Thread(new Runnable() { |
270 |
|
threadAssertFalse(lock.writeLock().tryLock()); |
271 |
|
} |
272 |
|
}); |
273 |
< |
try { |
274 |
< |
t.start(); |
275 |
< |
t.join(); |
276 |
< |
lock.writeLock().unlock(); |
277 |
< |
} catch(Exception e){ |
266 |
< |
unexpectedException(); |
267 |
< |
} |
268 |
< |
} |
273 |
> |
|
274 |
> |
t.start(); |
275 |
> |
t.join(); |
276 |
> |
lock.writeLock().unlock(); |
277 |
> |
} |
278 |
|
|
279 |
|
/** |
280 |
|
* read-tryLock fails if locked |
281 |
|
*/ |
282 |
< |
public void testReadTryLockWhenLocked() { |
282 |
> |
public void testReadTryLockWhenLocked() throws InterruptedException { |
283 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
284 |
|
lock.writeLock().lock(); |
285 |
|
Thread t = new Thread(new Runnable() { |
287 |
|
threadAssertFalse(lock.readLock().tryLock()); |
288 |
|
} |
289 |
|
}); |
290 |
< |
try { |
291 |
< |
t.start(); |
292 |
< |
t.join(); |
293 |
< |
lock.writeLock().unlock(); |
294 |
< |
} catch(Exception e){ |
286 |
< |
unexpectedException(); |
287 |
< |
} |
288 |
< |
} |
290 |
> |
|
291 |
> |
t.start(); |
292 |
> |
t.join(); |
293 |
> |
lock.writeLock().unlock(); |
294 |
> |
} |
295 |
|
|
296 |
|
/** |
297 |
|
* Multiple threads can hold a read lock when not write-locked |
298 |
|
*/ |
299 |
< |
public void testMultipleReadLocks() { |
299 |
> |
public void testMultipleReadLocks() throws InterruptedException { |
300 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
301 |
|
lock.readLock().lock(); |
302 |
|
Thread t = new Thread(new Runnable() { |
305 |
|
lock.readLock().unlock(); |
306 |
|
} |
307 |
|
}); |
308 |
< |
try { |
309 |
< |
t.start(); |
310 |
< |
t.join(); |
311 |
< |
lock.readLock().unlock(); |
312 |
< |
} catch(Exception e){ |
307 |
< |
unexpectedException(); |
308 |
< |
} |
309 |
< |
} |
308 |
> |
|
309 |
> |
t.start(); |
310 |
> |
t.join(); |
311 |
> |
lock.readLock().unlock(); |
312 |
> |
} |
313 |
|
|
314 |
|
/** |
315 |
|
* A writelock succeeds after reading threads unlock |
316 |
|
*/ |
317 |
< |
public void testWriteAfterMultipleReadLocks() { |
317 |
> |
public void testWriteAfterMultipleReadLocks() throws InterruptedException { |
318 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
319 |
|
lock.readLock().lock(); |
320 |
|
Thread t1 = new Thread(new Runnable() { |
330 |
|
} |
331 |
|
}); |
332 |
|
|
333 |
< |
try { |
334 |
< |
t1.start(); |
335 |
< |
t2.start(); |
336 |
< |
Thread.sleep(SHORT_DELAY_MS); |
337 |
< |
lock.readLock().unlock(); |
338 |
< |
t1.join(MEDIUM_DELAY_MS); |
339 |
< |
t2.join(MEDIUM_DELAY_MS); |
340 |
< |
assertTrue(!t1.isAlive()); |
341 |
< |
assertTrue(!t2.isAlive()); |
339 |
< |
|
340 |
< |
} catch(Exception e){ |
341 |
< |
unexpectedException(); |
342 |
< |
} |
343 |
< |
} |
333 |
> |
t1.start(); |
334 |
> |
t2.start(); |
335 |
> |
Thread.sleep(SHORT_DELAY_MS); |
336 |
> |
lock.readLock().unlock(); |
337 |
> |
t1.join(MEDIUM_DELAY_MS); |
338 |
> |
t2.join(MEDIUM_DELAY_MS); |
339 |
> |
assertTrue(!t1.isAlive()); |
340 |
> |
assertTrue(!t2.isAlive()); |
341 |
> |
} |
342 |
|
|
343 |
|
/** |
344 |
|
* Readlocks succeed after a writing thread unlocks |
345 |
|
*/ |
346 |
< |
public void testReadAfterWriteLock() { |
346 |
> |
public void testReadAfterWriteLock() throws InterruptedException { |
347 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
348 |
|
lock.writeLock().lock(); |
349 |
|
Thread t1 = new Thread(new Runnable() { |
359 |
|
} |
360 |
|
}); |
361 |
|
|
362 |
< |
try { |
363 |
< |
t1.start(); |
364 |
< |
t2.start(); |
365 |
< |
Thread.sleep(SHORT_DELAY_MS); |
366 |
< |
lock.writeLock().unlock(); |
367 |
< |
t1.join(MEDIUM_DELAY_MS); |
368 |
< |
t2.join(MEDIUM_DELAY_MS); |
369 |
< |
assertTrue(!t1.isAlive()); |
370 |
< |
assertTrue(!t2.isAlive()); |
373 |
< |
|
374 |
< |
} catch(Exception e){ |
375 |
< |
unexpectedException(); |
376 |
< |
} |
377 |
< |
} |
362 |
> |
t1.start(); |
363 |
> |
t2.start(); |
364 |
> |
Thread.sleep(SHORT_DELAY_MS); |
365 |
> |
lock.writeLock().unlock(); |
366 |
> |
t1.join(MEDIUM_DELAY_MS); |
367 |
> |
t2.join(MEDIUM_DELAY_MS); |
368 |
> |
assertTrue(!t1.isAlive()); |
369 |
> |
assertTrue(!t2.isAlive()); |
370 |
> |
} |
371 |
|
|
372 |
|
/** |
373 |
|
* Read trylock succeeds if write locked by current thread |
374 |
|
*/ |
375 |
< |
public void testReadHoldingWriteLock() { |
375 |
> |
public void testReadHoldingWriteLock() { |
376 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
377 |
|
lock.writeLock().lock(); |
378 |
|
assertTrue(lock.readLock().tryLock()); |
379 |
|
lock.readLock().unlock(); |
380 |
|
lock.writeLock().unlock(); |
381 |
< |
} |
381 |
> |
} |
382 |
|
|
383 |
|
/** |
384 |
|
* Read lock succeeds if write locked by current thread even if |
385 |
< |
* other threads are waiting |
385 |
> |
* other threads are waiting for readlock |
386 |
|
*/ |
387 |
< |
public void testReadHoldingWriteLock2() { |
387 |
> |
public void testReadHoldingWriteLock2() throws InterruptedException { |
388 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
389 |
|
lock.writeLock().lock(); |
390 |
|
Thread t1 = new Thread(new Runnable() { |
400 |
|
} |
401 |
|
}); |
402 |
|
|
403 |
< |
try { |
404 |
< |
t1.start(); |
405 |
< |
t2.start(); |
406 |
< |
lock.readLock().lock(); |
407 |
< |
lock.readLock().unlock(); |
408 |
< |
Thread.sleep(SHORT_DELAY_MS); |
409 |
< |
lock.readLock().lock(); |
410 |
< |
lock.readLock().unlock(); |
411 |
< |
lock.writeLock().unlock(); |
412 |
< |
t1.join(MEDIUM_DELAY_MS); |
413 |
< |
t2.join(MEDIUM_DELAY_MS); |
414 |
< |
assertTrue(!t1.isAlive()); |
415 |
< |
assertTrue(!t2.isAlive()); |
416 |
< |
|
417 |
< |
} catch(Exception e){ |
418 |
< |
unexpectedException(); |
419 |
< |
} |
420 |
< |
} |
403 |
> |
t1.start(); |
404 |
> |
t2.start(); |
405 |
> |
lock.readLock().lock(); |
406 |
> |
lock.readLock().unlock(); |
407 |
> |
Thread.sleep(SHORT_DELAY_MS); |
408 |
> |
lock.readLock().lock(); |
409 |
> |
lock.readLock().unlock(); |
410 |
> |
lock.writeLock().unlock(); |
411 |
> |
t1.join(MEDIUM_DELAY_MS); |
412 |
> |
t2.join(MEDIUM_DELAY_MS); |
413 |
> |
assertTrue(!t1.isAlive()); |
414 |
> |
assertTrue(!t2.isAlive()); |
415 |
> |
} |
416 |
> |
|
417 |
> |
/** |
418 |
> |
* Read lock succeeds if write locked by current thread even if |
419 |
> |
* other threads are waiting for writelock |
420 |
> |
*/ |
421 |
> |
public void testReadHoldingWriteLock3() throws InterruptedException { |
422 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
423 |
> |
lock.writeLock().lock(); |
424 |
> |
Thread t1 = new Thread(new Runnable() { |
425 |
> |
public void run() { |
426 |
> |
lock.writeLock().lock(); |
427 |
> |
lock.writeLock().unlock(); |
428 |
> |
} |
429 |
> |
}); |
430 |
> |
Thread t2 = new Thread(new Runnable() { |
431 |
> |
public void run() { |
432 |
> |
lock.writeLock().lock(); |
433 |
> |
lock.writeLock().unlock(); |
434 |
> |
} |
435 |
> |
}); |
436 |
> |
|
437 |
> |
t1.start(); |
438 |
> |
t2.start(); |
439 |
> |
lock.readLock().lock(); |
440 |
> |
lock.readLock().unlock(); |
441 |
> |
Thread.sleep(SHORT_DELAY_MS); |
442 |
> |
lock.readLock().lock(); |
443 |
> |
lock.readLock().unlock(); |
444 |
> |
lock.writeLock().unlock(); |
445 |
> |
t1.join(MEDIUM_DELAY_MS); |
446 |
> |
t2.join(MEDIUM_DELAY_MS); |
447 |
> |
assertTrue(!t1.isAlive()); |
448 |
> |
assertTrue(!t2.isAlive()); |
449 |
> |
} |
450 |
> |
|
451 |
> |
|
452 |
> |
/** |
453 |
> |
* Write lock succeeds if write locked by current thread even if |
454 |
> |
* other threads are waiting for writelock |
455 |
> |
*/ |
456 |
> |
public void testWriteHoldingWriteLock4() throws InterruptedException { |
457 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
458 |
> |
lock.writeLock().lock(); |
459 |
> |
Thread t1 = new Thread(new Runnable() { |
460 |
> |
public void run() { |
461 |
> |
lock.writeLock().lock(); |
462 |
> |
lock.writeLock().unlock(); |
463 |
> |
} |
464 |
> |
}); |
465 |
> |
Thread t2 = new Thread(new Runnable() { |
466 |
> |
public void run() { |
467 |
> |
lock.writeLock().lock(); |
468 |
> |
lock.writeLock().unlock(); |
469 |
> |
} |
470 |
> |
}); |
471 |
> |
|
472 |
> |
t1.start(); |
473 |
> |
t2.start(); |
474 |
> |
lock.writeLock().lock(); |
475 |
> |
lock.writeLock().unlock(); |
476 |
> |
Thread.sleep(SHORT_DELAY_MS); |
477 |
> |
lock.writeLock().lock(); |
478 |
> |
lock.writeLock().unlock(); |
479 |
> |
lock.writeLock().unlock(); |
480 |
> |
t1.join(MEDIUM_DELAY_MS); |
481 |
> |
t2.join(MEDIUM_DELAY_MS); |
482 |
> |
assertTrue(!t1.isAlive()); |
483 |
> |
assertTrue(!t2.isAlive()); |
484 |
> |
} |
485 |
> |
|
486 |
|
|
487 |
|
/** |
488 |
|
* Fair Read trylock succeeds if write locked by current thread |
489 |
|
*/ |
490 |
< |
public void testReadHoldingWriteLockFair() { |
490 |
> |
public void testReadHoldingWriteLockFair() { |
491 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
492 |
|
lock.writeLock().lock(); |
493 |
|
assertTrue(lock.readLock().tryLock()); |
494 |
|
lock.readLock().unlock(); |
495 |
|
lock.writeLock().unlock(); |
496 |
< |
} |
496 |
> |
} |
497 |
|
|
498 |
|
/** |
499 |
|
* Fair Read lock succeeds if write locked by current thread even if |
500 |
< |
* other threads are waiting |
500 |
> |
* other threads are waiting for readlock |
501 |
|
*/ |
502 |
< |
public void testReadHoldingWriteLockFair2() { |
502 |
> |
public void testReadHoldingWriteLockFair2() throws InterruptedException { |
503 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
504 |
|
lock.writeLock().lock(); |
505 |
|
Thread t1 = new Thread(new Runnable() { |
515 |
|
} |
516 |
|
}); |
517 |
|
|
518 |
< |
try { |
519 |
< |
t1.start(); |
520 |
< |
t2.start(); |
521 |
< |
lock.readLock().lock(); |
522 |
< |
lock.readLock().unlock(); |
523 |
< |
Thread.sleep(SHORT_DELAY_MS); |
524 |
< |
lock.readLock().lock(); |
525 |
< |
lock.readLock().unlock(); |
526 |
< |
lock.writeLock().unlock(); |
527 |
< |
t1.join(MEDIUM_DELAY_MS); |
528 |
< |
t2.join(MEDIUM_DELAY_MS); |
529 |
< |
assertTrue(!t1.isAlive()); |
530 |
< |
assertTrue(!t2.isAlive()); |
531 |
< |
|
532 |
< |
} catch(Exception e){ |
533 |
< |
unexpectedException(); |
534 |
< |
} |
535 |
< |
} |
518 |
> |
t1.start(); |
519 |
> |
t2.start(); |
520 |
> |
lock.readLock().lock(); |
521 |
> |
lock.readLock().unlock(); |
522 |
> |
Thread.sleep(SHORT_DELAY_MS); |
523 |
> |
lock.readLock().lock(); |
524 |
> |
lock.readLock().unlock(); |
525 |
> |
lock.writeLock().unlock(); |
526 |
> |
t1.join(MEDIUM_DELAY_MS); |
527 |
> |
t2.join(MEDIUM_DELAY_MS); |
528 |
> |
assertTrue(!t1.isAlive()); |
529 |
> |
assertTrue(!t2.isAlive()); |
530 |
> |
} |
531 |
> |
|
532 |
> |
|
533 |
> |
/** |
534 |
> |
* Fair Read lock succeeds if write locked by current thread even if |
535 |
> |
* other threads are waiting for writelock |
536 |
> |
*/ |
537 |
> |
public void testReadHoldingWriteLockFair3() throws InterruptedException { |
538 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
539 |
> |
lock.writeLock().lock(); |
540 |
> |
Thread t1 = new Thread(new Runnable() { |
541 |
> |
public void run() { |
542 |
> |
lock.writeLock().lock(); |
543 |
> |
lock.writeLock().unlock(); |
544 |
> |
} |
545 |
> |
}); |
546 |
> |
Thread t2 = new Thread(new Runnable() { |
547 |
> |
public void run() { |
548 |
> |
lock.writeLock().lock(); |
549 |
> |
lock.writeLock().unlock(); |
550 |
> |
} |
551 |
> |
}); |
552 |
> |
|
553 |
> |
t1.start(); |
554 |
> |
t2.start(); |
555 |
> |
lock.readLock().lock(); |
556 |
> |
lock.readLock().unlock(); |
557 |
> |
Thread.sleep(SHORT_DELAY_MS); |
558 |
> |
lock.readLock().lock(); |
559 |
> |
lock.readLock().unlock(); |
560 |
> |
lock.writeLock().unlock(); |
561 |
> |
t1.join(MEDIUM_DELAY_MS); |
562 |
> |
t2.join(MEDIUM_DELAY_MS); |
563 |
> |
assertTrue(!t1.isAlive()); |
564 |
> |
assertTrue(!t2.isAlive()); |
565 |
> |
} |
566 |
> |
|
567 |
> |
|
568 |
> |
/** |
569 |
> |
* Fair Write lock succeeds if write locked by current thread even if |
570 |
> |
* other threads are waiting for writelock |
571 |
> |
*/ |
572 |
> |
public void testWriteHoldingWriteLockFair4() throws InterruptedException { |
573 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
574 |
> |
lock.writeLock().lock(); |
575 |
> |
Thread t1 = new Thread(new Runnable() { |
576 |
> |
public void run() { |
577 |
> |
lock.writeLock().lock(); |
578 |
> |
lock.writeLock().unlock(); |
579 |
> |
} |
580 |
> |
}); |
581 |
> |
Thread t2 = new Thread(new Runnable() { |
582 |
> |
public void run() { |
583 |
> |
lock.writeLock().lock(); |
584 |
> |
lock.writeLock().unlock(); |
585 |
> |
} |
586 |
> |
}); |
587 |
> |
|
588 |
> |
t1.start(); |
589 |
> |
t2.start(); |
590 |
> |
Thread.sleep(SHORT_DELAY_MS); |
591 |
> |
assertTrue(lock.isWriteLockedByCurrentThread()); |
592 |
> |
assertTrue(lock.getWriteHoldCount() == 1); |
593 |
> |
lock.writeLock().lock(); |
594 |
> |
assertTrue(lock.getWriteHoldCount() == 2); |
595 |
> |
lock.writeLock().unlock(); |
596 |
> |
lock.writeLock().lock(); |
597 |
> |
lock.writeLock().unlock(); |
598 |
> |
lock.writeLock().unlock(); |
599 |
> |
t1.join(MEDIUM_DELAY_MS); |
600 |
> |
t2.join(MEDIUM_DELAY_MS); |
601 |
> |
assertTrue(!t1.isAlive()); |
602 |
> |
assertTrue(!t2.isAlive()); |
603 |
> |
} |
604 |
|
|
605 |
|
|
606 |
|
/** |
607 |
|
* Read tryLock succeeds if readlocked but not writelocked |
608 |
|
*/ |
609 |
< |
public void testTryLockWhenReadLocked() { |
609 |
> |
public void testTryLockWhenReadLocked() throws InterruptedException { |
610 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
611 |
|
lock.readLock().lock(); |
612 |
|
Thread t = new Thread(new Runnable() { |
615 |
|
lock.readLock().unlock(); |
616 |
|
} |
617 |
|
}); |
492 |
– |
try { |
493 |
– |
t.start(); |
494 |
– |
t.join(); |
495 |
– |
lock.readLock().unlock(); |
496 |
– |
} catch(Exception e){ |
497 |
– |
unexpectedException(); |
498 |
– |
} |
499 |
– |
} |
618 |
|
|
619 |
< |
|
619 |
> |
t.start(); |
620 |
> |
t.join(); |
621 |
> |
lock.readLock().unlock(); |
622 |
> |
} |
623 |
> |
|
624 |
> |
|
625 |
|
|
626 |
|
/** |
627 |
|
* write tryLock fails when readlocked |
628 |
|
*/ |
629 |
< |
public void testWriteTryLockWhenReadLocked() { |
629 |
> |
public void testWriteTryLockWhenReadLocked() throws InterruptedException { |
630 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
631 |
|
lock.readLock().lock(); |
632 |
|
Thread t = new Thread(new Runnable() { |
634 |
|
threadAssertFalse(lock.writeLock().tryLock()); |
635 |
|
} |
636 |
|
}); |
514 |
– |
try { |
515 |
– |
t.start(); |
516 |
– |
t.join(); |
517 |
– |
lock.readLock().unlock(); |
518 |
– |
} catch(Exception e){ |
519 |
– |
unexpectedException(); |
520 |
– |
} |
521 |
– |
} |
637 |
|
|
638 |
< |
|
638 |
> |
t.start(); |
639 |
> |
t.join(); |
640 |
> |
lock.readLock().unlock(); |
641 |
> |
} |
642 |
> |
|
643 |
|
|
644 |
|
/** |
645 |
< |
* write timed tryLock times out if locked |
645 |
> |
* Fair Read tryLock succeeds if readlocked but not writelocked |
646 |
|
*/ |
647 |
< |
public void testWriteTryLock_Timeout() { |
648 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
649 |
< |
lock.writeLock().lock(); |
647 |
> |
public void testTryLockWhenReadLockedFair() throws InterruptedException { |
648 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
649 |
> |
lock.readLock().lock(); |
650 |
|
Thread t = new Thread(new Runnable() { |
651 |
|
public void run() { |
652 |
< |
try { |
653 |
< |
threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
535 |
< |
} catch (Exception ex) { |
536 |
< |
threadUnexpectedException(); |
537 |
< |
} |
652 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
653 |
> |
lock.readLock().unlock(); |
654 |
|
} |
655 |
|
}); |
656 |
< |
try { |
657 |
< |
t.start(); |
658 |
< |
t.join(); |
659 |
< |
lock.writeLock().unlock(); |
660 |
< |
} catch(Exception e){ |
661 |
< |
unexpectedException(); |
662 |
< |
} |
547 |
< |
} |
656 |
> |
|
657 |
> |
t.start(); |
658 |
> |
t.join(); |
659 |
> |
lock.readLock().unlock(); |
660 |
> |
} |
661 |
> |
|
662 |
> |
|
663 |
|
|
664 |
|
/** |
665 |
< |
* read timed tryLock times out if write-locked |
665 |
> |
* Fair write tryLock fails when readlocked |
666 |
|
*/ |
667 |
< |
public void testReadTryLock_Timeout() { |
668 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
669 |
< |
lock.writeLock().lock(); |
667 |
> |
public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { |
668 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
669 |
> |
lock.readLock().lock(); |
670 |
|
Thread t = new Thread(new Runnable() { |
671 |
|
public void run() { |
672 |
< |
try { |
558 |
< |
threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
559 |
< |
} catch (Exception ex) { |
560 |
< |
threadUnexpectedException(); |
561 |
< |
} |
672 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
673 |
|
} |
674 |
|
}); |
675 |
< |
try { |
676 |
< |
t.start(); |
677 |
< |
t.join(); |
678 |
< |
lock.writeLock().unlock(); |
679 |
< |
} catch(Exception e){ |
680 |
< |
unexpectedException(); |
681 |
< |
} |
682 |
< |
} |
675 |
> |
|
676 |
> |
t.start(); |
677 |
> |
t.join(); |
678 |
> |
lock.readLock().unlock(); |
679 |
> |
} |
680 |
> |
|
681 |
> |
|
682 |
> |
|
683 |
> |
/** |
684 |
> |
* write timed tryLock times out if locked |
685 |
> |
*/ |
686 |
> |
public void testWriteTryLock_Timeout() throws InterruptedException { |
687 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
688 |
> |
lock.writeLock().lock(); |
689 |
> |
Thread t = new Thread(new CheckedRunnable() { |
690 |
> |
public void realRun() throws InterruptedException { |
691 |
> |
threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
692 |
> |
}}); |
693 |
> |
|
694 |
> |
t.start(); |
695 |
> |
t.join(); |
696 |
> |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
697 |
> |
lock.writeLock().unlock(); |
698 |
> |
} |
699 |
> |
|
700 |
> |
/** |
701 |
> |
* read timed tryLock times out if write-locked |
702 |
> |
*/ |
703 |
> |
public void testReadTryLock_Timeout() throws InterruptedException { |
704 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
705 |
> |
lock.writeLock().lock(); |
706 |
> |
Thread t = new Thread(new CheckedRunnable() { |
707 |
> |
public void realRun() throws InterruptedException { |
708 |
> |
threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
709 |
> |
}}); |
710 |
> |
|
711 |
> |
t.start(); |
712 |
> |
t.join(); |
713 |
> |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
714 |
> |
lock.writeLock().unlock(); |
715 |
> |
} |
716 |
|
|
717 |
|
|
718 |
|
/** |
719 |
|
* write lockInterruptibly succeeds if lock free else is interruptible |
720 |
|
*/ |
721 |
< |
public void testWriteLockInterruptibly() { |
721 |
> |
public void testWriteLockInterruptibly() throws InterruptedException { |
722 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
723 |
< |
try { |
724 |
< |
lock.writeLock().lockInterruptibly(); |
725 |
< |
} catch(Exception e) { |
726 |
< |
unexpectedException(); |
727 |
< |
} |
728 |
< |
Thread t = new Thread(new Runnable() { |
729 |
< |
public void run() { |
730 |
< |
try { |
731 |
< |
lock.writeLock().lockInterruptibly(); |
732 |
< |
threadShouldThrow(); |
733 |
< |
} |
734 |
< |
catch(InterruptedException success) { |
591 |
< |
} |
592 |
< |
} |
593 |
< |
}); |
594 |
< |
try { |
595 |
< |
t.start(); |
596 |
< |
t.interrupt(); |
597 |
< |
t.join(); |
598 |
< |
lock.writeLock().unlock(); |
599 |
< |
} catch(Exception e){ |
600 |
< |
unexpectedException(); |
601 |
< |
} |
723 |
> |
lock.writeLock().lockInterruptibly(); |
724 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
725 |
> |
public void realRun() throws InterruptedException { |
726 |
> |
lock.writeLock().lockInterruptibly(); |
727 |
> |
}}); |
728 |
> |
|
729 |
> |
t.start(); |
730 |
> |
Thread.sleep(SHORT_DELAY_MS); |
731 |
> |
t.interrupt(); |
732 |
> |
Thread.sleep(SHORT_DELAY_MS); |
733 |
> |
t.join(); |
734 |
> |
lock.writeLock().unlock(); |
735 |
|
} |
736 |
|
|
737 |
|
/** |
738 |
|
* read lockInterruptibly succeeds if lock free else is interruptible |
739 |
|
*/ |
740 |
< |
public void testReadLockInterruptibly() { |
740 |
> |
public void testReadLockInterruptibly() throws InterruptedException { |
741 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
742 |
< |
try { |
743 |
< |
lock.writeLock().lockInterruptibly(); |
744 |
< |
} catch(Exception e) { |
745 |
< |
unexpectedException(); |
746 |
< |
} |
747 |
< |
Thread t = new Thread(new Runnable() { |
748 |
< |
public void run() { |
749 |
< |
try { |
750 |
< |
lock.readLock().lockInterruptibly(); |
751 |
< |
threadShouldThrow(); |
752 |
< |
} |
620 |
< |
catch(InterruptedException success) { |
621 |
< |
} |
622 |
< |
} |
623 |
< |
}); |
624 |
< |
try { |
625 |
< |
t.start(); |
626 |
< |
t.interrupt(); |
627 |
< |
t.join(); |
628 |
< |
lock.writeLock().unlock(); |
629 |
< |
} catch(Exception e){ |
630 |
< |
unexpectedException(); |
631 |
< |
} |
742 |
> |
lock.writeLock().lockInterruptibly(); |
743 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
744 |
> |
public void realRun() throws InterruptedException { |
745 |
> |
lock.readLock().lockInterruptibly(); |
746 |
> |
}}); |
747 |
> |
|
748 |
> |
t.start(); |
749 |
> |
Thread.sleep(SHORT_DELAY_MS); |
750 |
> |
t.interrupt(); |
751 |
> |
t.join(); |
752 |
> |
lock.writeLock().unlock(); |
753 |
|
} |
754 |
|
|
755 |
|
/** |
756 |
|
* Calling await without holding lock throws IllegalMonitorStateException |
757 |
|
*/ |
758 |
< |
public void testAwait_IllegalMonitor() { |
759 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
758 |
> |
public void testAwait_IllegalMonitor() throws InterruptedException { |
759 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
760 |
|
final Condition c = lock.writeLock().newCondition(); |
761 |
|
try { |
762 |
|
c.await(); |
763 |
|
shouldThrow(); |
764 |
< |
} |
644 |
< |
catch (IllegalMonitorStateException success) { |
645 |
< |
} |
646 |
< |
catch (Exception ex) { |
647 |
< |
shouldThrow(); |
648 |
< |
} |
764 |
> |
} catch (IllegalMonitorStateException success) {} |
765 |
|
} |
766 |
|
|
767 |
|
/** |
768 |
|
* Calling signal without holding lock throws IllegalMonitorStateException |
769 |
|
*/ |
770 |
|
public void testSignal_IllegalMonitor() { |
771 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
771 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
772 |
|
final Condition c = lock.writeLock().newCondition(); |
773 |
|
try { |
774 |
|
c.signal(); |
775 |
|
shouldThrow(); |
776 |
< |
} |
661 |
< |
catch (IllegalMonitorStateException success) { |
662 |
< |
} |
663 |
< |
catch (Exception ex) { |
664 |
< |
unexpectedException(); |
665 |
< |
} |
776 |
> |
} catch (IllegalMonitorStateException success) {} |
777 |
|
} |
778 |
|
|
779 |
|
/** |
780 |
|
* awaitNanos without a signal times out |
781 |
|
*/ |
782 |
< |
public void testAwaitNanos_Timeout() { |
783 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
782 |
> |
public void testAwaitNanos_Timeout() throws InterruptedException { |
783 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
784 |
|
final Condition c = lock.writeLock().newCondition(); |
785 |
< |
try { |
786 |
< |
lock.writeLock().lock(); |
787 |
< |
long t = c.awaitNanos(100); |
788 |
< |
assertTrue(t <= 0); |
789 |
< |
lock.writeLock().unlock(); |
679 |
< |
} |
680 |
< |
catch (Exception ex) { |
681 |
< |
unexpectedException(); |
682 |
< |
} |
785 |
> |
|
786 |
> |
lock.writeLock().lock(); |
787 |
> |
long t = c.awaitNanos(100); |
788 |
> |
assertTrue(t <= 0); |
789 |
> |
lock.writeLock().unlock(); |
790 |
|
} |
791 |
|
|
792 |
|
|
793 |
|
/** |
794 |
|
* timed await without a signal times out |
795 |
|
*/ |
796 |
< |
public void testAwait_Timeout() { |
797 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
796 |
> |
public void testAwait_Timeout() throws InterruptedException { |
797 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
798 |
|
final Condition c = lock.writeLock().newCondition(); |
799 |
< |
try { |
800 |
< |
lock.writeLock().lock(); |
801 |
< |
lock.writeLock().unlock(); |
695 |
< |
} |
696 |
< |
catch (Exception ex) { |
697 |
< |
unexpectedException(); |
698 |
< |
} |
799 |
> |
lock.writeLock().lock(); |
800 |
> |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
801 |
> |
lock.writeLock().unlock(); |
802 |
|
} |
803 |
|
|
804 |
|
/** |
805 |
|
* awaitUntil without a signal times out |
806 |
|
*/ |
807 |
< |
public void testAwaitUntil_Timeout() { |
808 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
807 |
> |
public void testAwaitUntil_Timeout() throws InterruptedException { |
808 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
809 |
|
final Condition c = lock.writeLock().newCondition(); |
810 |
< |
try { |
811 |
< |
lock.writeLock().lock(); |
812 |
< |
java.util.Date d = new java.util.Date(); |
813 |
< |
lock.writeLock().unlock(); |
711 |
< |
} |
712 |
< |
catch (Exception ex) { |
713 |
< |
unexpectedException(); |
714 |
< |
} |
810 |
> |
lock.writeLock().lock(); |
811 |
> |
java.util.Date d = new java.util.Date(); |
812 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
813 |
> |
lock.writeLock().unlock(); |
814 |
|
} |
815 |
|
|
816 |
|
/** |
817 |
|
* await returns when signalled |
818 |
|
*/ |
819 |
< |
public void testAwait() { |
820 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
819 |
> |
public void testAwait() throws InterruptedException { |
820 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
821 |
|
final Condition c = lock.writeLock().newCondition(); |
822 |
< |
Thread t = new Thread(new Runnable() { |
823 |
< |
public void run() { |
824 |
< |
try { |
825 |
< |
lock.writeLock().lock(); |
826 |
< |
c.await(); |
827 |
< |
lock.writeLock().unlock(); |
729 |
< |
} |
730 |
< |
catch(InterruptedException e) { |
731 |
< |
threadUnexpectedException(); |
732 |
< |
} |
733 |
< |
} |
734 |
< |
}); |
822 |
> |
Thread t = new Thread(new CheckedRunnable() { |
823 |
> |
public void realRun() throws InterruptedException { |
824 |
> |
lock.writeLock().lock(); |
825 |
> |
c.await(); |
826 |
> |
lock.writeLock().unlock(); |
827 |
> |
}}); |
828 |
|
|
829 |
< |
try { |
830 |
< |
t.start(); |
831 |
< |
Thread.sleep(SHORT_DELAY_MS); |
832 |
< |
lock.writeLock().lock(); |
833 |
< |
c.signal(); |
834 |
< |
lock.writeLock().unlock(); |
835 |
< |
t.join(SHORT_DELAY_MS); |
743 |
< |
assertFalse(t.isAlive()); |
744 |
< |
} |
745 |
< |
catch (Exception ex) { |
746 |
< |
unexpectedException(); |
747 |
< |
} |
829 |
> |
t.start(); |
830 |
> |
Thread.sleep(SHORT_DELAY_MS); |
831 |
> |
lock.writeLock().lock(); |
832 |
> |
c.signal(); |
833 |
> |
lock.writeLock().unlock(); |
834 |
> |
t.join(SHORT_DELAY_MS); |
835 |
> |
assertFalse(t.isAlive()); |
836 |
|
} |
837 |
|
|
838 |
|
/** A helper class for uninterruptible wait tests */ |
839 |
|
class UninterruptableThread extends Thread { |
840 |
|
private Lock lock; |
841 |
|
private Condition c; |
842 |
< |
|
842 |
> |
|
843 |
|
public volatile boolean canAwake = false; |
844 |
|
public volatile boolean interrupted = false; |
845 |
|
public volatile boolean lockStarted = false; |
846 |
< |
|
846 |
> |
|
847 |
|
public UninterruptableThread(Lock lock, Condition c) { |
848 |
|
this.lock = lock; |
849 |
|
this.c = c; |
850 |
|
} |
851 |
< |
|
851 |
> |
|
852 |
|
public synchronized void run() { |
853 |
|
lock.lock(); |
854 |
|
lockStarted = true; |
855 |
< |
|
855 |
> |
|
856 |
|
while (!canAwake) { |
857 |
|
c.awaitUninterruptibly(); |
858 |
|
} |
859 |
< |
|
859 |
> |
|
860 |
|
interrupted = isInterrupted(); |
861 |
|
lock.unlock(); |
862 |
|
} |
865 |
|
/** |
866 |
|
* awaitUninterruptibly doesn't abort on interrupt |
867 |
|
*/ |
868 |
< |
public void testAwaitUninterruptibly() { |
868 |
> |
public void testAwaitUninterruptibly() throws InterruptedException { |
869 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
870 |
|
final Condition c = lock.writeLock().newCondition(); |
871 |
|
UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c); |
872 |
|
|
873 |
< |
try { |
786 |
< |
thread.start(); |
787 |
< |
|
788 |
< |
while (!thread.lockStarted) { |
789 |
< |
Thread.sleep(100); |
790 |
< |
} |
873 |
> |
thread.start(); |
874 |
|
|
875 |
< |
lock.writeLock().lock(); |
876 |
< |
try { |
877 |
< |
thread.interrupt(); |
795 |
< |
thread.canAwake = true; |
796 |
< |
c.signal(); |
797 |
< |
} finally { |
798 |
< |
lock.writeLock().unlock(); |
799 |
< |
} |
875 |
> |
while (!thread.lockStarted) { |
876 |
> |
Thread.sleep(100); |
877 |
> |
} |
878 |
|
|
879 |
< |
thread.join(); |
880 |
< |
assertTrue(thread.interrupted); |
881 |
< |
assertFalse(thread.isAlive()); |
882 |
< |
} catch (Exception ex) { |
883 |
< |
unexpectedException(); |
879 |
> |
lock.writeLock().lock(); |
880 |
> |
try { |
881 |
> |
thread.interrupt(); |
882 |
> |
thread.canAwake = true; |
883 |
> |
c.signal(); |
884 |
> |
} finally { |
885 |
> |
lock.writeLock().unlock(); |
886 |
|
} |
887 |
+ |
|
888 |
+ |
thread.join(); |
889 |
+ |
assertTrue(thread.interrupted); |
890 |
+ |
assertFalse(thread.isAlive()); |
891 |
|
} |
892 |
|
|
893 |
|
/** |
894 |
|
* await is interruptible |
895 |
|
*/ |
896 |
< |
public void testAwait_Interrupt() { |
897 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
896 |
> |
public void testAwait_Interrupt() throws InterruptedException { |
897 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
898 |
|
final Condition c = lock.writeLock().newCondition(); |
899 |
< |
Thread t = new Thread(new Runnable() { |
900 |
< |
public void run() { |
901 |
< |
try { |
902 |
< |
lock.writeLock().lock(); |
903 |
< |
c.await(); |
904 |
< |
lock.writeLock().unlock(); |
821 |
< |
threadShouldThrow(); |
822 |
< |
} |
823 |
< |
catch(InterruptedException success) { |
824 |
< |
} |
825 |
< |
} |
826 |
< |
}); |
899 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
900 |
> |
public void realRun() throws InterruptedException { |
901 |
> |
lock.writeLock().lock(); |
902 |
> |
c.await(); |
903 |
> |
lock.writeLock().unlock(); |
904 |
> |
}}); |
905 |
|
|
906 |
< |
try { |
907 |
< |
t.start(); |
908 |
< |
Thread.sleep(SHORT_DELAY_MS); |
909 |
< |
t.interrupt(); |
910 |
< |
t.join(SHORT_DELAY_MS); |
833 |
< |
assertFalse(t.isAlive()); |
834 |
< |
} |
835 |
< |
catch (Exception ex) { |
836 |
< |
unexpectedException(); |
837 |
< |
} |
906 |
> |
t.start(); |
907 |
> |
Thread.sleep(SHORT_DELAY_MS); |
908 |
> |
t.interrupt(); |
909 |
> |
t.join(SHORT_DELAY_MS); |
910 |
> |
assertFalse(t.isAlive()); |
911 |
|
} |
912 |
|
|
913 |
|
/** |
914 |
|
* awaitNanos is interruptible |
915 |
|
*/ |
916 |
< |
public void testAwaitNanos_Interrupt() { |
917 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
916 |
> |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
917 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
918 |
|
final Condition c = lock.writeLock().newCondition(); |
919 |
< |
Thread t = new Thread(new Runnable() { |
920 |
< |
public void run() { |
921 |
< |
try { |
922 |
< |
lock.writeLock().lock(); |
923 |
< |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
924 |
< |
lock.writeLock().unlock(); |
852 |
< |
threadShouldThrow(); |
853 |
< |
} |
854 |
< |
catch(InterruptedException success) { |
855 |
< |
} |
856 |
< |
} |
857 |
< |
}); |
919 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
920 |
> |
public void realRun() throws InterruptedException { |
921 |
> |
lock.writeLock().lock(); |
922 |
> |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
923 |
> |
lock.writeLock().unlock(); |
924 |
> |
}}); |
925 |
|
|
926 |
< |
try { |
927 |
< |
t.start(); |
928 |
< |
Thread.sleep(SHORT_DELAY_MS); |
929 |
< |
t.interrupt(); |
930 |
< |
t.join(SHORT_DELAY_MS); |
864 |
< |
assertFalse(t.isAlive()); |
865 |
< |
} |
866 |
< |
catch (Exception ex) { |
867 |
< |
unexpectedException(); |
868 |
< |
} |
926 |
> |
t.start(); |
927 |
> |
Thread.sleep(SHORT_DELAY_MS); |
928 |
> |
t.interrupt(); |
929 |
> |
t.join(SHORT_DELAY_MS); |
930 |
> |
assertFalse(t.isAlive()); |
931 |
|
} |
932 |
|
|
933 |
|
/** |
934 |
|
* awaitUntil is interruptible |
935 |
|
*/ |
936 |
< |
public void testAwaitUntil_Interrupt() { |
937 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
936 |
> |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
937 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
938 |
|
final Condition c = lock.writeLock().newCondition(); |
939 |
< |
Thread t = new Thread(new Runnable() { |
940 |
< |
public void run() { |
941 |
< |
try { |
942 |
< |
lock.writeLock().lock(); |
943 |
< |
java.util.Date d = new java.util.Date(); |
944 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
945 |
< |
lock.writeLock().unlock(); |
884 |
< |
threadShouldThrow(); |
885 |
< |
} |
886 |
< |
catch(InterruptedException success) { |
887 |
< |
} |
888 |
< |
} |
889 |
< |
}); |
939 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
940 |
> |
public void realRun() throws InterruptedException { |
941 |
> |
lock.writeLock().lock(); |
942 |
> |
java.util.Date d = new java.util.Date(); |
943 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
944 |
> |
lock.writeLock().unlock(); |
945 |
> |
}}); |
946 |
|
|
947 |
< |
try { |
948 |
< |
t.start(); |
949 |
< |
Thread.sleep(SHORT_DELAY_MS); |
950 |
< |
t.interrupt(); |
951 |
< |
t.join(SHORT_DELAY_MS); |
896 |
< |
assertFalse(t.isAlive()); |
897 |
< |
} |
898 |
< |
catch (Exception ex) { |
899 |
< |
unexpectedException(); |
900 |
< |
} |
947 |
> |
t.start(); |
948 |
> |
Thread.sleep(SHORT_DELAY_MS); |
949 |
> |
t.interrupt(); |
950 |
> |
t.join(SHORT_DELAY_MS); |
951 |
> |
assertFalse(t.isAlive()); |
952 |
|
} |
953 |
|
|
954 |
|
/** |
955 |
|
* signalAll wakes up all threads |
956 |
|
*/ |
957 |
< |
public void testSignalAll() { |
958 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
957 |
> |
public void testSignalAll() throws InterruptedException { |
958 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
959 |
|
final Condition c = lock.writeLock().newCondition(); |
960 |
< |
Thread t1 = new Thread(new Runnable() { |
961 |
< |
public void run() { |
962 |
< |
try { |
963 |
< |
lock.writeLock().lock(); |
964 |
< |
c.await(); |
965 |
< |
lock.writeLock().unlock(); |
915 |
< |
} |
916 |
< |
catch(InterruptedException e) { |
917 |
< |
threadUnexpectedException(); |
918 |
< |
} |
919 |
< |
} |
920 |
< |
}); |
960 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
961 |
> |
public void realRun() throws InterruptedException { |
962 |
> |
lock.writeLock().lock(); |
963 |
> |
c.await(); |
964 |
> |
lock.writeLock().unlock(); |
965 |
> |
}}); |
966 |
|
|
967 |
< |
Thread t2 = new Thread(new Runnable() { |
968 |
< |
public void run() { |
969 |
< |
try { |
970 |
< |
lock.writeLock().lock(); |
971 |
< |
c.await(); |
972 |
< |
lock.writeLock().unlock(); |
928 |
< |
} |
929 |
< |
catch(InterruptedException e) { |
930 |
< |
threadUnexpectedException(); |
931 |
< |
} |
932 |
< |
} |
933 |
< |
}); |
967 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
968 |
> |
public void realRun() throws InterruptedException { |
969 |
> |
lock.writeLock().lock(); |
970 |
> |
c.await(); |
971 |
> |
lock.writeLock().unlock(); |
972 |
> |
}}); |
973 |
|
|
974 |
< |
try { |
975 |
< |
t1.start(); |
976 |
< |
t2.start(); |
977 |
< |
Thread.sleep(SHORT_DELAY_MS); |
978 |
< |
lock.writeLock().lock(); |
979 |
< |
c.signalAll(); |
980 |
< |
lock.writeLock().unlock(); |
981 |
< |
t1.join(SHORT_DELAY_MS); |
982 |
< |
t2.join(SHORT_DELAY_MS); |
983 |
< |
assertFalse(t1.isAlive()); |
945 |
< |
assertFalse(t2.isAlive()); |
946 |
< |
} |
947 |
< |
catch (Exception ex) { |
948 |
< |
unexpectedException(); |
949 |
< |
} |
974 |
> |
t1.start(); |
975 |
> |
t2.start(); |
976 |
> |
Thread.sleep(SHORT_DELAY_MS); |
977 |
> |
lock.writeLock().lock(); |
978 |
> |
c.signalAll(); |
979 |
> |
lock.writeLock().unlock(); |
980 |
> |
t1.join(SHORT_DELAY_MS); |
981 |
> |
t2.join(SHORT_DELAY_MS); |
982 |
> |
assertFalse(t1.isAlive()); |
983 |
> |
assertFalse(t2.isAlive()); |
984 |
|
} |
985 |
|
|
986 |
|
/** |
987 |
|
* A serialized lock deserializes as unlocked |
988 |
|
*/ |
989 |
< |
public void testSerialization() { |
989 |
> |
public void testSerialization() throws Exception { |
990 |
|
ReentrantReadWriteLock l = new ReentrantReadWriteLock(); |
991 |
|
l.readLock().lock(); |
992 |
|
l.readLock().unlock(); |
993 |
|
|
994 |
< |
try { |
995 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
996 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
997 |
< |
out.writeObject(l); |
998 |
< |
out.close(); |
999 |
< |
|
1000 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1001 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1002 |
< |
ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject(); |
1003 |
< |
r.readLock().lock(); |
970 |
< |
r.readLock().unlock(); |
971 |
< |
} catch(Exception e){ |
972 |
< |
e.printStackTrace(); |
973 |
< |
unexpectedException(); |
974 |
< |
} |
994 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
995 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
996 |
> |
out.writeObject(l); |
997 |
> |
out.close(); |
998 |
> |
|
999 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1000 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1001 |
> |
ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject(); |
1002 |
> |
r.readLock().lock(); |
1003 |
> |
r.readLock().unlock(); |
1004 |
|
} |
1005 |
|
|
1006 |
|
/** |
1007 |
|
* hasQueuedThreads reports whether there are waiting threads |
1008 |
|
*/ |
1009 |
< |
public void testhasQueuedThreads() { |
1009 |
> |
public void testhasQueuedThreads() throws InterruptedException { |
1010 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1011 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1012 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1013 |
< |
try { |
1014 |
< |
assertFalse(lock.hasQueuedThreads()); |
1015 |
< |
lock.writeLock().lock(); |
1016 |
< |
t1.start(); |
1017 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1018 |
< |
assertTrue(lock.hasQueuedThreads()); |
1019 |
< |
t2.start(); |
1020 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1021 |
< |
assertTrue(lock.hasQueuedThreads()); |
1022 |
< |
t1.interrupt(); |
1023 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1024 |
< |
assertTrue(lock.hasQueuedThreads()); |
1025 |
< |
lock.writeLock().unlock(); |
1026 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1027 |
< |
assertFalse(lock.hasQueuedThreads()); |
1028 |
< |
t1.join(); |
1029 |
< |
t2.join(); |
1001 |
< |
} catch(Exception e){ |
1002 |
< |
unexpectedException(); |
1003 |
< |
} |
1004 |
< |
} |
1013 |
> |
assertFalse(lock.hasQueuedThreads()); |
1014 |
> |
lock.writeLock().lock(); |
1015 |
> |
t1.start(); |
1016 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1017 |
> |
assertTrue(lock.hasQueuedThreads()); |
1018 |
> |
t2.start(); |
1019 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1020 |
> |
assertTrue(lock.hasQueuedThreads()); |
1021 |
> |
t1.interrupt(); |
1022 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1023 |
> |
assertTrue(lock.hasQueuedThreads()); |
1024 |
> |
lock.writeLock().unlock(); |
1025 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1026 |
> |
assertFalse(lock.hasQueuedThreads()); |
1027 |
> |
t1.join(); |
1028 |
> |
t2.join(); |
1029 |
> |
} |
1030 |
|
|
1031 |
|
/** |
1032 |
|
* hasQueuedThread(null) throws NPE |
1033 |
|
*/ |
1034 |
< |
public void testHasQueuedThreadNPE() { |
1034 |
> |
public void testHasQueuedThreadNPE() { |
1035 |
|
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1036 |
|
try { |
1037 |
|
sync.hasQueuedThread(null); |
1038 |
|
shouldThrow(); |
1039 |
< |
} catch (NullPointerException success) { |
1015 |
< |
} |
1039 |
> |
} catch (NullPointerException success) {} |
1040 |
|
} |
1041 |
|
|
1042 |
|
/** |
1043 |
|
* hasQueuedThread reports whether a thread is queued. |
1044 |
|
*/ |
1045 |
< |
public void testHasQueuedThread() { |
1045 |
> |
public void testHasQueuedThread() throws InterruptedException { |
1046 |
|
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1047 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
1048 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
1049 |
< |
try { |
1050 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
1051 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
1052 |
< |
sync.writeLock().lock(); |
1053 |
< |
t1.start(); |
1054 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1055 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
1056 |
< |
t2.start(); |
1057 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1058 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
1059 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
1060 |
< |
t1.interrupt(); |
1061 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1062 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
1063 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
1064 |
< |
sync.writeLock().unlock(); |
1065 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1066 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
1067 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1068 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
1069 |
< |
t1.join(); |
1070 |
< |
t2.join(); |
1047 |
< |
} catch(Exception e){ |
1048 |
< |
unexpectedException(); |
1049 |
< |
} |
1050 |
< |
} |
1049 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
1050 |
> |
assertFalse(sync.hasQueuedThread(t2)); |
1051 |
> |
sync.writeLock().lock(); |
1052 |
> |
t1.start(); |
1053 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1054 |
> |
assertTrue(sync.hasQueuedThread(t1)); |
1055 |
> |
t2.start(); |
1056 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1057 |
> |
assertTrue(sync.hasQueuedThread(t1)); |
1058 |
> |
assertTrue(sync.hasQueuedThread(t2)); |
1059 |
> |
t1.interrupt(); |
1060 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1061 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
1062 |
> |
assertTrue(sync.hasQueuedThread(t2)); |
1063 |
> |
sync.writeLock().unlock(); |
1064 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1065 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
1066 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1067 |
> |
assertFalse(sync.hasQueuedThread(t2)); |
1068 |
> |
t1.join(); |
1069 |
> |
t2.join(); |
1070 |
> |
} |
1071 |
|
|
1072 |
|
|
1073 |
|
/** |
1074 |
|
* getQueueLength reports number of waiting threads |
1075 |
|
*/ |
1076 |
< |
public void testGetQueueLength() { |
1076 |
> |
public void testGetQueueLength() throws InterruptedException { |
1077 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1078 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1079 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1080 |
< |
try { |
1081 |
< |
assertEquals(0, lock.getQueueLength()); |
1082 |
< |
lock.writeLock().lock(); |
1083 |
< |
t1.start(); |
1084 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1085 |
< |
assertEquals(1, lock.getQueueLength()); |
1086 |
< |
t2.start(); |
1087 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1088 |
< |
assertEquals(2, lock.getQueueLength()); |
1089 |
< |
t1.interrupt(); |
1090 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1091 |
< |
assertEquals(1, lock.getQueueLength()); |
1092 |
< |
lock.writeLock().unlock(); |
1093 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1094 |
< |
assertEquals(0, lock.getQueueLength()); |
1095 |
< |
t1.join(); |
1096 |
< |
t2.join(); |
1077 |
< |
} catch(Exception e){ |
1078 |
< |
unexpectedException(); |
1079 |
< |
} |
1080 |
< |
} |
1080 |
> |
assertEquals(0, lock.getQueueLength()); |
1081 |
> |
lock.writeLock().lock(); |
1082 |
> |
t1.start(); |
1083 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1084 |
> |
assertEquals(1, lock.getQueueLength()); |
1085 |
> |
t2.start(); |
1086 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1087 |
> |
assertEquals(2, lock.getQueueLength()); |
1088 |
> |
t1.interrupt(); |
1089 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1090 |
> |
assertEquals(1, lock.getQueueLength()); |
1091 |
> |
lock.writeLock().unlock(); |
1092 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1093 |
> |
assertEquals(0, lock.getQueueLength()); |
1094 |
> |
t1.join(); |
1095 |
> |
t2.join(); |
1096 |
> |
} |
1097 |
|
|
1098 |
|
/** |
1099 |
|
* getQueuedThreads includes waiting threads |
1100 |
|
*/ |
1101 |
< |
public void testGetQueuedThreads() { |
1101 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
1102 |
|
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1103 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1104 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1105 |
< |
try { |
1106 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1107 |
< |
lock.writeLock().lock(); |
1108 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1109 |
< |
t1.start(); |
1110 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1111 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1112 |
< |
t2.start(); |
1113 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1114 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1115 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1116 |
< |
t1.interrupt(); |
1117 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1118 |
< |
assertFalse(lock.getQueuedThreads().contains(t1)); |
1119 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1120 |
< |
lock.writeLock().unlock(); |
1121 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1122 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1123 |
< |
t1.join(); |
1124 |
< |
t2.join(); |
1109 |
< |
} catch(Exception e){ |
1110 |
< |
unexpectedException(); |
1111 |
< |
} |
1112 |
< |
} |
1105 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1106 |
> |
lock.writeLock().lock(); |
1107 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1108 |
> |
t1.start(); |
1109 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1110 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1111 |
> |
t2.start(); |
1112 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1113 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1114 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1115 |
> |
t1.interrupt(); |
1116 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1117 |
> |
assertFalse(lock.getQueuedThreads().contains(t1)); |
1118 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1119 |
> |
lock.writeLock().unlock(); |
1120 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1121 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1122 |
> |
t1.join(); |
1123 |
> |
t2.join(); |
1124 |
> |
} |
1125 |
|
|
1126 |
|
/** |
1127 |
|
* hasWaiters throws NPE if null |
1131 |
|
try { |
1132 |
|
lock.hasWaiters(null); |
1133 |
|
shouldThrow(); |
1134 |
< |
} catch (NullPointerException success) { |
1123 |
< |
} catch (Exception ex) { |
1124 |
< |
unexpectedException(); |
1125 |
< |
} |
1134 |
> |
} catch (NullPointerException success) {} |
1135 |
|
} |
1136 |
|
|
1137 |
|
/** |
1142 |
|
try { |
1143 |
|
lock.getWaitQueueLength(null); |
1144 |
|
shouldThrow(); |
1145 |
< |
} catch (NullPointerException success) { |
1137 |
< |
} catch (Exception ex) { |
1138 |
< |
unexpectedException(); |
1139 |
< |
} |
1145 |
> |
} catch (NullPointerException success) {} |
1146 |
|
} |
1147 |
|
|
1148 |
|
|
1154 |
|
try { |
1155 |
|
lock.getWaitingThreads(null); |
1156 |
|
shouldThrow(); |
1157 |
< |
} catch (NullPointerException success) { |
1152 |
< |
} catch (Exception ex) { |
1153 |
< |
unexpectedException(); |
1154 |
< |
} |
1157 |
> |
} catch (NullPointerException success) {} |
1158 |
|
} |
1159 |
|
|
1160 |
|
/** |
1162 |
|
*/ |
1163 |
|
public void testHasWaitersIAE() { |
1164 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1165 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1165 |
> |
final Condition c = lock.writeLock().newCondition(); |
1166 |
|
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1167 |
|
try { |
1168 |
|
lock2.hasWaiters(c); |
1169 |
|
shouldThrow(); |
1170 |
< |
} catch (IllegalArgumentException success) { |
1168 |
< |
} catch (Exception ex) { |
1169 |
< |
unexpectedException(); |
1170 |
< |
} |
1170 |
> |
} catch (IllegalArgumentException success) {} |
1171 |
|
} |
1172 |
|
|
1173 |
|
/** |
1175 |
|
*/ |
1176 |
|
public void testHasWaitersIMSE() { |
1177 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1178 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1178 |
> |
final Condition c = lock.writeLock().newCondition(); |
1179 |
|
try { |
1180 |
|
lock.hasWaiters(c); |
1181 |
|
shouldThrow(); |
1182 |
< |
} catch (IllegalMonitorStateException success) { |
1183 |
< |
} catch (Exception ex) { |
1184 |
< |
unexpectedException(); |
1185 |
< |
} |
1182 |
> |
} catch (IllegalMonitorStateException success) {} |
1183 |
|
} |
1184 |
|
|
1185 |
|
|
1188 |
|
*/ |
1189 |
|
public void testGetWaitQueueLengthIAE() { |
1190 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1191 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1191 |
> |
final Condition c = lock.writeLock().newCondition(); |
1192 |
|
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1193 |
|
try { |
1194 |
|
lock2.getWaitQueueLength(c); |
1195 |
|
shouldThrow(); |
1196 |
< |
} catch (IllegalArgumentException success) { |
1200 |
< |
} catch (Exception ex) { |
1201 |
< |
unexpectedException(); |
1202 |
< |
} |
1196 |
> |
} catch (IllegalArgumentException success) {} |
1197 |
|
} |
1198 |
|
|
1199 |
|
/** |
1201 |
|
*/ |
1202 |
|
public void testGetWaitQueueLengthIMSE() { |
1203 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1204 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1204 |
> |
final Condition c = lock.writeLock().newCondition(); |
1205 |
|
try { |
1206 |
|
lock.getWaitQueueLength(c); |
1207 |
|
shouldThrow(); |
1208 |
< |
} catch (IllegalMonitorStateException success) { |
1215 |
< |
} catch (Exception ex) { |
1216 |
< |
unexpectedException(); |
1217 |
< |
} |
1208 |
> |
} catch (IllegalMonitorStateException success) {} |
1209 |
|
} |
1210 |
|
|
1211 |
|
|
1213 |
|
* getWaitingThreads throws IAE if not owned |
1214 |
|
*/ |
1215 |
|
public void testGetWaitingThreadsIAE() { |
1216 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1217 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1218 |
< |
final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); |
1216 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1217 |
> |
final Condition c = lock.writeLock().newCondition(); |
1218 |
> |
final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); |
1219 |
|
try { |
1220 |
|
lock2.getWaitingThreads(c); |
1221 |
|
shouldThrow(); |
1222 |
< |
} catch (IllegalArgumentException success) { |
1232 |
< |
} catch (Exception ex) { |
1233 |
< |
unexpectedException(); |
1234 |
< |
} |
1222 |
> |
} catch (IllegalArgumentException success) {} |
1223 |
|
} |
1224 |
|
|
1225 |
|
/** |
1226 |
|
* getWaitingThreads throws IMSE if not locked |
1227 |
|
*/ |
1228 |
|
public void testGetWaitingThreadsIMSE() { |
1229 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1230 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1229 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1230 |
> |
final Condition c = lock.writeLock().newCondition(); |
1231 |
|
try { |
1232 |
|
lock.getWaitingThreads(c); |
1233 |
|
shouldThrow(); |
1234 |
< |
} catch (IllegalMonitorStateException success) { |
1247 |
< |
} catch (Exception ex) { |
1248 |
< |
unexpectedException(); |
1249 |
< |
} |
1234 |
> |
} catch (IllegalMonitorStateException success) {} |
1235 |
|
} |
1236 |
|
|
1237 |
|
|
1238 |
|
/** |
1239 |
|
* hasWaiters returns true when a thread is waiting, else false |
1240 |
|
*/ |
1241 |
< |
public void testHasWaiters() { |
1241 |
> |
public void testHasWaiters() throws InterruptedException { |
1242 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1243 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1244 |
< |
Thread t = new Thread(new Runnable() { |
1245 |
< |
public void run() { |
1246 |
< |
try { |
1247 |
< |
lock.writeLock().lock(); |
1248 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1249 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1250 |
< |
c.await(); |
1251 |
< |
lock.writeLock().unlock(); |
1267 |
< |
} |
1268 |
< |
catch(InterruptedException e) { |
1269 |
< |
threadUnexpectedException(); |
1270 |
< |
} |
1271 |
< |
} |
1272 |
< |
}); |
1243 |
> |
final Condition c = lock.writeLock().newCondition(); |
1244 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1245 |
> |
public void realRun() throws InterruptedException { |
1246 |
> |
lock.writeLock().lock(); |
1247 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
1248 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1249 |
> |
c.await(); |
1250 |
> |
lock.writeLock().unlock(); |
1251 |
> |
}}); |
1252 |
|
|
1253 |
< |
try { |
1254 |
< |
t.start(); |
1255 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1256 |
< |
lock.writeLock().lock(); |
1257 |
< |
assertTrue(lock.hasWaiters(c)); |
1258 |
< |
assertEquals(1, lock.getWaitQueueLength(c)); |
1259 |
< |
c.signal(); |
1260 |
< |
lock.writeLock().unlock(); |
1261 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1262 |
< |
lock.writeLock().lock(); |
1263 |
< |
assertFalse(lock.hasWaiters(c)); |
1264 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
1265 |
< |
lock.writeLock().unlock(); |
1266 |
< |
t.join(SHORT_DELAY_MS); |
1288 |
< |
assertFalse(t.isAlive()); |
1289 |
< |
} |
1290 |
< |
catch (Exception ex) { |
1291 |
< |
unexpectedException(); |
1292 |
< |
} |
1253 |
> |
t.start(); |
1254 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1255 |
> |
lock.writeLock().lock(); |
1256 |
> |
assertTrue(lock.hasWaiters(c)); |
1257 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
1258 |
> |
c.signal(); |
1259 |
> |
lock.writeLock().unlock(); |
1260 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1261 |
> |
lock.writeLock().lock(); |
1262 |
> |
assertFalse(lock.hasWaiters(c)); |
1263 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1264 |
> |
lock.writeLock().unlock(); |
1265 |
> |
t.join(SHORT_DELAY_MS); |
1266 |
> |
assertFalse(t.isAlive()); |
1267 |
|
} |
1268 |
|
|
1269 |
|
/** |
1270 |
|
* getWaitQueueLength returns number of waiting threads |
1271 |
|
*/ |
1272 |
< |
public void testGetWaitQueueLength() { |
1272 |
> |
public void testGetWaitQueueLength() throws InterruptedException { |
1273 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1274 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1275 |
< |
Thread t = new Thread(new Runnable() { |
1276 |
< |
public void run() { |
1277 |
< |
try { |
1278 |
< |
lock.writeLock().lock(); |
1279 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1280 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1281 |
< |
c.await(); |
1282 |
< |
lock.writeLock().unlock(); |
1309 |
< |
} |
1310 |
< |
catch(InterruptedException e) { |
1311 |
< |
threadUnexpectedException(); |
1312 |
< |
} |
1313 |
< |
} |
1314 |
< |
}); |
1274 |
> |
final Condition c = lock.writeLock().newCondition(); |
1275 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1276 |
> |
public void realRun() throws InterruptedException { |
1277 |
> |
lock.writeLock().lock(); |
1278 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
1279 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1280 |
> |
c.await(); |
1281 |
> |
lock.writeLock().unlock(); |
1282 |
> |
}}); |
1283 |
|
|
1284 |
< |
try { |
1285 |
< |
t.start(); |
1286 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1287 |
< |
lock.writeLock().lock(); |
1288 |
< |
assertTrue(lock.hasWaiters(c)); |
1289 |
< |
assertEquals(1, lock.getWaitQueueLength(c)); |
1290 |
< |
c.signal(); |
1291 |
< |
lock.writeLock().unlock(); |
1292 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1293 |
< |
lock.writeLock().lock(); |
1294 |
< |
assertFalse(lock.hasWaiters(c)); |
1295 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
1296 |
< |
lock.writeLock().unlock(); |
1297 |
< |
t.join(SHORT_DELAY_MS); |
1330 |
< |
assertFalse(t.isAlive()); |
1331 |
< |
} |
1332 |
< |
catch (Exception ex) { |
1333 |
< |
unexpectedException(); |
1334 |
< |
} |
1284 |
> |
t.start(); |
1285 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1286 |
> |
lock.writeLock().lock(); |
1287 |
> |
assertTrue(lock.hasWaiters(c)); |
1288 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
1289 |
> |
c.signal(); |
1290 |
> |
lock.writeLock().unlock(); |
1291 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1292 |
> |
lock.writeLock().lock(); |
1293 |
> |
assertFalse(lock.hasWaiters(c)); |
1294 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1295 |
> |
lock.writeLock().unlock(); |
1296 |
> |
t.join(SHORT_DELAY_MS); |
1297 |
> |
assertFalse(t.isAlive()); |
1298 |
|
} |
1299 |
|
|
1300 |
|
|
1301 |
|
/** |
1302 |
|
* getWaitingThreads returns only and all waiting threads |
1303 |
|
*/ |
1304 |
< |
public void testGetWaitingThreads() { |
1305 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1304 |
> |
public void testGetWaitingThreads() throws InterruptedException { |
1305 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1306 |
|
final Condition c = lock.writeLock().newCondition(); |
1307 |
< |
Thread t1 = new Thread(new Runnable() { |
1308 |
< |
public void run() { |
1309 |
< |
try { |
1310 |
< |
lock.writeLock().lock(); |
1311 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
1312 |
< |
c.await(); |
1313 |
< |
lock.writeLock().unlock(); |
1351 |
< |
} |
1352 |
< |
catch(InterruptedException e) { |
1353 |
< |
threadUnexpectedException(); |
1354 |
< |
} |
1355 |
< |
} |
1356 |
< |
}); |
1307 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
1308 |
> |
public void realRun() throws InterruptedException { |
1309 |
> |
lock.writeLock().lock(); |
1310 |
> |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
1311 |
> |
c.await(); |
1312 |
> |
lock.writeLock().unlock(); |
1313 |
> |
}}); |
1314 |
|
|
1315 |
< |
Thread t2 = new Thread(new Runnable() { |
1316 |
< |
public void run() { |
1317 |
< |
try { |
1318 |
< |
lock.writeLock().lock(); |
1319 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
1320 |
< |
c.await(); |
1321 |
< |
lock.writeLock().unlock(); |
1365 |
< |
} |
1366 |
< |
catch(InterruptedException e) { |
1367 |
< |
threadUnexpectedException(); |
1368 |
< |
} |
1369 |
< |
} |
1370 |
< |
}); |
1315 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
1316 |
> |
public void realRun() throws InterruptedException { |
1317 |
> |
lock.writeLock().lock(); |
1318 |
> |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
1319 |
> |
c.await(); |
1320 |
> |
lock.writeLock().unlock(); |
1321 |
> |
}}); |
1322 |
|
|
1323 |
< |
try { |
1324 |
< |
lock.writeLock().lock(); |
1325 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1326 |
< |
lock.writeLock().unlock(); |
1327 |
< |
t1.start(); |
1328 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1329 |
< |
t2.start(); |
1330 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1331 |
< |
lock.writeLock().lock(); |
1332 |
< |
assertTrue(lock.hasWaiters(c)); |
1333 |
< |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
1334 |
< |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
1335 |
< |
c.signalAll(); |
1336 |
< |
lock.writeLock().unlock(); |
1337 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1338 |
< |
lock.writeLock().lock(); |
1339 |
< |
assertFalse(lock.hasWaiters(c)); |
1340 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1341 |
< |
lock.writeLock().unlock(); |
1342 |
< |
t1.join(SHORT_DELAY_MS); |
1343 |
< |
t2.join(SHORT_DELAY_MS); |
1344 |
< |
assertFalse(t1.isAlive()); |
1394 |
< |
assertFalse(t2.isAlive()); |
1395 |
< |
} |
1396 |
< |
catch (Exception ex) { |
1397 |
< |
unexpectedException(); |
1398 |
< |
} |
1323 |
> |
lock.writeLock().lock(); |
1324 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1325 |
> |
lock.writeLock().unlock(); |
1326 |
> |
t1.start(); |
1327 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1328 |
> |
t2.start(); |
1329 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1330 |
> |
lock.writeLock().lock(); |
1331 |
> |
assertTrue(lock.hasWaiters(c)); |
1332 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
1333 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
1334 |
> |
c.signalAll(); |
1335 |
> |
lock.writeLock().unlock(); |
1336 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1337 |
> |
lock.writeLock().lock(); |
1338 |
> |
assertFalse(lock.hasWaiters(c)); |
1339 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1340 |
> |
lock.writeLock().unlock(); |
1341 |
> |
t1.join(SHORT_DELAY_MS); |
1342 |
> |
t2.join(SHORT_DELAY_MS); |
1343 |
> |
assertFalse(t1.isAlive()); |
1344 |
> |
assertFalse(t2.isAlive()); |
1345 |
|
} |
1346 |
|
|
1347 |
|
/** |