1 |
|
/* |
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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
9 |
|
import junit.framework.*; |
10 |
|
import java.util.concurrent.locks.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.io.*; |
14 |
|
import java.util.*; |
15 |
|
|
16 |
|
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(ReentrantReadWriteLockTest.class); |
21 |
> |
return new TestSuite(ReentrantReadWriteLockTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
/** |
62 |
|
* Constructor sets given fairness, and is in unlocked state |
63 |
|
*/ |
64 |
|
public void testConstructor() { |
65 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
65 |
> |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
66 |
|
assertFalse(rl.isFair()); |
67 |
|
assertFalse(rl.isWriteLocked()); |
68 |
|
assertEquals(0, rl.getReadLockCount()); |
69 |
< |
ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true); |
69 |
> |
ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true); |
70 |
|
assertTrue(r2.isFair()); |
71 |
|
assertFalse(r2.isWriteLocked()); |
72 |
|
assertEquals(0, r2.getReadLockCount()); |
73 |
< |
ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false); |
73 |
> |
ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false); |
74 |
|
assertFalse(r3.isFair()); |
75 |
|
assertFalse(r3.isWriteLocked()); |
76 |
|
assertEquals(0, r3.getReadLockCount()); |
80 |
|
* write-locking and read-locking an unlocked lock succeed |
81 |
|
*/ |
82 |
|
public void testLock() { |
83 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
83 |
> |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
84 |
|
rl.writeLock().lock(); |
85 |
|
assertTrue(rl.isWriteLocked()); |
86 |
|
assertTrue(rl.isWriteLockedByCurrentThread()); |
106 |
|
* locking an unlocked fair lock succeeds |
107 |
|
*/ |
108 |
|
public void testFairLock() { |
109 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true); |
109 |
> |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true); |
110 |
|
rl.writeLock().lock(); |
111 |
|
assertTrue(rl.isWriteLocked()); |
112 |
|
assertTrue(rl.isWriteLockedByCurrentThread()); |
131 |
|
* getWriteHoldCount returns number of recursive holds |
132 |
|
*/ |
133 |
|
public void testGetWriteHoldCount() { |
134 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
135 |
< |
for (int i = 1; i <= SIZE; i++) { |
136 |
< |
lock.writeLock().lock(); |
137 |
< |
assertEquals(i,lock.getWriteHoldCount()); |
138 |
< |
} |
139 |
< |
for (int i = SIZE; i > 0; i--) { |
140 |
< |
lock.writeLock().unlock(); |
141 |
< |
assertEquals(i-1,lock.getWriteHoldCount()); |
142 |
< |
} |
134 |
> |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
135 |
> |
for (int i = 1; i <= SIZE; i++) { |
136 |
> |
lock.writeLock().lock(); |
137 |
> |
assertEquals(i,lock.getWriteHoldCount()); |
138 |
> |
} |
139 |
> |
for (int i = SIZE; i > 0; i--) { |
140 |
> |
lock.writeLock().unlock(); |
141 |
> |
assertEquals(i-1,lock.getWriteHoldCount()); |
142 |
> |
} |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
|
* WriteLock.getHoldCount returns number of recursive holds |
147 |
|
*/ |
148 |
|
public void testGetHoldCount() { |
149 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
150 |
< |
for (int i = 1; i <= SIZE; i++) { |
151 |
< |
lock.writeLock().lock(); |
152 |
< |
assertEquals(i,lock.writeLock().getHoldCount()); |
153 |
< |
} |
154 |
< |
for (int i = SIZE; i > 0; i--) { |
155 |
< |
lock.writeLock().unlock(); |
156 |
< |
assertEquals(i-1,lock.writeLock().getHoldCount()); |
157 |
< |
} |
149 |
> |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
150 |
> |
for (int i = 1; i <= SIZE; i++) { |
151 |
> |
lock.writeLock().lock(); |
152 |
> |
assertEquals(i,lock.writeLock().getHoldCount()); |
153 |
> |
} |
154 |
> |
for (int i = SIZE; i > 0; i--) { |
155 |
> |
lock.writeLock().unlock(); |
156 |
> |
assertEquals(i-1,lock.writeLock().getHoldCount()); |
157 |
> |
} |
158 |
|
} |
159 |
|
|
160 |
|
/** |
161 |
|
* getReadHoldCount returns number of recursive holds |
162 |
|
*/ |
163 |
|
public void testGetReadHoldCount() { |
164 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
165 |
< |
for (int i = 1; i <= SIZE; i++) { |
166 |
< |
lock.readLock().lock(); |
167 |
< |
assertEquals(i,lock.getReadHoldCount()); |
168 |
< |
} |
169 |
< |
for (int i = SIZE; i > 0; i--) { |
170 |
< |
lock.readLock().unlock(); |
171 |
< |
assertEquals(i-1,lock.getReadHoldCount()); |
172 |
< |
} |
164 |
> |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
165 |
> |
for (int i = 1; i <= SIZE; i++) { |
166 |
> |
lock.readLock().lock(); |
167 |
> |
assertEquals(i,lock.getReadHoldCount()); |
168 |
> |
} |
169 |
> |
for (int i = SIZE; i > 0; i--) { |
170 |
> |
lock.readLock().unlock(); |
171 |
> |
assertEquals(i-1,lock.getReadHoldCount()); |
172 |
> |
} |
173 |
|
} |
174 |
|
|
175 |
|
|
177 |
|
* write-unlocking an unlocked lock throws IllegalMonitorStateException |
178 |
|
*/ |
179 |
|
public void testUnlock_IllegalMonitorStateException() { |
180 |
< |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
181 |
< |
try { |
182 |
< |
rl.writeLock().unlock(); |
183 |
< |
shouldThrow(); |
184 |
< |
} catch (IllegalMonitorStateException success) {} |
180 |
> |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
181 |
> |
try { |
182 |
> |
rl.writeLock().unlock(); |
183 |
> |
shouldThrow(); |
184 |
> |
} catch (IllegalMonitorStateException success) {} |
185 |
|
} |
186 |
|
|
187 |
|
|
189 |
|
* write-lockInterruptibly is interruptible |
190 |
|
*/ |
191 |
|
public void testWriteLockInterruptibly_Interrupted() throws Exception { |
192 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
193 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
192 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
193 |
> |
lock.writeLock().lock(); |
194 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
195 |
|
public void realRun() throws InterruptedException { |
196 |
|
lock.writeLock().lockInterruptibly(); |
197 |
|
lock.writeLock().unlock(); |
199 |
|
lock.writeLock().unlock(); |
200 |
|
}}); |
201 |
|
|
200 |
– |
lock.writeLock().lock(); |
201 |
– |
t.start(); |
202 |
|
Thread.sleep(SHORT_DELAY_MS); |
203 |
|
t.interrupt(); |
204 |
|
Thread.sleep(SHORT_DELAY_MS); |
210 |
|
* timed write-tryLock is interruptible |
211 |
|
*/ |
212 |
|
public void testWriteTryLock_Interrupted() throws InterruptedException { |
213 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
214 |
< |
lock.writeLock().lock(); |
215 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
213 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
214 |
> |
lock.writeLock().lock(); |
215 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
216 |
|
public void realRun() throws InterruptedException { |
217 |
< |
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
217 |
> |
lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS); |
218 |
|
}}); |
219 |
|
|
220 |
< |
t.start(); |
220 |
> |
Thread.sleep(SHORT_DELAY_MS); |
221 |
|
t.interrupt(); |
222 |
|
lock.writeLock().unlock(); |
223 |
|
t.join(); |
227 |
|
* read-lockInterruptibly is interruptible |
228 |
|
*/ |
229 |
|
public void testReadLockInterruptibly_Interrupted() throws InterruptedException { |
230 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
231 |
< |
lock.writeLock().lock(); |
232 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
230 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
231 |
> |
lock.writeLock().lock(); |
232 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
233 |
|
public void realRun() throws InterruptedException { |
234 |
|
lock.readLock().lockInterruptibly(); |
235 |
|
}}); |
236 |
|
|
237 |
– |
t.start(); |
237 |
|
Thread.sleep(SHORT_DELAY_MS); |
238 |
|
t.interrupt(); |
239 |
|
Thread.sleep(SHORT_DELAY_MS); |
245 |
|
* timed read-tryLock is interruptible |
246 |
|
*/ |
247 |
|
public void testReadTryLock_Interrupted() throws InterruptedException { |
248 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
249 |
< |
lock.writeLock().lock(); |
250 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
248 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
249 |
> |
lock.writeLock().lock(); |
250 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
251 |
|
public void realRun() throws InterruptedException { |
252 |
< |
lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS); |
252 |
> |
lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS); |
253 |
|
}}); |
254 |
|
|
255 |
< |
t.start(); |
255 |
> |
Thread.sleep(SHORT_DELAY_MS); |
256 |
|
t.interrupt(); |
257 |
|
t.join(); |
258 |
|
} |
262 |
|
* write-tryLock fails if locked |
263 |
|
*/ |
264 |
|
public void testWriteTryLockWhenLocked() throws InterruptedException { |
265 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
266 |
< |
lock.writeLock().lock(); |
267 |
< |
Thread t = new Thread(new Runnable() { |
268 |
< |
public void run() { |
269 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
270 |
< |
} |
272 |
< |
}); |
265 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
266 |
> |
lock.writeLock().lock(); |
267 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
268 |
> |
public void realRun() { |
269 |
> |
assertFalse(lock.writeLock().tryLock()); |
270 |
> |
}}); |
271 |
|
|
274 |
– |
t.start(); |
272 |
|
t.join(); |
273 |
|
lock.writeLock().unlock(); |
274 |
|
} |
277 |
|
* read-tryLock fails if locked |
278 |
|
*/ |
279 |
|
public void testReadTryLockWhenLocked() throws InterruptedException { |
280 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
281 |
< |
lock.writeLock().lock(); |
282 |
< |
Thread t = new Thread(new Runnable() { |
283 |
< |
public void run() { |
284 |
< |
threadAssertFalse(lock.readLock().tryLock()); |
285 |
< |
} |
289 |
< |
}); |
280 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
281 |
> |
lock.writeLock().lock(); |
282 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
283 |
> |
public void realRun() { |
284 |
> |
assertFalse(lock.readLock().tryLock()); |
285 |
> |
}}); |
286 |
|
|
291 |
– |
t.start(); |
287 |
|
t.join(); |
288 |
|
lock.writeLock().unlock(); |
289 |
|
} |
292 |
|
* Multiple threads can hold a read lock when not write-locked |
293 |
|
*/ |
294 |
|
public void testMultipleReadLocks() throws InterruptedException { |
295 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
296 |
< |
lock.readLock().lock(); |
297 |
< |
Thread t = new Thread(new Runnable() { |
298 |
< |
public void run() { |
299 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
300 |
< |
lock.readLock().unlock(); |
301 |
< |
} |
307 |
< |
}); |
295 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
296 |
> |
lock.readLock().lock(); |
297 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
298 |
> |
public void realRun() { |
299 |
> |
assertTrue(lock.readLock().tryLock()); |
300 |
> |
lock.readLock().unlock(); |
301 |
> |
}}); |
302 |
|
|
309 |
– |
t.start(); |
303 |
|
t.join(); |
304 |
|
lock.readLock().unlock(); |
305 |
|
} |
308 |
|
* A writelock succeeds after reading threads unlock |
309 |
|
*/ |
310 |
|
public void testWriteAfterMultipleReadLocks() throws InterruptedException { |
311 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
312 |
< |
lock.readLock().lock(); |
313 |
< |
Thread t1 = new Thread(new Runnable() { |
314 |
< |
public void run() { |
315 |
< |
lock.readLock().lock(); |
316 |
< |
lock.readLock().unlock(); |
317 |
< |
} |
318 |
< |
}); |
319 |
< |
Thread t2 = new Thread(new Runnable() { |
320 |
< |
public void run() { |
321 |
< |
lock.writeLock().lock(); |
322 |
< |
lock.writeLock().unlock(); |
330 |
< |
} |
331 |
< |
}); |
311 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
312 |
> |
lock.readLock().lock(); |
313 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
314 |
> |
public void realRun() { |
315 |
> |
lock.readLock().lock(); |
316 |
> |
lock.readLock().unlock(); |
317 |
> |
}}); |
318 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
319 |
> |
public void realRun() { |
320 |
> |
lock.writeLock().lock(); |
321 |
> |
lock.writeLock().unlock(); |
322 |
> |
}}); |
323 |
|
|
333 |
– |
t1.start(); |
334 |
– |
t2.start(); |
324 |
|
Thread.sleep(SHORT_DELAY_MS); |
325 |
|
lock.readLock().unlock(); |
326 |
|
t1.join(MEDIUM_DELAY_MS); |
333 |
|
* Readlocks succeed after a writing thread unlocks |
334 |
|
*/ |
335 |
|
public void testReadAfterWriteLock() throws InterruptedException { |
336 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
337 |
< |
lock.writeLock().lock(); |
338 |
< |
Thread t1 = new Thread(new Runnable() { |
339 |
< |
public void run() { |
340 |
< |
lock.readLock().lock(); |
341 |
< |
lock.readLock().unlock(); |
342 |
< |
} |
343 |
< |
}); |
344 |
< |
Thread t2 = new Thread(new Runnable() { |
345 |
< |
public void run() { |
346 |
< |
lock.readLock().lock(); |
347 |
< |
lock.readLock().unlock(); |
359 |
< |
} |
360 |
< |
}); |
336 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
337 |
> |
lock.writeLock().lock(); |
338 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
339 |
> |
public void realRun() { |
340 |
> |
lock.readLock().lock(); |
341 |
> |
lock.readLock().unlock(); |
342 |
> |
}}); |
343 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
344 |
> |
public void realRun() { |
345 |
> |
lock.readLock().lock(); |
346 |
> |
lock.readLock().unlock(); |
347 |
> |
}}); |
348 |
|
|
362 |
– |
t1.start(); |
363 |
– |
t2.start(); |
349 |
|
Thread.sleep(SHORT_DELAY_MS); |
350 |
|
lock.writeLock().unlock(); |
351 |
|
t1.join(MEDIUM_DELAY_MS); |
358 |
|
* Read trylock succeeds if write locked by current thread |
359 |
|
*/ |
360 |
|
public void testReadHoldingWriteLock() { |
361 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
362 |
< |
lock.writeLock().lock(); |
361 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
362 |
> |
lock.writeLock().lock(); |
363 |
|
assertTrue(lock.readLock().tryLock()); |
364 |
|
lock.readLock().unlock(); |
365 |
|
lock.writeLock().unlock(); |
370 |
|
* other threads are waiting for readlock |
371 |
|
*/ |
372 |
|
public void testReadHoldingWriteLock2() throws InterruptedException { |
373 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
374 |
< |
lock.writeLock().lock(); |
375 |
< |
Thread t1 = new Thread(new Runnable() { |
376 |
< |
public void run() { |
377 |
< |
lock.readLock().lock(); |
378 |
< |
lock.readLock().unlock(); |
379 |
< |
} |
380 |
< |
}); |
381 |
< |
Thread t2 = new Thread(new Runnable() { |
382 |
< |
public void run() { |
383 |
< |
lock.readLock().lock(); |
384 |
< |
lock.readLock().unlock(); |
400 |
< |
} |
401 |
< |
}); |
373 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
374 |
> |
lock.writeLock().lock(); |
375 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
376 |
> |
public void realRun() { |
377 |
> |
lock.readLock().lock(); |
378 |
> |
lock.readLock().unlock(); |
379 |
> |
}}); |
380 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
381 |
> |
public void realRun() { |
382 |
> |
lock.readLock().lock(); |
383 |
> |
lock.readLock().unlock(); |
384 |
> |
}}); |
385 |
|
|
403 |
– |
t1.start(); |
404 |
– |
t2.start(); |
386 |
|
lock.readLock().lock(); |
387 |
|
lock.readLock().unlock(); |
388 |
|
Thread.sleep(SHORT_DELAY_MS); |
396 |
|
} |
397 |
|
|
398 |
|
/** |
399 |
< |
* Read lock succeeds if write locked by current thread even if |
399 |
> |
* Read lock succeeds if write locked by current thread even if |
400 |
|
* other threads are waiting for writelock |
401 |
|
*/ |
402 |
|
public void testReadHoldingWriteLock3() throws InterruptedException { |
403 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
404 |
< |
lock.writeLock().lock(); |
405 |
< |
Thread t1 = new Thread(new Runnable() { |
406 |
< |
public void run() { |
407 |
< |
lock.writeLock().lock(); |
408 |
< |
lock.writeLock().unlock(); |
409 |
< |
} |
410 |
< |
}); |
411 |
< |
Thread t2 = new Thread(new Runnable() { |
412 |
< |
public void run() { |
413 |
< |
lock.writeLock().lock(); |
414 |
< |
lock.writeLock().unlock(); |
434 |
< |
} |
435 |
< |
}); |
403 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
404 |
> |
lock.writeLock().lock(); |
405 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
406 |
> |
public void realRun() { |
407 |
> |
lock.writeLock().lock(); |
408 |
> |
lock.writeLock().unlock(); |
409 |
> |
}}); |
410 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
411 |
> |
public void realRun() { |
412 |
> |
lock.writeLock().lock(); |
413 |
> |
lock.writeLock().unlock(); |
414 |
> |
}}); |
415 |
|
|
437 |
– |
t1.start(); |
438 |
– |
t2.start(); |
416 |
|
lock.readLock().lock(); |
417 |
|
lock.readLock().unlock(); |
418 |
|
Thread.sleep(SHORT_DELAY_MS); |
427 |
|
|
428 |
|
|
429 |
|
/** |
430 |
< |
* Write lock succeeds if write locked by current thread even if |
430 |
> |
* Write lock succeeds if write locked by current thread even if |
431 |
|
* other threads are waiting for writelock |
432 |
|
*/ |
433 |
|
public void testWriteHoldingWriteLock4() throws InterruptedException { |
434 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
435 |
< |
lock.writeLock().lock(); |
436 |
< |
Thread t1 = new Thread(new Runnable() { |
437 |
< |
public void run() { |
438 |
< |
lock.writeLock().lock(); |
439 |
< |
lock.writeLock().unlock(); |
440 |
< |
} |
441 |
< |
}); |
442 |
< |
Thread t2 = new Thread(new Runnable() { |
443 |
< |
public void run() { |
444 |
< |
lock.writeLock().lock(); |
445 |
< |
lock.writeLock().unlock(); |
469 |
< |
} |
470 |
< |
}); |
434 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
435 |
> |
lock.writeLock().lock(); |
436 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
437 |
> |
public void realRun() { |
438 |
> |
lock.writeLock().lock(); |
439 |
> |
lock.writeLock().unlock(); |
440 |
> |
}}); |
441 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
442 |
> |
public void realRun() { |
443 |
> |
lock.writeLock().lock(); |
444 |
> |
lock.writeLock().unlock(); |
445 |
> |
}}); |
446 |
|
|
472 |
– |
t1.start(); |
473 |
– |
t2.start(); |
447 |
|
lock.writeLock().lock(); |
448 |
|
lock.writeLock().unlock(); |
449 |
|
Thread.sleep(SHORT_DELAY_MS); |
461 |
|
* Fair Read trylock succeeds if write locked by current thread |
462 |
|
*/ |
463 |
|
public void testReadHoldingWriteLockFair() { |
464 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
465 |
< |
lock.writeLock().lock(); |
464 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
465 |
> |
lock.writeLock().lock(); |
466 |
|
assertTrue(lock.readLock().tryLock()); |
467 |
|
lock.readLock().unlock(); |
468 |
|
lock.writeLock().unlock(); |
473 |
|
* other threads are waiting for readlock |
474 |
|
*/ |
475 |
|
public void testReadHoldingWriteLockFair2() throws InterruptedException { |
476 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
477 |
< |
lock.writeLock().lock(); |
478 |
< |
Thread t1 = new Thread(new Runnable() { |
479 |
< |
public void run() { |
480 |
< |
lock.readLock().lock(); |
481 |
< |
lock.readLock().unlock(); |
482 |
< |
} |
483 |
< |
}); |
484 |
< |
Thread t2 = new Thread(new Runnable() { |
485 |
< |
public void run() { |
486 |
< |
lock.readLock().lock(); |
487 |
< |
lock.readLock().unlock(); |
515 |
< |
} |
516 |
< |
}); |
476 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
477 |
> |
lock.writeLock().lock(); |
478 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
479 |
> |
public void realRun() { |
480 |
> |
lock.readLock().lock(); |
481 |
> |
lock.readLock().unlock(); |
482 |
> |
}}); |
483 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
484 |
> |
public void realRun() { |
485 |
> |
lock.readLock().lock(); |
486 |
> |
lock.readLock().unlock(); |
487 |
> |
}}); |
488 |
|
|
518 |
– |
t1.start(); |
519 |
– |
t2.start(); |
489 |
|
lock.readLock().lock(); |
490 |
|
lock.readLock().unlock(); |
491 |
|
Thread.sleep(SHORT_DELAY_MS); |
504 |
|
* other threads are waiting for writelock |
505 |
|
*/ |
506 |
|
public void testReadHoldingWriteLockFair3() throws InterruptedException { |
507 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
508 |
< |
lock.writeLock().lock(); |
509 |
< |
Thread t1 = new Thread(new Runnable() { |
510 |
< |
public void run() { |
511 |
< |
lock.writeLock().lock(); |
512 |
< |
lock.writeLock().unlock(); |
513 |
< |
} |
514 |
< |
}); |
515 |
< |
Thread t2 = new Thread(new Runnable() { |
516 |
< |
public void run() { |
517 |
< |
lock.writeLock().lock(); |
518 |
< |
lock.writeLock().unlock(); |
550 |
< |
} |
551 |
< |
}); |
507 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
508 |
> |
lock.writeLock().lock(); |
509 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
510 |
> |
public void realRun() { |
511 |
> |
lock.writeLock().lock(); |
512 |
> |
lock.writeLock().unlock(); |
513 |
> |
}}); |
514 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
515 |
> |
public void realRun() { |
516 |
> |
lock.writeLock().lock(); |
517 |
> |
lock.writeLock().unlock(); |
518 |
> |
}}); |
519 |
|
|
553 |
– |
t1.start(); |
554 |
– |
t2.start(); |
520 |
|
lock.readLock().lock(); |
521 |
|
lock.readLock().unlock(); |
522 |
|
Thread.sleep(SHORT_DELAY_MS); |
535 |
|
* other threads are waiting for writelock |
536 |
|
*/ |
537 |
|
public void testWriteHoldingWriteLockFair4() 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(); |
585 |
< |
} |
586 |
< |
}); |
538 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
539 |
> |
lock.writeLock().lock(); |
540 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
541 |
> |
public void realRun() { |
542 |
> |
lock.writeLock().lock(); |
543 |
> |
lock.writeLock().unlock(); |
544 |
> |
}}); |
545 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
546 |
> |
public void realRun() { |
547 |
> |
lock.writeLock().lock(); |
548 |
> |
lock.writeLock().unlock(); |
549 |
> |
}}); |
550 |
|
|
588 |
– |
t1.start(); |
589 |
– |
t2.start(); |
551 |
|
Thread.sleep(SHORT_DELAY_MS); |
552 |
|
assertTrue(lock.isWriteLockedByCurrentThread()); |
553 |
< |
assertTrue(lock.getWriteHoldCount() == 1); |
553 |
> |
assertEquals(1, lock.getWriteHoldCount()); |
554 |
|
lock.writeLock().lock(); |
555 |
< |
assertTrue(lock.getWriteHoldCount() == 2); |
555 |
> |
assertEquals(2, lock.getWriteHoldCount()); |
556 |
|
lock.writeLock().unlock(); |
557 |
|
lock.writeLock().lock(); |
558 |
|
lock.writeLock().unlock(); |
568 |
|
* Read tryLock succeeds if readlocked but not writelocked |
569 |
|
*/ |
570 |
|
public void testTryLockWhenReadLocked() throws InterruptedException { |
571 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
572 |
< |
lock.readLock().lock(); |
573 |
< |
Thread t = new Thread(new Runnable() { |
574 |
< |
public void run() { |
575 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
576 |
< |
lock.readLock().unlock(); |
577 |
< |
} |
617 |
< |
}); |
571 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
572 |
> |
lock.readLock().lock(); |
573 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
574 |
> |
public void realRun() { |
575 |
> |
assertTrue(lock.readLock().tryLock()); |
576 |
> |
lock.readLock().unlock(); |
577 |
> |
}}); |
578 |
|
|
619 |
– |
t.start(); |
579 |
|
t.join(); |
580 |
|
lock.readLock().unlock(); |
581 |
|
} |
582 |
|
|
624 |
– |
|
625 |
– |
|
583 |
|
/** |
584 |
|
* write tryLock fails when readlocked |
585 |
|
*/ |
586 |
|
public void testWriteTryLockWhenReadLocked() throws InterruptedException { |
587 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
588 |
< |
lock.readLock().lock(); |
589 |
< |
Thread t = new Thread(new Runnable() { |
590 |
< |
public void run() { |
591 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
592 |
< |
} |
636 |
< |
}); |
587 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
588 |
> |
lock.readLock().lock(); |
589 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
590 |
> |
public void realRun() { |
591 |
> |
assertFalse(lock.writeLock().tryLock()); |
592 |
> |
}}); |
593 |
|
|
638 |
– |
t.start(); |
594 |
|
t.join(); |
595 |
|
lock.readLock().unlock(); |
596 |
|
} |
600 |
|
* Fair Read tryLock succeeds if readlocked but not writelocked |
601 |
|
*/ |
602 |
|
public void testTryLockWhenReadLockedFair() throws InterruptedException { |
603 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
604 |
< |
lock.readLock().lock(); |
605 |
< |
Thread t = new Thread(new Runnable() { |
606 |
< |
public void run() { |
607 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
608 |
< |
lock.readLock().unlock(); |
609 |
< |
} |
655 |
< |
}); |
603 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
604 |
> |
lock.readLock().lock(); |
605 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
606 |
> |
public void realRun() { |
607 |
> |
assertTrue(lock.readLock().tryLock()); |
608 |
> |
lock.readLock().unlock(); |
609 |
> |
}}); |
610 |
|
|
657 |
– |
t.start(); |
611 |
|
t.join(); |
612 |
|
lock.readLock().unlock(); |
613 |
|
} |
618 |
|
* Fair write tryLock fails when readlocked |
619 |
|
*/ |
620 |
|
public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { |
621 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
622 |
< |
lock.readLock().lock(); |
623 |
< |
Thread t = new Thread(new Runnable() { |
624 |
< |
public void run() { |
625 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
626 |
< |
} |
674 |
< |
}); |
621 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
622 |
> |
lock.readLock().lock(); |
623 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
624 |
> |
public void realRun() { |
625 |
> |
assertFalse(lock.writeLock().tryLock()); |
626 |
> |
}}); |
627 |
|
|
676 |
– |
t.start(); |
628 |
|
t.join(); |
629 |
|
lock.readLock().unlock(); |
630 |
|
} |
635 |
|
* write timed tryLock times out if locked |
636 |
|
*/ |
637 |
|
public void testWriteTryLock_Timeout() throws InterruptedException { |
638 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
639 |
< |
lock.writeLock().lock(); |
640 |
< |
Thread t = new Thread(new CheckedRunnable() { |
638 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
639 |
> |
lock.writeLock().lock(); |
640 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
641 |
|
public void realRun() throws InterruptedException { |
642 |
< |
threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
642 |
> |
assertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); |
643 |
|
}}); |
644 |
|
|
694 |
– |
t.start(); |
645 |
|
t.join(); |
646 |
|
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
647 |
|
lock.writeLock().unlock(); |
651 |
|
* read timed tryLock times out if write-locked |
652 |
|
*/ |
653 |
|
public void testReadTryLock_Timeout() throws InterruptedException { |
654 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
655 |
< |
lock.writeLock().lock(); |
656 |
< |
Thread t = new Thread(new CheckedRunnable() { |
654 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
655 |
> |
lock.writeLock().lock(); |
656 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
657 |
|
public void realRun() throws InterruptedException { |
658 |
< |
threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
658 |
> |
assertFalse(lock.readLock().tryLock(1, MILLISECONDS)); |
659 |
|
}}); |
660 |
|
|
711 |
– |
t.start(); |
661 |
|
t.join(); |
662 |
|
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
663 |
|
lock.writeLock().unlock(); |
668 |
|
* write lockInterruptibly succeeds if lock free else is interruptible |
669 |
|
*/ |
670 |
|
public void testWriteLockInterruptibly() throws InterruptedException { |
671 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
671 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
672 |
|
lock.writeLock().lockInterruptibly(); |
673 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
673 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
674 |
|
public void realRun() throws InterruptedException { |
675 |
|
lock.writeLock().lockInterruptibly(); |
676 |
|
}}); |
677 |
|
|
729 |
– |
t.start(); |
678 |
|
Thread.sleep(SHORT_DELAY_MS); |
679 |
|
t.interrupt(); |
680 |
|
Thread.sleep(SHORT_DELAY_MS); |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
< |
* read lockInterruptibly succeeds if lock free else is interruptible |
686 |
> |
* read lockInterruptibly succeeds if lock free else is interruptible |
687 |
|
*/ |
688 |
|
public void testReadLockInterruptibly() throws InterruptedException { |
689 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
689 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
690 |
|
lock.writeLock().lockInterruptibly(); |
691 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
691 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
692 |
|
public void realRun() throws InterruptedException { |
693 |
|
lock.readLock().lockInterruptibly(); |
694 |
|
}}); |
695 |
|
|
748 |
– |
t.start(); |
696 |
|
Thread.sleep(SHORT_DELAY_MS); |
697 |
|
t.interrupt(); |
698 |
|
t.join(); |
703 |
|
* Calling await without holding lock throws IllegalMonitorStateException |
704 |
|
*/ |
705 |
|
public void testAwait_IllegalMonitor() throws InterruptedException { |
706 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
706 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
707 |
|
final Condition c = lock.writeLock().newCondition(); |
708 |
|
try { |
709 |
|
c.await(); |
715 |
|
* Calling signal without holding lock throws IllegalMonitorStateException |
716 |
|
*/ |
717 |
|
public void testSignal_IllegalMonitor() { |
718 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
718 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
719 |
|
final Condition c = lock.writeLock().newCondition(); |
720 |
|
try { |
721 |
|
c.signal(); |
727 |
|
* awaitNanos without a signal times out |
728 |
|
*/ |
729 |
|
public void testAwaitNanos_Timeout() throws InterruptedException { |
730 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
730 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
731 |
|
final Condition c = lock.writeLock().newCondition(); |
732 |
|
|
733 |
|
lock.writeLock().lock(); |
738 |
|
|
739 |
|
|
740 |
|
/** |
741 |
< |
* timed await without a signal times out |
741 |
> |
* timed await without a signal times out |
742 |
|
*/ |
743 |
|
public void testAwait_Timeout() throws InterruptedException { |
744 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
744 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
745 |
|
final Condition c = lock.writeLock().newCondition(); |
746 |
|
lock.writeLock().lock(); |
747 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
747 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
748 |
|
lock.writeLock().unlock(); |
749 |
|
} |
750 |
|
|
752 |
|
* awaitUntil without a signal times out |
753 |
|
*/ |
754 |
|
public void testAwaitUntil_Timeout() throws InterruptedException { |
755 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
755 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
756 |
|
final Condition c = lock.writeLock().newCondition(); |
757 |
|
lock.writeLock().lock(); |
758 |
|
java.util.Date d = new java.util.Date(); |
764 |
|
* await returns when signalled |
765 |
|
*/ |
766 |
|
public void testAwait() throws InterruptedException { |
767 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
767 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
768 |
|
final Condition c = lock.writeLock().newCondition(); |
769 |
< |
Thread t = new Thread(new CheckedRunnable() { |
769 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
770 |
|
public void realRun() throws InterruptedException { |
771 |
|
lock.writeLock().lock(); |
772 |
|
c.await(); |
773 |
|
lock.writeLock().unlock(); |
774 |
|
}}); |
775 |
|
|
829 |
– |
t.start(); |
776 |
|
Thread.sleep(SHORT_DELAY_MS); |
777 |
|
lock.writeLock().lock(); |
778 |
|
c.signal(); |
840 |
|
* await is interruptible |
841 |
|
*/ |
842 |
|
public void testAwait_Interrupt() throws InterruptedException { |
843 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
843 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
844 |
|
final Condition c = lock.writeLock().newCondition(); |
845 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
845 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
846 |
|
public void realRun() throws InterruptedException { |
847 |
|
lock.writeLock().lock(); |
848 |
|
c.await(); |
849 |
|
lock.writeLock().unlock(); |
850 |
|
}}); |
851 |
|
|
906 |
– |
t.start(); |
852 |
|
Thread.sleep(SHORT_DELAY_MS); |
853 |
|
t.interrupt(); |
854 |
|
t.join(SHORT_DELAY_MS); |
859 |
|
* awaitNanos is interruptible |
860 |
|
*/ |
861 |
|
public void testAwaitNanos_Interrupt() throws InterruptedException { |
862 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
862 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
863 |
|
final Condition c = lock.writeLock().newCondition(); |
864 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
864 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
865 |
|
public void realRun() throws InterruptedException { |
866 |
|
lock.writeLock().lock(); |
867 |
< |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
867 |
> |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
868 |
|
lock.writeLock().unlock(); |
869 |
|
}}); |
870 |
|
|
926 |
– |
t.start(); |
871 |
|
Thread.sleep(SHORT_DELAY_MS); |
872 |
|
t.interrupt(); |
873 |
|
t.join(SHORT_DELAY_MS); |
878 |
|
* awaitUntil is interruptible |
879 |
|
*/ |
880 |
|
public void testAwaitUntil_Interrupt() throws InterruptedException { |
881 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
881 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
882 |
|
final Condition c = lock.writeLock().newCondition(); |
883 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
883 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
884 |
|
public void realRun() throws InterruptedException { |
885 |
|
lock.writeLock().lock(); |
886 |
|
java.util.Date d = new java.util.Date(); |
888 |
|
lock.writeLock().unlock(); |
889 |
|
}}); |
890 |
|
|
947 |
– |
t.start(); |
891 |
|
Thread.sleep(SHORT_DELAY_MS); |
892 |
|
t.interrupt(); |
893 |
|
t.join(SHORT_DELAY_MS); |
898 |
|
* signalAll wakes up all threads |
899 |
|
*/ |
900 |
|
public void testSignalAll() throws InterruptedException { |
901 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
901 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
902 |
|
final Condition c = lock.writeLock().newCondition(); |
903 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
903 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
904 |
|
public void realRun() throws InterruptedException { |
905 |
|
lock.writeLock().lock(); |
906 |
|
c.await(); |
907 |
|
lock.writeLock().unlock(); |
908 |
|
}}); |
909 |
|
|
910 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
910 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
911 |
|
public void realRun() throws InterruptedException { |
912 |
|
lock.writeLock().lock(); |
913 |
|
c.await(); |
914 |
|
lock.writeLock().unlock(); |
915 |
|
}}); |
916 |
|
|
974 |
– |
t1.start(); |
975 |
– |
t2.start(); |
917 |
|
Thread.sleep(SHORT_DELAY_MS); |
918 |
|
lock.writeLock().lock(); |
919 |
|
c.signalAll(); |
948 |
|
* hasQueuedThreads reports whether there are waiting threads |
949 |
|
*/ |
950 |
|
public void testhasQueuedThreads() throws InterruptedException { |
951 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
951 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
952 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
953 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
954 |
|
assertFalse(lock.hasQueuedThreads()); |
973 |
|
* hasQueuedThread(null) throws NPE |
974 |
|
*/ |
975 |
|
public void testHasQueuedThreadNPE() { |
976 |
< |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
976 |
> |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
977 |
|
try { |
978 |
|
sync.hasQueuedThread(null); |
979 |
|
shouldThrow(); |
984 |
|
* hasQueuedThread reports whether a thread is queued. |
985 |
|
*/ |
986 |
|
public void testHasQueuedThread() throws InterruptedException { |
987 |
< |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
987 |
> |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
988 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
989 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
990 |
|
assertFalse(sync.hasQueuedThread(t1)); |
1015 |
|
* getQueueLength reports number of waiting threads |
1016 |
|
*/ |
1017 |
|
public void testGetQueueLength() throws InterruptedException { |
1018 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1018 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1019 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1020 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1021 |
|
assertEquals(0, lock.getQueueLength()); |
1040 |
|
* getQueuedThreads includes waiting threads |
1041 |
|
*/ |
1042 |
|
public void testGetQueuedThreads() throws InterruptedException { |
1043 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1043 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1044 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1045 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1046 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
1068 |
|
* hasWaiters throws NPE if null |
1069 |
|
*/ |
1070 |
|
public void testHasWaitersNPE() { |
1071 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1071 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1072 |
|
try { |
1073 |
|
lock.hasWaiters(null); |
1074 |
|
shouldThrow(); |
1079 |
|
* getWaitQueueLength throws NPE if null |
1080 |
|
*/ |
1081 |
|
public void testGetWaitQueueLengthNPE() { |
1082 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1082 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1083 |
|
try { |
1084 |
|
lock.getWaitQueueLength(null); |
1085 |
|
shouldThrow(); |
1091 |
|
* getWaitingThreads throws NPE if null |
1092 |
|
*/ |
1093 |
|
public void testGetWaitingThreadsNPE() { |
1094 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1094 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1095 |
|
try { |
1096 |
|
lock.getWaitingThreads(null); |
1097 |
|
shouldThrow(); |
1102 |
|
* hasWaiters throws IAE if not owned |
1103 |
|
*/ |
1104 |
|
public void testHasWaitersIAE() { |
1105 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1105 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1106 |
|
final Condition c = lock.writeLock().newCondition(); |
1107 |
< |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1107 |
> |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1108 |
|
try { |
1109 |
|
lock2.hasWaiters(c); |
1110 |
|
shouldThrow(); |
1115 |
|
* hasWaiters throws IMSE if not locked |
1116 |
|
*/ |
1117 |
|
public void testHasWaitersIMSE() { |
1118 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1118 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1119 |
|
final Condition c = lock.writeLock().newCondition(); |
1120 |
|
try { |
1121 |
|
lock.hasWaiters(c); |
1128 |
|
* getWaitQueueLength throws IAE if not owned |
1129 |
|
*/ |
1130 |
|
public void testGetWaitQueueLengthIAE() { |
1131 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1131 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1132 |
|
final Condition c = lock.writeLock().newCondition(); |
1133 |
< |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1133 |
> |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1134 |
|
try { |
1135 |
|
lock2.getWaitQueueLength(c); |
1136 |
|
shouldThrow(); |
1141 |
|
* getWaitQueueLength throws IMSE if not locked |
1142 |
|
*/ |
1143 |
|
public void testGetWaitQueueLengthIMSE() { |
1144 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1144 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1145 |
|
final Condition c = lock.writeLock().newCondition(); |
1146 |
|
try { |
1147 |
|
lock.getWaitQueueLength(c); |
1154 |
|
* getWaitingThreads throws IAE if not owned |
1155 |
|
*/ |
1156 |
|
public void testGetWaitingThreadsIAE() { |
1157 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1157 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1158 |
|
final Condition c = lock.writeLock().newCondition(); |
1159 |
< |
final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); |
1159 |
> |
final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); |
1160 |
|
try { |
1161 |
|
lock2.getWaitingThreads(c); |
1162 |
|
shouldThrow(); |
1167 |
|
* getWaitingThreads throws IMSE if not locked |
1168 |
|
*/ |
1169 |
|
public void testGetWaitingThreadsIMSE() { |
1170 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1170 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1171 |
|
final Condition c = lock.writeLock().newCondition(); |
1172 |
|
try { |
1173 |
|
lock.getWaitingThreads(c); |
1180 |
|
* hasWaiters returns true when a thread is waiting, else false |
1181 |
|
*/ |
1182 |
|
public void testHasWaiters() throws InterruptedException { |
1183 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1183 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1184 |
|
final Condition c = lock.writeLock().newCondition(); |
1185 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1185 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1186 |
|
public void realRun() throws InterruptedException { |
1187 |
|
lock.writeLock().lock(); |
1188 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1189 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1188 |
> |
assertFalse(lock.hasWaiters(c)); |
1189 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1190 |
|
c.await(); |
1191 |
|
lock.writeLock().unlock(); |
1192 |
|
}}); |
1193 |
|
|
1253 |
– |
t.start(); |
1194 |
|
Thread.sleep(SHORT_DELAY_MS); |
1195 |
|
lock.writeLock().lock(); |
1196 |
|
assertTrue(lock.hasWaiters(c)); |
1210 |
|
* getWaitQueueLength returns number of waiting threads |
1211 |
|
*/ |
1212 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
1213 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1213 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1214 |
|
final Condition c = lock.writeLock().newCondition(); |
1215 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1215 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1216 |
|
public void realRun() throws InterruptedException { |
1217 |
|
lock.writeLock().lock(); |
1218 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1219 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1218 |
> |
assertFalse(lock.hasWaiters(c)); |
1219 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1220 |
|
c.await(); |
1221 |
|
lock.writeLock().unlock(); |
1222 |
|
}}); |
1223 |
|
|
1284 |
– |
t.start(); |
1224 |
|
Thread.sleep(SHORT_DELAY_MS); |
1225 |
|
lock.writeLock().lock(); |
1226 |
|
assertTrue(lock.hasWaiters(c)); |
1241 |
|
* getWaitingThreads returns only and all waiting threads |
1242 |
|
*/ |
1243 |
|
public void testGetWaitingThreads() throws InterruptedException { |
1244 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1244 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1245 |
|
final Condition c = lock.writeLock().newCondition(); |
1246 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
1246 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
1247 |
|
public void realRun() throws InterruptedException { |
1248 |
|
lock.writeLock().lock(); |
1249 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
1249 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1250 |
|
c.await(); |
1251 |
|
lock.writeLock().unlock(); |
1252 |
|
}}); |
1253 |
|
|
1254 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
1254 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
1255 |
|
public void realRun() throws InterruptedException { |
1256 |
|
lock.writeLock().lock(); |
1257 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
1257 |
> |
assertFalse(lock.getWaitingThreads(c).isEmpty()); |
1258 |
|
c.await(); |
1259 |
|
lock.writeLock().unlock(); |
1260 |
|
}}); |