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 |
|
/** |
25 |
|
* A runnable calling lockInterruptibly |
26 |
|
*/ |
27 |
< |
class InterruptibleLockRunnable implements Runnable { |
27 |
> |
class InterruptibleLockRunnable extends CheckedRunnable { |
28 |
|
final ReentrantReadWriteLock lock; |
29 |
|
InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
30 |
< |
public void run() { |
31 |
< |
try { |
31 |
< |
lock.writeLock().lockInterruptibly(); |
32 |
< |
} catch(InterruptedException success){} |
30 |
> |
public void realRun() throws InterruptedException { |
31 |
> |
lock.writeLock().lockInterruptibly(); |
32 |
|
} |
33 |
|
} |
34 |
|
|
37 |
|
* A runnable calling lockInterruptibly that expects to be |
38 |
|
* interrupted |
39 |
|
*/ |
40 |
< |
class InterruptedLockRunnable implements Runnable { |
40 |
> |
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
41 |
|
final ReentrantReadWriteLock lock; |
42 |
|
InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
43 |
< |
public void run() { |
44 |
< |
try { |
46 |
< |
lock.writeLock().lockInterruptibly(); |
47 |
< |
threadShouldThrow(); |
48 |
< |
} catch(InterruptedException success){} |
43 |
> |
public void realRun() throws InterruptedException { |
44 |
> |
lock.writeLock().lockInterruptibly(); |
45 |
|
} |
46 |
|
} |
47 |
|
|
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); |
74 |
+ |
assertFalse(r3.isFair()); |
75 |
+ |
assertFalse(r3.isWriteLocked()); |
76 |
+ |
assertEquals(0, r3.getReadLockCount()); |
77 |
|
} |
78 |
|
|
79 |
|
/** |
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 |
|
|
188 |
|
/** |
189 |
|
* write-lockInterruptibly is interruptible |
190 |
|
*/ |
191 |
< |
public void testWriteLockInterruptibly_Interrupted() { |
192 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
193 |
< |
Thread t = new Thread(new Runnable() { |
194 |
< |
public void run() { |
195 |
< |
try { |
196 |
< |
lock.writeLock().lockInterruptibly(); |
197 |
< |
lock.writeLock().unlock(); |
198 |
< |
lock.writeLock().lockInterruptibly(); |
199 |
< |
lock.writeLock().unlock(); |
200 |
< |
} catch(InterruptedException success){} |
201 |
< |
} |
202 |
< |
}); |
203 |
< |
try { |
204 |
< |
lock.writeLock().lock(); |
205 |
< |
t.start(); |
206 |
< |
Thread.sleep(SHORT_DELAY_MS); |
207 |
< |
t.interrupt(); |
208 |
< |
Thread.sleep(SHORT_DELAY_MS); |
209 |
< |
lock.writeLock().unlock(); |
210 |
< |
t.join(); |
211 |
< |
} catch(Exception e){ |
212 |
< |
unexpectedException(); |
213 |
< |
} |
191 |
> |
public void testWriteLockInterruptibly_Interrupted() throws Exception { |
192 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
193 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
194 |
> |
public void realRun() throws InterruptedException { |
195 |
> |
lock.writeLock().lockInterruptibly(); |
196 |
> |
lock.writeLock().unlock(); |
197 |
> |
lock.writeLock().lockInterruptibly(); |
198 |
> |
lock.writeLock().unlock(); |
199 |
> |
}}); |
200 |
> |
|
201 |
> |
lock.writeLock().lock(); |
202 |
> |
t.start(); |
203 |
> |
Thread.sleep(SHORT_DELAY_MS); |
204 |
> |
t.interrupt(); |
205 |
> |
Thread.sleep(SHORT_DELAY_MS); |
206 |
> |
lock.writeLock().unlock(); |
207 |
> |
t.join(); |
208 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
|
* timed write-tryLock is interruptible |
212 |
|
*/ |
213 |
< |
public void testWriteTryLock_Interrupted() { |
214 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
215 |
< |
lock.writeLock().lock(); |
216 |
< |
Thread t = new Thread(new Runnable() { |
217 |
< |
public void run() { |
218 |
< |
try { |
219 |
< |
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
220 |
< |
} catch(InterruptedException success){} |
221 |
< |
} |
222 |
< |
}); |
223 |
< |
try { |
224 |
< |
t.start(); |
225 |
< |
t.interrupt(); |
232 |
< |
lock.writeLock().unlock(); |
233 |
< |
t.join(); |
234 |
< |
} catch(Exception e){ |
235 |
< |
unexpectedException(); |
236 |
< |
} |
213 |
> |
public void testWriteTryLock_Interrupted() throws InterruptedException { |
214 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
215 |
> |
lock.writeLock().lock(); |
216 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
217 |
> |
public void realRun() throws InterruptedException { |
218 |
> |
lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS); |
219 |
> |
}}); |
220 |
> |
|
221 |
> |
t.start(); |
222 |
> |
Thread.sleep(SHORT_DELAY_MS); |
223 |
> |
t.interrupt(); |
224 |
> |
lock.writeLock().unlock(); |
225 |
> |
t.join(); |
226 |
|
} |
227 |
|
|
228 |
|
/** |
229 |
|
* read-lockInterruptibly is interruptible |
230 |
|
*/ |
231 |
< |
public void testReadLockInterruptibly_Interrupted() { |
232 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
233 |
< |
lock.writeLock().lock(); |
234 |
< |
Thread t = new Thread(new Runnable() { |
235 |
< |
public void run() { |
236 |
< |
try { |
237 |
< |
lock.readLock().lockInterruptibly(); |
238 |
< |
} catch(InterruptedException success){} |
239 |
< |
} |
240 |
< |
}); |
241 |
< |
try { |
242 |
< |
t.start(); |
243 |
< |
Thread.sleep(SHORT_DELAY_MS); |
244 |
< |
t.interrupt(); |
256 |
< |
Thread.sleep(SHORT_DELAY_MS); |
257 |
< |
lock.writeLock().unlock(); |
258 |
< |
t.join(); |
259 |
< |
} catch(Exception e){ |
260 |
< |
unexpectedException(); |
261 |
< |
} |
231 |
> |
public void testReadLockInterruptibly_Interrupted() throws InterruptedException { |
232 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
233 |
> |
lock.writeLock().lock(); |
234 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
235 |
> |
public void realRun() throws InterruptedException { |
236 |
> |
lock.readLock().lockInterruptibly(); |
237 |
> |
}}); |
238 |
> |
|
239 |
> |
t.start(); |
240 |
> |
Thread.sleep(SHORT_DELAY_MS); |
241 |
> |
t.interrupt(); |
242 |
> |
Thread.sleep(SHORT_DELAY_MS); |
243 |
> |
lock.writeLock().unlock(); |
244 |
> |
t.join(); |
245 |
|
} |
246 |
|
|
247 |
|
/** |
248 |
|
* timed read-tryLock is interruptible |
249 |
|
*/ |
250 |
< |
public void testReadTryLock_Interrupted() { |
251 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
252 |
< |
lock.writeLock().lock(); |
253 |
< |
Thread t = new Thread(new Runnable() { |
254 |
< |
public void run() { |
255 |
< |
try { |
256 |
< |
lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS); |
257 |
< |
threadShouldThrow(); |
258 |
< |
} catch(InterruptedException success){} |
259 |
< |
} |
260 |
< |
}); |
261 |
< |
try { |
279 |
< |
t.start(); |
280 |
< |
t.interrupt(); |
281 |
< |
t.join(); |
282 |
< |
} catch(Exception e){ |
283 |
< |
unexpectedException(); |
284 |
< |
} |
250 |
> |
public void testReadTryLock_Interrupted() throws InterruptedException { |
251 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
252 |
> |
lock.writeLock().lock(); |
253 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
254 |
> |
public void realRun() throws InterruptedException { |
255 |
> |
lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS); |
256 |
> |
}}); |
257 |
> |
|
258 |
> |
t.start(); |
259 |
> |
Thread.sleep(SHORT_DELAY_MS); |
260 |
> |
t.interrupt(); |
261 |
> |
t.join(); |
262 |
|
} |
263 |
|
|
264 |
|
|
265 |
|
/** |
266 |
|
* write-tryLock fails if locked |
267 |
|
*/ |
268 |
< |
public void testWriteTryLockWhenLocked() { |
269 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
270 |
< |
lock.writeLock().lock(); |
271 |
< |
Thread t = new Thread(new Runnable() { |
272 |
< |
public void run() { |
273 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
274 |
< |
} |
275 |
< |
}); |
276 |
< |
try { |
277 |
< |
t.start(); |
278 |
< |
t.join(); |
302 |
< |
lock.writeLock().unlock(); |
303 |
< |
} catch(Exception e){ |
304 |
< |
unexpectedException(); |
305 |
< |
} |
268 |
> |
public void testWriteTryLockWhenLocked() throws InterruptedException { |
269 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
270 |
> |
lock.writeLock().lock(); |
271 |
> |
Thread t = new Thread(new CheckedRunnable() { |
272 |
> |
public void realRun() { |
273 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
274 |
> |
}}); |
275 |
> |
|
276 |
> |
t.start(); |
277 |
> |
t.join(); |
278 |
> |
lock.writeLock().unlock(); |
279 |
|
} |
280 |
|
|
281 |
|
/** |
282 |
|
* read-tryLock fails if locked |
283 |
|
*/ |
284 |
< |
public void testReadTryLockWhenLocked() { |
285 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
286 |
< |
lock.writeLock().lock(); |
287 |
< |
Thread t = new Thread(new Runnable() { |
288 |
< |
public void run() { |
289 |
< |
threadAssertFalse(lock.readLock().tryLock()); |
290 |
< |
} |
291 |
< |
}); |
292 |
< |
try { |
293 |
< |
t.start(); |
294 |
< |
t.join(); |
322 |
< |
lock.writeLock().unlock(); |
323 |
< |
} catch(Exception e){ |
324 |
< |
unexpectedException(); |
325 |
< |
} |
284 |
> |
public void testReadTryLockWhenLocked() throws InterruptedException { |
285 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
286 |
> |
lock.writeLock().lock(); |
287 |
> |
Thread t = new Thread(new CheckedRunnable() { |
288 |
> |
public void realRun() { |
289 |
> |
threadAssertFalse(lock.readLock().tryLock()); |
290 |
> |
}}); |
291 |
> |
|
292 |
> |
t.start(); |
293 |
> |
t.join(); |
294 |
> |
lock.writeLock().unlock(); |
295 |
|
} |
296 |
|
|
297 |
|
/** |
298 |
|
* Multiple threads can hold a read lock when not write-locked |
299 |
|
*/ |
300 |
< |
public void testMultipleReadLocks() { |
301 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
302 |
< |
lock.readLock().lock(); |
303 |
< |
Thread t = new Thread(new Runnable() { |
304 |
< |
public void run() { |
305 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
306 |
< |
lock.readLock().unlock(); |
307 |
< |
} |
308 |
< |
}); |
309 |
< |
try { |
310 |
< |
t.start(); |
311 |
< |
t.join(); |
343 |
< |
lock.readLock().unlock(); |
344 |
< |
} catch(Exception e){ |
345 |
< |
unexpectedException(); |
346 |
< |
} |
300 |
> |
public void testMultipleReadLocks() throws InterruptedException { |
301 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
302 |
> |
lock.readLock().lock(); |
303 |
> |
Thread t = new Thread(new CheckedRunnable() { |
304 |
> |
public void realRun() { |
305 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
306 |
> |
lock.readLock().unlock(); |
307 |
> |
}}); |
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() { |
318 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
319 |
< |
lock.readLock().lock(); |
320 |
< |
Thread t1 = new Thread(new Runnable() { |
321 |
< |
public void run() { |
322 |
< |
lock.readLock().lock(); |
323 |
< |
lock.readLock().unlock(); |
324 |
< |
} |
325 |
< |
}); |
326 |
< |
Thread t2 = new Thread(new Runnable() { |
327 |
< |
public void run() { |
328 |
< |
lock.writeLock().lock(); |
329 |
< |
lock.writeLock().unlock(); |
365 |
< |
} |
366 |
< |
}); |
367 |
< |
|
368 |
< |
try { |
369 |
< |
t1.start(); |
370 |
< |
t2.start(); |
371 |
< |
Thread.sleep(SHORT_DELAY_MS); |
372 |
< |
lock.readLock().unlock(); |
373 |
< |
t1.join(MEDIUM_DELAY_MS); |
374 |
< |
t2.join(MEDIUM_DELAY_MS); |
375 |
< |
assertTrue(!t1.isAlive()); |
376 |
< |
assertTrue(!t2.isAlive()); |
317 |
> |
public void testWriteAfterMultipleReadLocks() throws InterruptedException { |
318 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
319 |
> |
lock.readLock().lock(); |
320 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
321 |
> |
public void realRun() { |
322 |
> |
lock.readLock().lock(); |
323 |
> |
lock.readLock().unlock(); |
324 |
> |
}}); |
325 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
326 |
> |
public void realRun() { |
327 |
> |
lock.writeLock().lock(); |
328 |
> |
lock.writeLock().unlock(); |
329 |
> |
}}); |
330 |
|
|
331 |
< |
} catch(Exception e){ |
332 |
< |
unexpectedException(); |
333 |
< |
} |
331 |
> |
t1.start(); |
332 |
> |
t2.start(); |
333 |
> |
Thread.sleep(SHORT_DELAY_MS); |
334 |
> |
lock.readLock().unlock(); |
335 |
> |
t1.join(MEDIUM_DELAY_MS); |
336 |
> |
t2.join(MEDIUM_DELAY_MS); |
337 |
> |
assertTrue(!t1.isAlive()); |
338 |
> |
assertTrue(!t2.isAlive()); |
339 |
|
} |
340 |
|
|
341 |
|
/** |
342 |
|
* Readlocks succeed after a writing thread unlocks |
343 |
|
*/ |
344 |
< |
public void testReadAfterWriteLock() { |
345 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
346 |
< |
lock.writeLock().lock(); |
347 |
< |
Thread t1 = new Thread(new Runnable() { |
348 |
< |
public void run() { |
349 |
< |
lock.readLock().lock(); |
350 |
< |
lock.readLock().unlock(); |
351 |
< |
} |
352 |
< |
}); |
353 |
< |
Thread t2 = new Thread(new Runnable() { |
354 |
< |
public void run() { |
355 |
< |
lock.readLock().lock(); |
356 |
< |
lock.readLock().unlock(); |
357 |
< |
} |
358 |
< |
}); |
359 |
< |
|
360 |
< |
try { |
361 |
< |
t1.start(); |
362 |
< |
t2.start(); |
363 |
< |
Thread.sleep(SHORT_DELAY_MS); |
364 |
< |
lock.writeLock().unlock(); |
365 |
< |
t1.join(MEDIUM_DELAY_MS); |
408 |
< |
t2.join(MEDIUM_DELAY_MS); |
409 |
< |
assertTrue(!t1.isAlive()); |
410 |
< |
assertTrue(!t2.isAlive()); |
411 |
< |
|
412 |
< |
} catch(Exception e){ |
413 |
< |
unexpectedException(); |
414 |
< |
} |
344 |
> |
public void testReadAfterWriteLock() throws InterruptedException { |
345 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
346 |
> |
lock.writeLock().lock(); |
347 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
348 |
> |
public void realRun() { |
349 |
> |
lock.readLock().lock(); |
350 |
> |
lock.readLock().unlock(); |
351 |
> |
}}); |
352 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
353 |
> |
public void realRun() { |
354 |
> |
lock.readLock().lock(); |
355 |
> |
lock.readLock().unlock(); |
356 |
> |
}}); |
357 |
> |
|
358 |
> |
t1.start(); |
359 |
> |
t2.start(); |
360 |
> |
Thread.sleep(SHORT_DELAY_MS); |
361 |
> |
lock.writeLock().unlock(); |
362 |
> |
t1.join(MEDIUM_DELAY_MS); |
363 |
> |
t2.join(MEDIUM_DELAY_MS); |
364 |
> |
assertTrue(!t1.isAlive()); |
365 |
> |
assertTrue(!t2.isAlive()); |
366 |
|
} |
367 |
|
|
368 |
|
/** |
369 |
|
* Read trylock succeeds if write locked by current thread |
370 |
|
*/ |
371 |
|
public void testReadHoldingWriteLock() { |
372 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
373 |
< |
lock.writeLock().lock(); |
372 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
373 |
> |
lock.writeLock().lock(); |
374 |
|
assertTrue(lock.readLock().tryLock()); |
375 |
|
lock.readLock().unlock(); |
376 |
|
lock.writeLock().unlock(); |
380 |
|
* Read lock succeeds if write locked by current thread even if |
381 |
|
* other threads are waiting for readlock |
382 |
|
*/ |
383 |
< |
public void testReadHoldingWriteLock2() { |
384 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
385 |
< |
lock.writeLock().lock(); |
386 |
< |
Thread t1 = new Thread(new Runnable() { |
387 |
< |
public void run() { |
388 |
< |
lock.readLock().lock(); |
389 |
< |
lock.readLock().unlock(); |
390 |
< |
} |
391 |
< |
}); |
392 |
< |
Thread t2 = new Thread(new Runnable() { |
393 |
< |
public void run() { |
394 |
< |
lock.readLock().lock(); |
395 |
< |
lock.readLock().unlock(); |
396 |
< |
} |
397 |
< |
}); |
398 |
< |
|
399 |
< |
try { |
400 |
< |
t1.start(); |
401 |
< |
t2.start(); |
402 |
< |
lock.readLock().lock(); |
403 |
< |
lock.readLock().unlock(); |
404 |
< |
Thread.sleep(SHORT_DELAY_MS); |
405 |
< |
lock.readLock().lock(); |
406 |
< |
lock.readLock().unlock(); |
407 |
< |
lock.writeLock().unlock(); |
408 |
< |
t1.join(MEDIUM_DELAY_MS); |
458 |
< |
t2.join(MEDIUM_DELAY_MS); |
459 |
< |
assertTrue(!t1.isAlive()); |
460 |
< |
assertTrue(!t2.isAlive()); |
461 |
< |
|
462 |
< |
} catch(Exception e){ |
463 |
< |
unexpectedException(); |
464 |
< |
} |
383 |
> |
public void testReadHoldingWriteLock2() throws InterruptedException { |
384 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
385 |
> |
lock.writeLock().lock(); |
386 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
387 |
> |
public void realRun() { |
388 |
> |
lock.readLock().lock(); |
389 |
> |
lock.readLock().unlock(); |
390 |
> |
}}); |
391 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
392 |
> |
public void realRun() { |
393 |
> |
lock.readLock().lock(); |
394 |
> |
lock.readLock().unlock(); |
395 |
> |
}}); |
396 |
> |
|
397 |
> |
t1.start(); |
398 |
> |
t2.start(); |
399 |
> |
lock.readLock().lock(); |
400 |
> |
lock.readLock().unlock(); |
401 |
> |
Thread.sleep(SHORT_DELAY_MS); |
402 |
> |
lock.readLock().lock(); |
403 |
> |
lock.readLock().unlock(); |
404 |
> |
lock.writeLock().unlock(); |
405 |
> |
t1.join(MEDIUM_DELAY_MS); |
406 |
> |
t2.join(MEDIUM_DELAY_MS); |
407 |
> |
assertTrue(!t1.isAlive()); |
408 |
> |
assertTrue(!t2.isAlive()); |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
|
* Read lock succeeds if write locked by current thread even if |
413 |
|
* other threads are waiting for writelock |
414 |
|
*/ |
415 |
< |
public void testReadHoldingWriteLock3() { |
416 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
417 |
< |
lock.writeLock().lock(); |
418 |
< |
Thread t1 = new Thread(new Runnable() { |
419 |
< |
public void run() { |
420 |
< |
lock.writeLock().lock(); |
421 |
< |
lock.writeLock().unlock(); |
422 |
< |
} |
423 |
< |
}); |
424 |
< |
Thread t2 = new Thread(new Runnable() { |
425 |
< |
public void run() { |
426 |
< |
lock.writeLock().lock(); |
427 |
< |
lock.writeLock().unlock(); |
484 |
< |
} |
485 |
< |
}); |
486 |
< |
|
487 |
< |
try { |
488 |
< |
t1.start(); |
489 |
< |
t2.start(); |
490 |
< |
lock.readLock().lock(); |
491 |
< |
lock.readLock().unlock(); |
492 |
< |
Thread.sleep(SHORT_DELAY_MS); |
493 |
< |
lock.readLock().lock(); |
494 |
< |
lock.readLock().unlock(); |
495 |
< |
lock.writeLock().unlock(); |
496 |
< |
t1.join(MEDIUM_DELAY_MS); |
497 |
< |
t2.join(MEDIUM_DELAY_MS); |
498 |
< |
assertTrue(!t1.isAlive()); |
499 |
< |
assertTrue(!t2.isAlive()); |
415 |
> |
public void testReadHoldingWriteLock3() throws InterruptedException { |
416 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
417 |
> |
lock.writeLock().lock(); |
418 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
419 |
> |
public void realRun() { |
420 |
> |
lock.writeLock().lock(); |
421 |
> |
lock.writeLock().unlock(); |
422 |
> |
}}); |
423 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
424 |
> |
public void realRun() { |
425 |
> |
lock.writeLock().lock(); |
426 |
> |
lock.writeLock().unlock(); |
427 |
> |
}}); |
428 |
|
|
429 |
< |
} catch(Exception e){ |
430 |
< |
unexpectedException(); |
431 |
< |
} |
429 |
> |
t1.start(); |
430 |
> |
t2.start(); |
431 |
> |
lock.readLock().lock(); |
432 |
> |
lock.readLock().unlock(); |
433 |
> |
Thread.sleep(SHORT_DELAY_MS); |
434 |
> |
lock.readLock().lock(); |
435 |
> |
lock.readLock().unlock(); |
436 |
> |
lock.writeLock().unlock(); |
437 |
> |
t1.join(MEDIUM_DELAY_MS); |
438 |
> |
t2.join(MEDIUM_DELAY_MS); |
439 |
> |
assertTrue(!t1.isAlive()); |
440 |
> |
assertTrue(!t2.isAlive()); |
441 |
|
} |
442 |
|
|
443 |
|
|
445 |
|
* Write lock succeeds if write locked by current thread even if |
446 |
|
* other threads are waiting for writelock |
447 |
|
*/ |
448 |
< |
public void testWriteHoldingWriteLock4() { |
449 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
450 |
< |
lock.writeLock().lock(); |
451 |
< |
Thread t1 = new Thread(new Runnable() { |
452 |
< |
public void run() { |
453 |
< |
lock.writeLock().lock(); |
454 |
< |
lock.writeLock().unlock(); |
455 |
< |
} |
456 |
< |
}); |
457 |
< |
Thread t2 = new Thread(new Runnable() { |
458 |
< |
public void run() { |
459 |
< |
lock.writeLock().lock(); |
460 |
< |
lock.writeLock().unlock(); |
524 |
< |
} |
525 |
< |
}); |
526 |
< |
|
527 |
< |
try { |
528 |
< |
t1.start(); |
529 |
< |
t2.start(); |
530 |
< |
lock.writeLock().lock(); |
531 |
< |
lock.writeLock().unlock(); |
532 |
< |
Thread.sleep(SHORT_DELAY_MS); |
533 |
< |
lock.writeLock().lock(); |
534 |
< |
lock.writeLock().unlock(); |
535 |
< |
lock.writeLock().unlock(); |
536 |
< |
t1.join(MEDIUM_DELAY_MS); |
537 |
< |
t2.join(MEDIUM_DELAY_MS); |
538 |
< |
assertTrue(!t1.isAlive()); |
539 |
< |
assertTrue(!t2.isAlive()); |
448 |
> |
public void testWriteHoldingWriteLock4() throws InterruptedException { |
449 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
450 |
> |
lock.writeLock().lock(); |
451 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
452 |
> |
public void realRun() { |
453 |
> |
lock.writeLock().lock(); |
454 |
> |
lock.writeLock().unlock(); |
455 |
> |
}}); |
456 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
457 |
> |
public void realRun() { |
458 |
> |
lock.writeLock().lock(); |
459 |
> |
lock.writeLock().unlock(); |
460 |
> |
}}); |
461 |
|
|
462 |
< |
} catch(Exception e){ |
463 |
< |
unexpectedException(); |
464 |
< |
} |
462 |
> |
t1.start(); |
463 |
> |
t2.start(); |
464 |
> |
lock.writeLock().lock(); |
465 |
> |
lock.writeLock().unlock(); |
466 |
> |
Thread.sleep(SHORT_DELAY_MS); |
467 |
> |
lock.writeLock().lock(); |
468 |
> |
lock.writeLock().unlock(); |
469 |
> |
lock.writeLock().unlock(); |
470 |
> |
t1.join(MEDIUM_DELAY_MS); |
471 |
> |
t2.join(MEDIUM_DELAY_MS); |
472 |
> |
assertTrue(!t1.isAlive()); |
473 |
> |
assertTrue(!t2.isAlive()); |
474 |
|
} |
475 |
|
|
476 |
|
|
478 |
|
* Fair Read trylock succeeds if write locked by current thread |
479 |
|
*/ |
480 |
|
public void testReadHoldingWriteLockFair() { |
481 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
482 |
< |
lock.writeLock().lock(); |
481 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
482 |
> |
lock.writeLock().lock(); |
483 |
|
assertTrue(lock.readLock().tryLock()); |
484 |
|
lock.readLock().unlock(); |
485 |
|
lock.writeLock().unlock(); |
489 |
|
* Fair Read lock succeeds if write locked by current thread even if |
490 |
|
* other threads are waiting for readlock |
491 |
|
*/ |
492 |
< |
public void testReadHoldingWriteLockFair2() { |
493 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
494 |
< |
lock.writeLock().lock(); |
495 |
< |
Thread t1 = new Thread(new Runnable() { |
496 |
< |
public void run() { |
497 |
< |
lock.readLock().lock(); |
498 |
< |
lock.readLock().unlock(); |
499 |
< |
} |
500 |
< |
}); |
501 |
< |
Thread t2 = new Thread(new Runnable() { |
502 |
< |
public void run() { |
503 |
< |
lock.readLock().lock(); |
504 |
< |
lock.readLock().unlock(); |
505 |
< |
} |
506 |
< |
}); |
507 |
< |
|
508 |
< |
try { |
509 |
< |
t1.start(); |
510 |
< |
t2.start(); |
511 |
< |
lock.readLock().lock(); |
512 |
< |
lock.readLock().unlock(); |
513 |
< |
Thread.sleep(SHORT_DELAY_MS); |
514 |
< |
lock.readLock().lock(); |
515 |
< |
lock.readLock().unlock(); |
516 |
< |
lock.writeLock().unlock(); |
517 |
< |
t1.join(MEDIUM_DELAY_MS); |
588 |
< |
t2.join(MEDIUM_DELAY_MS); |
589 |
< |
assertTrue(!t1.isAlive()); |
590 |
< |
assertTrue(!t2.isAlive()); |
591 |
< |
|
592 |
< |
} catch(Exception e){ |
593 |
< |
unexpectedException(); |
594 |
< |
} |
492 |
> |
public void testReadHoldingWriteLockFair2() throws InterruptedException { |
493 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
494 |
> |
lock.writeLock().lock(); |
495 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
496 |
> |
public void realRun() { |
497 |
> |
lock.readLock().lock(); |
498 |
> |
lock.readLock().unlock(); |
499 |
> |
}}); |
500 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
501 |
> |
public void realRun() { |
502 |
> |
lock.readLock().lock(); |
503 |
> |
lock.readLock().unlock(); |
504 |
> |
}}); |
505 |
> |
|
506 |
> |
t1.start(); |
507 |
> |
t2.start(); |
508 |
> |
lock.readLock().lock(); |
509 |
> |
lock.readLock().unlock(); |
510 |
> |
Thread.sleep(SHORT_DELAY_MS); |
511 |
> |
lock.readLock().lock(); |
512 |
> |
lock.readLock().unlock(); |
513 |
> |
lock.writeLock().unlock(); |
514 |
> |
t1.join(MEDIUM_DELAY_MS); |
515 |
> |
t2.join(MEDIUM_DELAY_MS); |
516 |
> |
assertTrue(!t1.isAlive()); |
517 |
> |
assertTrue(!t2.isAlive()); |
518 |
|
} |
519 |
|
|
520 |
|
|
522 |
|
* Fair Read lock succeeds if write locked by current thread even if |
523 |
|
* other threads are waiting for writelock |
524 |
|
*/ |
525 |
< |
public void testReadHoldingWriteLockFair3() { |
526 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
527 |
< |
lock.writeLock().lock(); |
528 |
< |
Thread t1 = new Thread(new Runnable() { |
529 |
< |
public void run() { |
530 |
< |
lock.writeLock().lock(); |
531 |
< |
lock.writeLock().unlock(); |
532 |
< |
} |
533 |
< |
}); |
534 |
< |
Thread t2 = new Thread(new Runnable() { |
535 |
< |
public void run() { |
536 |
< |
lock.writeLock().lock(); |
537 |
< |
lock.writeLock().unlock(); |
615 |
< |
} |
616 |
< |
}); |
617 |
< |
|
618 |
< |
try { |
619 |
< |
t1.start(); |
620 |
< |
t2.start(); |
621 |
< |
lock.readLock().lock(); |
622 |
< |
lock.readLock().unlock(); |
623 |
< |
Thread.sleep(SHORT_DELAY_MS); |
624 |
< |
lock.readLock().lock(); |
625 |
< |
lock.readLock().unlock(); |
626 |
< |
lock.writeLock().unlock(); |
627 |
< |
t1.join(MEDIUM_DELAY_MS); |
628 |
< |
t2.join(MEDIUM_DELAY_MS); |
629 |
< |
assertTrue(!t1.isAlive()); |
630 |
< |
assertTrue(!t2.isAlive()); |
525 |
> |
public void testReadHoldingWriteLockFair3() throws InterruptedException { |
526 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
527 |
> |
lock.writeLock().lock(); |
528 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
529 |
> |
public void realRun() { |
530 |
> |
lock.writeLock().lock(); |
531 |
> |
lock.writeLock().unlock(); |
532 |
> |
}}); |
533 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
534 |
> |
public void realRun() { |
535 |
> |
lock.writeLock().lock(); |
536 |
> |
lock.writeLock().unlock(); |
537 |
> |
}}); |
538 |
|
|
539 |
< |
} catch(Exception e){ |
540 |
< |
unexpectedException(); |
541 |
< |
} |
539 |
> |
t1.start(); |
540 |
> |
t2.start(); |
541 |
> |
lock.readLock().lock(); |
542 |
> |
lock.readLock().unlock(); |
543 |
> |
Thread.sleep(SHORT_DELAY_MS); |
544 |
> |
lock.readLock().lock(); |
545 |
> |
lock.readLock().unlock(); |
546 |
> |
lock.writeLock().unlock(); |
547 |
> |
t1.join(MEDIUM_DELAY_MS); |
548 |
> |
t2.join(MEDIUM_DELAY_MS); |
549 |
> |
assertTrue(!t1.isAlive()); |
550 |
> |
assertTrue(!t2.isAlive()); |
551 |
|
} |
552 |
|
|
553 |
|
|
555 |
|
* Fair Write lock succeeds if write locked by current thread even if |
556 |
|
* other threads are waiting for writelock |
557 |
|
*/ |
558 |
< |
public void testWriteHoldingWriteLockFair4() { |
559 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
560 |
< |
lock.writeLock().lock(); |
561 |
< |
Thread t1 = new Thread(new Runnable() { |
562 |
< |
public void run() { |
563 |
< |
lock.writeLock().lock(); |
564 |
< |
lock.writeLock().unlock(); |
565 |
< |
} |
566 |
< |
}); |
567 |
< |
Thread t2 = new Thread(new Runnable() { |
568 |
< |
public void run() { |
569 |
< |
lock.writeLock().lock(); |
570 |
< |
lock.writeLock().unlock(); |
655 |
< |
} |
656 |
< |
}); |
657 |
< |
|
658 |
< |
try { |
659 |
< |
t1.start(); |
660 |
< |
t2.start(); |
661 |
< |
Thread.sleep(SHORT_DELAY_MS); |
662 |
< |
assertTrue(lock.isWriteLockedByCurrentThread()); |
663 |
< |
assertTrue(lock.getWriteHoldCount() == 1); |
664 |
< |
lock.writeLock().lock(); |
665 |
< |
assertTrue(lock.getWriteHoldCount() == 2); |
666 |
< |
lock.writeLock().unlock(); |
667 |
< |
lock.writeLock().lock(); |
668 |
< |
lock.writeLock().unlock(); |
669 |
< |
lock.writeLock().unlock(); |
670 |
< |
t1.join(MEDIUM_DELAY_MS); |
671 |
< |
t2.join(MEDIUM_DELAY_MS); |
672 |
< |
assertTrue(!t1.isAlive()); |
673 |
< |
assertTrue(!t2.isAlive()); |
558 |
> |
public void testWriteHoldingWriteLockFair4() throws InterruptedException { |
559 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
560 |
> |
lock.writeLock().lock(); |
561 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
562 |
> |
public void realRun() { |
563 |
> |
lock.writeLock().lock(); |
564 |
> |
lock.writeLock().unlock(); |
565 |
> |
}}); |
566 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
567 |
> |
public void realRun() { |
568 |
> |
lock.writeLock().lock(); |
569 |
> |
lock.writeLock().unlock(); |
570 |
> |
}}); |
571 |
|
|
572 |
< |
} catch(Exception e){ |
573 |
< |
unexpectedException(); |
574 |
< |
} |
572 |
> |
t1.start(); |
573 |
> |
t2.start(); |
574 |
> |
Thread.sleep(SHORT_DELAY_MS); |
575 |
> |
assertTrue(lock.isWriteLockedByCurrentThread()); |
576 |
> |
assertTrue(lock.getWriteHoldCount() == 1); |
577 |
> |
lock.writeLock().lock(); |
578 |
> |
assertTrue(lock.getWriteHoldCount() == 2); |
579 |
> |
lock.writeLock().unlock(); |
580 |
> |
lock.writeLock().lock(); |
581 |
> |
lock.writeLock().unlock(); |
582 |
> |
lock.writeLock().unlock(); |
583 |
> |
t1.join(MEDIUM_DELAY_MS); |
584 |
> |
t2.join(MEDIUM_DELAY_MS); |
585 |
> |
assertTrue(!t1.isAlive()); |
586 |
> |
assertTrue(!t2.isAlive()); |
587 |
|
} |
588 |
|
|
589 |
|
|
590 |
|
/** |
591 |
|
* Read tryLock succeeds if readlocked but not writelocked |
592 |
|
*/ |
593 |
< |
public void testTryLockWhenReadLocked() { |
594 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
595 |
< |
lock.readLock().lock(); |
596 |
< |
Thread t = new Thread(new Runnable() { |
597 |
< |
public void run() { |
598 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
599 |
< |
lock.readLock().unlock(); |
600 |
< |
} |
601 |
< |
}); |
602 |
< |
try { |
603 |
< |
t.start(); |
604 |
< |
t.join(); |
696 |
< |
lock.readLock().unlock(); |
697 |
< |
} catch(Exception e){ |
698 |
< |
unexpectedException(); |
699 |
< |
} |
593 |
> |
public void testTryLockWhenReadLocked() throws InterruptedException { |
594 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
595 |
> |
lock.readLock().lock(); |
596 |
> |
Thread t = new Thread(new CheckedRunnable() { |
597 |
> |
public void realRun() { |
598 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
599 |
> |
lock.readLock().unlock(); |
600 |
> |
}}); |
601 |
> |
|
602 |
> |
t.start(); |
603 |
> |
t.join(); |
604 |
> |
lock.readLock().unlock(); |
605 |
|
} |
606 |
|
|
607 |
|
|
609 |
|
/** |
610 |
|
* write tryLock fails when readlocked |
611 |
|
*/ |
612 |
< |
public void testWriteTryLockWhenReadLocked() { |
613 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
614 |
< |
lock.readLock().lock(); |
615 |
< |
Thread t = new Thread(new Runnable() { |
616 |
< |
public void run() { |
617 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
618 |
< |
} |
619 |
< |
}); |
620 |
< |
try { |
621 |
< |
t.start(); |
622 |
< |
t.join(); |
718 |
< |
lock.readLock().unlock(); |
719 |
< |
} catch(Exception e){ |
720 |
< |
unexpectedException(); |
721 |
< |
} |
612 |
> |
public void testWriteTryLockWhenReadLocked() throws InterruptedException { |
613 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
614 |
> |
lock.readLock().lock(); |
615 |
> |
Thread t = new Thread(new CheckedRunnable() { |
616 |
> |
public void realRun() { |
617 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
618 |
> |
}}); |
619 |
> |
|
620 |
> |
t.start(); |
621 |
> |
t.join(); |
622 |
> |
lock.readLock().unlock(); |
623 |
|
} |
624 |
|
|
625 |
|
|
626 |
|
/** |
627 |
|
* Fair Read tryLock succeeds if readlocked but not writelocked |
628 |
|
*/ |
629 |
< |
public void testTryLockWhenReadLockedFair() { |
630 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
631 |
< |
lock.readLock().lock(); |
632 |
< |
Thread t = new Thread(new Runnable() { |
633 |
< |
public void run() { |
634 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
635 |
< |
lock.readLock().unlock(); |
636 |
< |
} |
637 |
< |
}); |
638 |
< |
try { |
639 |
< |
t.start(); |
640 |
< |
t.join(); |
740 |
< |
lock.readLock().unlock(); |
741 |
< |
} catch(Exception e){ |
742 |
< |
unexpectedException(); |
743 |
< |
} |
629 |
> |
public void testTryLockWhenReadLockedFair() throws InterruptedException { |
630 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
631 |
> |
lock.readLock().lock(); |
632 |
> |
Thread t = new Thread(new CheckedRunnable() { |
633 |
> |
public void realRun() { |
634 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
635 |
> |
lock.readLock().unlock(); |
636 |
> |
}}); |
637 |
> |
|
638 |
> |
t.start(); |
639 |
> |
t.join(); |
640 |
> |
lock.readLock().unlock(); |
641 |
|
} |
642 |
|
|
643 |
|
|
645 |
|
/** |
646 |
|
* Fair write tryLock fails when readlocked |
647 |
|
*/ |
648 |
< |
public void testWriteTryLockWhenReadLockedFair() { |
649 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
650 |
< |
lock.readLock().lock(); |
651 |
< |
Thread t = new Thread(new Runnable() { |
652 |
< |
public void run() { |
653 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
654 |
< |
} |
655 |
< |
}); |
656 |
< |
try { |
657 |
< |
t.start(); |
658 |
< |
t.join(); |
762 |
< |
lock.readLock().unlock(); |
763 |
< |
} catch(Exception e){ |
764 |
< |
unexpectedException(); |
765 |
< |
} |
648 |
> |
public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { |
649 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
650 |
> |
lock.readLock().lock(); |
651 |
> |
Thread t = new Thread(new CheckedRunnable() { |
652 |
> |
public void realRun() { |
653 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
654 |
> |
}}); |
655 |
> |
|
656 |
> |
t.start(); |
657 |
> |
t.join(); |
658 |
> |
lock.readLock().unlock(); |
659 |
|
} |
660 |
|
|
661 |
|
|
663 |
|
/** |
664 |
|
* write timed tryLock times out if locked |
665 |
|
*/ |
666 |
< |
public void testWriteTryLock_Timeout() { |
667 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
668 |
< |
lock.writeLock().lock(); |
669 |
< |
Thread t = new Thread(new Runnable() { |
670 |
< |
public void run() { |
671 |
< |
try { |
672 |
< |
threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
673 |
< |
} catch (Exception ex) { |
674 |
< |
threadUnexpectedException(); |
675 |
< |
} |
676 |
< |
} |
677 |
< |
}); |
785 |
< |
try { |
786 |
< |
t.start(); |
787 |
< |
t.join(); |
788 |
< |
lock.writeLock().unlock(); |
789 |
< |
} catch(Exception e){ |
790 |
< |
unexpectedException(); |
791 |
< |
} |
666 |
> |
public void testWriteTryLock_Timeout() throws InterruptedException { |
667 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
668 |
> |
lock.writeLock().lock(); |
669 |
> |
Thread t = new Thread(new CheckedRunnable() { |
670 |
> |
public void realRun() throws InterruptedException { |
671 |
> |
threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); |
672 |
> |
}}); |
673 |
> |
|
674 |
> |
t.start(); |
675 |
> |
t.join(); |
676 |
> |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
677 |
> |
lock.writeLock().unlock(); |
678 |
|
} |
679 |
|
|
680 |
|
/** |
681 |
|
* read timed tryLock times out if write-locked |
682 |
|
*/ |
683 |
< |
public void testReadTryLock_Timeout() { |
684 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
685 |
< |
lock.writeLock().lock(); |
686 |
< |
Thread t = new Thread(new Runnable() { |
687 |
< |
public void run() { |
688 |
< |
try { |
689 |
< |
threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
690 |
< |
} catch (Exception ex) { |
691 |
< |
threadUnexpectedException(); |
692 |
< |
} |
693 |
< |
} |
694 |
< |
}); |
809 |
< |
try { |
810 |
< |
t.start(); |
811 |
< |
t.join(); |
812 |
< |
lock.writeLock().unlock(); |
813 |
< |
} catch(Exception e){ |
814 |
< |
unexpectedException(); |
815 |
< |
} |
683 |
> |
public void testReadTryLock_Timeout() throws InterruptedException { |
684 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
685 |
> |
lock.writeLock().lock(); |
686 |
> |
Thread t = new Thread(new CheckedRunnable() { |
687 |
> |
public void realRun() throws InterruptedException { |
688 |
> |
threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS)); |
689 |
> |
}}); |
690 |
> |
|
691 |
> |
t.start(); |
692 |
> |
t.join(); |
693 |
> |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
694 |
> |
lock.writeLock().unlock(); |
695 |
|
} |
696 |
|
|
697 |
|
|
698 |
|
/** |
699 |
|
* write lockInterruptibly succeeds if lock free else is interruptible |
700 |
|
*/ |
701 |
< |
public void testWriteLockInterruptibly() { |
702 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
703 |
< |
try { |
704 |
< |
lock.writeLock().lockInterruptibly(); |
705 |
< |
} catch(Exception e) { |
706 |
< |
unexpectedException(); |
707 |
< |
} |
708 |
< |
Thread t = new Thread(new Runnable() { |
709 |
< |
public void run() { |
710 |
< |
try { |
711 |
< |
lock.writeLock().lockInterruptibly(); |
712 |
< |
threadShouldThrow(); |
713 |
< |
} |
714 |
< |
catch(InterruptedException success) { |
836 |
< |
} |
837 |
< |
} |
838 |
< |
}); |
839 |
< |
try { |
840 |
< |
t.start(); |
841 |
< |
Thread.sleep(SHORT_DELAY_MS); |
842 |
< |
t.interrupt(); |
843 |
< |
Thread.sleep(SHORT_DELAY_MS); |
844 |
< |
t.join(); |
845 |
< |
lock.writeLock().unlock(); |
846 |
< |
} catch(Exception e){ |
847 |
< |
unexpectedException(); |
848 |
< |
} |
701 |
> |
public void testWriteLockInterruptibly() throws InterruptedException { |
702 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
703 |
> |
lock.writeLock().lockInterruptibly(); |
704 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
705 |
> |
public void realRun() throws InterruptedException { |
706 |
> |
lock.writeLock().lockInterruptibly(); |
707 |
> |
}}); |
708 |
> |
|
709 |
> |
t.start(); |
710 |
> |
Thread.sleep(SHORT_DELAY_MS); |
711 |
> |
t.interrupt(); |
712 |
> |
Thread.sleep(SHORT_DELAY_MS); |
713 |
> |
t.join(); |
714 |
> |
lock.writeLock().unlock(); |
715 |
|
} |
716 |
|
|
717 |
|
/** |
718 |
|
* read lockInterruptibly succeeds if lock free else is interruptible |
719 |
|
*/ |
720 |
< |
public void testReadLockInterruptibly() { |
721 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
722 |
< |
try { |
723 |
< |
lock.writeLock().lockInterruptibly(); |
724 |
< |
} catch(Exception e) { |
725 |
< |
unexpectedException(); |
726 |
< |
} |
727 |
< |
Thread t = new Thread(new Runnable() { |
728 |
< |
public void run() { |
729 |
< |
try { |
730 |
< |
lock.readLock().lockInterruptibly(); |
731 |
< |
threadShouldThrow(); |
732 |
< |
} |
867 |
< |
catch(InterruptedException success) { |
868 |
< |
} |
869 |
< |
} |
870 |
< |
}); |
871 |
< |
try { |
872 |
< |
t.start(); |
873 |
< |
Thread.sleep(SHORT_DELAY_MS); |
874 |
< |
t.interrupt(); |
875 |
< |
t.join(); |
876 |
< |
lock.writeLock().unlock(); |
877 |
< |
} catch(Exception e){ |
878 |
< |
unexpectedException(); |
879 |
< |
} |
720 |
> |
public void testReadLockInterruptibly() throws InterruptedException { |
721 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
722 |
> |
lock.writeLock().lockInterruptibly(); |
723 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
724 |
> |
public void realRun() throws InterruptedException { |
725 |
> |
lock.readLock().lockInterruptibly(); |
726 |
> |
}}); |
727 |
> |
|
728 |
> |
t.start(); |
729 |
> |
Thread.sleep(SHORT_DELAY_MS); |
730 |
> |
t.interrupt(); |
731 |
> |
t.join(); |
732 |
> |
lock.writeLock().unlock(); |
733 |
|
} |
734 |
|
|
735 |
|
/** |
736 |
|
* Calling await without holding lock throws IllegalMonitorStateException |
737 |
|
*/ |
738 |
< |
public void testAwait_IllegalMonitor() { |
739 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
738 |
> |
public void testAwait_IllegalMonitor() throws InterruptedException { |
739 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
740 |
|
final Condition c = lock.writeLock().newCondition(); |
741 |
|
try { |
742 |
|
c.await(); |
743 |
|
shouldThrow(); |
744 |
< |
} |
892 |
< |
catch (IllegalMonitorStateException success) { |
893 |
< |
} |
894 |
< |
catch (Exception ex) { |
895 |
< |
shouldThrow(); |
896 |
< |
} |
744 |
> |
} catch (IllegalMonitorStateException success) {} |
745 |
|
} |
746 |
|
|
747 |
|
/** |
748 |
|
* Calling signal without holding lock throws IllegalMonitorStateException |
749 |
|
*/ |
750 |
|
public void testSignal_IllegalMonitor() { |
751 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
751 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
752 |
|
final Condition c = lock.writeLock().newCondition(); |
753 |
|
try { |
754 |
|
c.signal(); |
755 |
|
shouldThrow(); |
756 |
< |
} |
909 |
< |
catch (IllegalMonitorStateException success) { |
910 |
< |
} |
911 |
< |
catch (Exception ex) { |
912 |
< |
unexpectedException(); |
913 |
< |
} |
756 |
> |
} catch (IllegalMonitorStateException success) {} |
757 |
|
} |
758 |
|
|
759 |
|
/** |
760 |
|
* awaitNanos without a signal times out |
761 |
|
*/ |
762 |
< |
public void testAwaitNanos_Timeout() { |
763 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
762 |
> |
public void testAwaitNanos_Timeout() throws InterruptedException { |
763 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
764 |
|
final Condition c = lock.writeLock().newCondition(); |
765 |
< |
try { |
766 |
< |
lock.writeLock().lock(); |
767 |
< |
long t = c.awaitNanos(100); |
768 |
< |
assertTrue(t <= 0); |
769 |
< |
lock.writeLock().unlock(); |
927 |
< |
} |
928 |
< |
catch (Exception ex) { |
929 |
< |
unexpectedException(); |
930 |
< |
} |
765 |
> |
|
766 |
> |
lock.writeLock().lock(); |
767 |
> |
long t = c.awaitNanos(100); |
768 |
> |
assertTrue(t <= 0); |
769 |
> |
lock.writeLock().unlock(); |
770 |
|
} |
771 |
|
|
772 |
|
|
773 |
|
/** |
774 |
|
* timed await without a signal times out |
775 |
|
*/ |
776 |
< |
public void testAwait_Timeout() { |
777 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
776 |
> |
public void testAwait_Timeout() throws InterruptedException { |
777 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
778 |
|
final Condition c = lock.writeLock().newCondition(); |
779 |
< |
try { |
780 |
< |
lock.writeLock().lock(); |
781 |
< |
lock.writeLock().unlock(); |
943 |
< |
} |
944 |
< |
catch (Exception ex) { |
945 |
< |
unexpectedException(); |
946 |
< |
} |
779 |
> |
lock.writeLock().lock(); |
780 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
781 |
> |
lock.writeLock().unlock(); |
782 |
|
} |
783 |
|
|
784 |
|
/** |
785 |
|
* awaitUntil without a signal times out |
786 |
|
*/ |
787 |
< |
public void testAwaitUntil_Timeout() { |
788 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
787 |
> |
public void testAwaitUntil_Timeout() throws InterruptedException { |
788 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
789 |
|
final Condition c = lock.writeLock().newCondition(); |
790 |
< |
try { |
791 |
< |
lock.writeLock().lock(); |
792 |
< |
java.util.Date d = new java.util.Date(); |
793 |
< |
lock.writeLock().unlock(); |
959 |
< |
} |
960 |
< |
catch (Exception ex) { |
961 |
< |
unexpectedException(); |
962 |
< |
} |
790 |
> |
lock.writeLock().lock(); |
791 |
> |
java.util.Date d = new java.util.Date(); |
792 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
793 |
> |
lock.writeLock().unlock(); |
794 |
|
} |
795 |
|
|
796 |
|
/** |
797 |
|
* await returns when signalled |
798 |
|
*/ |
799 |
< |
public void testAwait() { |
800 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
799 |
> |
public void testAwait() throws InterruptedException { |
800 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
801 |
|
final Condition c = lock.writeLock().newCondition(); |
802 |
< |
Thread t = new Thread(new Runnable() { |
803 |
< |
public void run() { |
804 |
< |
try { |
805 |
< |
lock.writeLock().lock(); |
806 |
< |
c.await(); |
807 |
< |
lock.writeLock().unlock(); |
977 |
< |
} |
978 |
< |
catch(InterruptedException e) { |
979 |
< |
threadUnexpectedException(); |
980 |
< |
} |
981 |
< |
} |
982 |
< |
}); |
802 |
> |
Thread t = new Thread(new CheckedRunnable() { |
803 |
> |
public void realRun() throws InterruptedException { |
804 |
> |
lock.writeLock().lock(); |
805 |
> |
c.await(); |
806 |
> |
lock.writeLock().unlock(); |
807 |
> |
}}); |
808 |
|
|
809 |
< |
try { |
810 |
< |
t.start(); |
811 |
< |
Thread.sleep(SHORT_DELAY_MS); |
812 |
< |
lock.writeLock().lock(); |
813 |
< |
c.signal(); |
814 |
< |
lock.writeLock().unlock(); |
815 |
< |
t.join(SHORT_DELAY_MS); |
991 |
< |
assertFalse(t.isAlive()); |
992 |
< |
} |
993 |
< |
catch (Exception ex) { |
994 |
< |
unexpectedException(); |
995 |
< |
} |
809 |
> |
t.start(); |
810 |
> |
Thread.sleep(SHORT_DELAY_MS); |
811 |
> |
lock.writeLock().lock(); |
812 |
> |
c.signal(); |
813 |
> |
lock.writeLock().unlock(); |
814 |
> |
t.join(SHORT_DELAY_MS); |
815 |
> |
assertFalse(t.isAlive()); |
816 |
|
} |
817 |
|
|
818 |
|
/** A helper class for uninterruptible wait tests */ |
845 |
|
/** |
846 |
|
* awaitUninterruptibly doesn't abort on interrupt |
847 |
|
*/ |
848 |
< |
public void testAwaitUninterruptibly() { |
848 |
> |
public void testAwaitUninterruptibly() throws InterruptedException { |
849 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
850 |
|
final Condition c = lock.writeLock().newCondition(); |
851 |
|
UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c); |
852 |
|
|
853 |
< |
try { |
1034 |
< |
thread.start(); |
1035 |
< |
|
1036 |
< |
while (!thread.lockStarted) { |
1037 |
< |
Thread.sleep(100); |
1038 |
< |
} |
853 |
> |
thread.start(); |
854 |
|
|
855 |
< |
lock.writeLock().lock(); |
856 |
< |
try { |
857 |
< |
thread.interrupt(); |
1043 |
< |
thread.canAwake = true; |
1044 |
< |
c.signal(); |
1045 |
< |
} finally { |
1046 |
< |
lock.writeLock().unlock(); |
1047 |
< |
} |
855 |
> |
while (!thread.lockStarted) { |
856 |
> |
Thread.sleep(100); |
857 |
> |
} |
858 |
|
|
859 |
< |
thread.join(); |
860 |
< |
assertTrue(thread.interrupted); |
861 |
< |
assertFalse(thread.isAlive()); |
862 |
< |
} catch (Exception ex) { |
863 |
< |
unexpectedException(); |
859 |
> |
lock.writeLock().lock(); |
860 |
> |
try { |
861 |
> |
thread.interrupt(); |
862 |
> |
thread.canAwake = true; |
863 |
> |
c.signal(); |
864 |
> |
} finally { |
865 |
> |
lock.writeLock().unlock(); |
866 |
|
} |
867 |
+ |
|
868 |
+ |
thread.join(); |
869 |
+ |
assertTrue(thread.interrupted); |
870 |
+ |
assertFalse(thread.isAlive()); |
871 |
|
} |
872 |
|
|
873 |
|
/** |
874 |
|
* await is interruptible |
875 |
|
*/ |
876 |
< |
public void testAwait_Interrupt() { |
877 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
876 |
> |
public void testAwait_Interrupt() throws InterruptedException { |
877 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
878 |
|
final Condition c = lock.writeLock().newCondition(); |
879 |
< |
Thread t = new Thread(new Runnable() { |
880 |
< |
public void run() { |
881 |
< |
try { |
882 |
< |
lock.writeLock().lock(); |
883 |
< |
c.await(); |
884 |
< |
lock.writeLock().unlock(); |
1069 |
< |
threadShouldThrow(); |
1070 |
< |
} |
1071 |
< |
catch(InterruptedException success) { |
1072 |
< |
} |
1073 |
< |
} |
1074 |
< |
}); |
879 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
880 |
> |
public void realRun() throws InterruptedException { |
881 |
> |
lock.writeLock().lock(); |
882 |
> |
c.await(); |
883 |
> |
lock.writeLock().unlock(); |
884 |
> |
}}); |
885 |
|
|
886 |
< |
try { |
887 |
< |
t.start(); |
888 |
< |
Thread.sleep(SHORT_DELAY_MS); |
889 |
< |
t.interrupt(); |
890 |
< |
t.join(SHORT_DELAY_MS); |
1081 |
< |
assertFalse(t.isAlive()); |
1082 |
< |
} |
1083 |
< |
catch (Exception ex) { |
1084 |
< |
unexpectedException(); |
1085 |
< |
} |
886 |
> |
t.start(); |
887 |
> |
Thread.sleep(SHORT_DELAY_MS); |
888 |
> |
t.interrupt(); |
889 |
> |
t.join(SHORT_DELAY_MS); |
890 |
> |
assertFalse(t.isAlive()); |
891 |
|
} |
892 |
|
|
893 |
|
/** |
894 |
|
* awaitNanos is interruptible |
895 |
|
*/ |
896 |
< |
public void testAwaitNanos_Interrupt() { |
897 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
896 |
> |
public void testAwaitNanos_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.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
904 |
< |
lock.writeLock().unlock(); |
1100 |
< |
threadShouldThrow(); |
1101 |
< |
} |
1102 |
< |
catch(InterruptedException success) { |
1103 |
< |
} |
1104 |
< |
} |
1105 |
< |
}); |
899 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
900 |
> |
public void realRun() throws InterruptedException { |
901 |
> |
lock.writeLock().lock(); |
902 |
> |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
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); |
1112 |
< |
assertFalse(t.isAlive()); |
1113 |
< |
} |
1114 |
< |
catch (Exception ex) { |
1115 |
< |
unexpectedException(); |
1116 |
< |
} |
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 |
|
* awaitUntil is interruptible |
915 |
|
*/ |
916 |
< |
public void testAwaitUntil_Interrupt() { |
917 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
916 |
> |
public void testAwaitUntil_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 |
< |
java.util.Date d = new java.util.Date(); |
924 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
925 |
< |
lock.writeLock().unlock(); |
1132 |
< |
threadShouldThrow(); |
1133 |
< |
} |
1134 |
< |
catch(InterruptedException success) { |
1135 |
< |
} |
1136 |
< |
} |
1137 |
< |
}); |
919 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
920 |
> |
public void realRun() throws InterruptedException { |
921 |
> |
lock.writeLock().lock(); |
922 |
> |
java.util.Date d = new java.util.Date(); |
923 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
924 |
> |
lock.writeLock().unlock(); |
925 |
> |
}}); |
926 |
|
|
927 |
< |
try { |
928 |
< |
t.start(); |
929 |
< |
Thread.sleep(SHORT_DELAY_MS); |
930 |
< |
t.interrupt(); |
931 |
< |
t.join(SHORT_DELAY_MS); |
1144 |
< |
assertFalse(t.isAlive()); |
1145 |
< |
} |
1146 |
< |
catch (Exception ex) { |
1147 |
< |
unexpectedException(); |
1148 |
< |
} |
927 |
> |
t.start(); |
928 |
> |
Thread.sleep(SHORT_DELAY_MS); |
929 |
> |
t.interrupt(); |
930 |
> |
t.join(SHORT_DELAY_MS); |
931 |
> |
assertFalse(t.isAlive()); |
932 |
|
} |
933 |
|
|
934 |
|
/** |
935 |
|
* signalAll wakes up all threads |
936 |
|
*/ |
937 |
< |
public void testSignalAll() { |
938 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
937 |
> |
public void testSignalAll() throws InterruptedException { |
938 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
939 |
|
final Condition c = lock.writeLock().newCondition(); |
940 |
< |
Thread t1 = new Thread(new Runnable() { |
941 |
< |
public void run() { |
942 |
< |
try { |
943 |
< |
lock.writeLock().lock(); |
944 |
< |
c.await(); |
945 |
< |
lock.writeLock().unlock(); |
1163 |
< |
} |
1164 |
< |
catch(InterruptedException e) { |
1165 |
< |
threadUnexpectedException(); |
1166 |
< |
} |
1167 |
< |
} |
1168 |
< |
}); |
1169 |
< |
|
1170 |
< |
Thread t2 = new Thread(new Runnable() { |
1171 |
< |
public void run() { |
1172 |
< |
try { |
1173 |
< |
lock.writeLock().lock(); |
1174 |
< |
c.await(); |
1175 |
< |
lock.writeLock().unlock(); |
1176 |
< |
} |
1177 |
< |
catch(InterruptedException e) { |
1178 |
< |
threadUnexpectedException(); |
1179 |
< |
} |
1180 |
< |
} |
1181 |
< |
}); |
940 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
941 |
> |
public void realRun() throws InterruptedException { |
942 |
> |
lock.writeLock().lock(); |
943 |
> |
c.await(); |
944 |
> |
lock.writeLock().unlock(); |
945 |
> |
}}); |
946 |
|
|
947 |
< |
try { |
948 |
< |
t1.start(); |
949 |
< |
t2.start(); |
950 |
< |
Thread.sleep(SHORT_DELAY_MS); |
951 |
< |
lock.writeLock().lock(); |
952 |
< |
c.signalAll(); |
953 |
< |
lock.writeLock().unlock(); |
954 |
< |
t1.join(SHORT_DELAY_MS); |
955 |
< |
t2.join(SHORT_DELAY_MS); |
956 |
< |
assertFalse(t1.isAlive()); |
957 |
< |
assertFalse(t2.isAlive()); |
958 |
< |
} |
959 |
< |
catch (Exception ex) { |
960 |
< |
unexpectedException(); |
961 |
< |
} |
947 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
948 |
> |
public void realRun() throws InterruptedException { |
949 |
> |
lock.writeLock().lock(); |
950 |
> |
c.await(); |
951 |
> |
lock.writeLock().unlock(); |
952 |
> |
}}); |
953 |
> |
|
954 |
> |
t1.start(); |
955 |
> |
t2.start(); |
956 |
> |
Thread.sleep(SHORT_DELAY_MS); |
957 |
> |
lock.writeLock().lock(); |
958 |
> |
c.signalAll(); |
959 |
> |
lock.writeLock().unlock(); |
960 |
> |
t1.join(SHORT_DELAY_MS); |
961 |
> |
t2.join(SHORT_DELAY_MS); |
962 |
> |
assertFalse(t1.isAlive()); |
963 |
> |
assertFalse(t2.isAlive()); |
964 |
|
} |
965 |
|
|
966 |
|
/** |
967 |
|
* A serialized lock deserializes as unlocked |
968 |
|
*/ |
969 |
< |
public void testSerialization() { |
969 |
> |
public void testSerialization() throws Exception { |
970 |
|
ReentrantReadWriteLock l = new ReentrantReadWriteLock(); |
971 |
|
l.readLock().lock(); |
972 |
|
l.readLock().unlock(); |
973 |
|
|
974 |
< |
try { |
975 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
976 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
977 |
< |
out.writeObject(l); |
978 |
< |
out.close(); |
979 |
< |
|
980 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
981 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
982 |
< |
ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject(); |
983 |
< |
r.readLock().lock(); |
1218 |
< |
r.readLock().unlock(); |
1219 |
< |
} catch(Exception e){ |
1220 |
< |
e.printStackTrace(); |
1221 |
< |
unexpectedException(); |
1222 |
< |
} |
974 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
975 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
976 |
> |
out.writeObject(l); |
977 |
> |
out.close(); |
978 |
> |
|
979 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
980 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
981 |
> |
ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject(); |
982 |
> |
r.readLock().lock(); |
983 |
> |
r.readLock().unlock(); |
984 |
|
} |
985 |
|
|
986 |
|
/** |
987 |
|
* hasQueuedThreads reports whether there are waiting threads |
988 |
|
*/ |
989 |
< |
public void testhasQueuedThreads() { |
990 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
989 |
> |
public void testhasQueuedThreads() throws InterruptedException { |
990 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
991 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
992 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
993 |
< |
try { |
994 |
< |
assertFalse(lock.hasQueuedThreads()); |
995 |
< |
lock.writeLock().lock(); |
996 |
< |
t1.start(); |
997 |
< |
Thread.sleep(SHORT_DELAY_MS); |
998 |
< |
assertTrue(lock.hasQueuedThreads()); |
999 |
< |
t2.start(); |
1000 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1001 |
< |
assertTrue(lock.hasQueuedThreads()); |
1002 |
< |
t1.interrupt(); |
1003 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1004 |
< |
assertTrue(lock.hasQueuedThreads()); |
1005 |
< |
lock.writeLock().unlock(); |
1006 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1007 |
< |
assertFalse(lock.hasQueuedThreads()); |
1008 |
< |
t1.join(); |
1248 |
< |
t2.join(); |
1249 |
< |
} catch(Exception e){ |
1250 |
< |
unexpectedException(); |
1251 |
< |
} |
993 |
> |
assertFalse(lock.hasQueuedThreads()); |
994 |
> |
lock.writeLock().lock(); |
995 |
> |
t1.start(); |
996 |
> |
Thread.sleep(SHORT_DELAY_MS); |
997 |
> |
assertTrue(lock.hasQueuedThreads()); |
998 |
> |
t2.start(); |
999 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1000 |
> |
assertTrue(lock.hasQueuedThreads()); |
1001 |
> |
t1.interrupt(); |
1002 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1003 |
> |
assertTrue(lock.hasQueuedThreads()); |
1004 |
> |
lock.writeLock().unlock(); |
1005 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1006 |
> |
assertFalse(lock.hasQueuedThreads()); |
1007 |
> |
t1.join(); |
1008 |
> |
t2.join(); |
1009 |
|
} |
1010 |
|
|
1011 |
|
/** |
1012 |
|
* hasQueuedThread(null) throws NPE |
1013 |
|
*/ |
1014 |
|
public void testHasQueuedThreadNPE() { |
1015 |
< |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1015 |
> |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1016 |
|
try { |
1017 |
|
sync.hasQueuedThread(null); |
1018 |
|
shouldThrow(); |
1019 |
< |
} catch (NullPointerException success) { |
1263 |
< |
} |
1019 |
> |
} catch (NullPointerException success) {} |
1020 |
|
} |
1021 |
|
|
1022 |
|
/** |
1023 |
|
* hasQueuedThread reports whether a thread is queued. |
1024 |
|
*/ |
1025 |
< |
public void testHasQueuedThread() { |
1026 |
< |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1025 |
> |
public void testHasQueuedThread() throws InterruptedException { |
1026 |
> |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1027 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
1028 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
1029 |
< |
try { |
1030 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
1031 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
1032 |
< |
sync.writeLock().lock(); |
1033 |
< |
t1.start(); |
1034 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1035 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
1036 |
< |
t2.start(); |
1037 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1038 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
1039 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
1040 |
< |
t1.interrupt(); |
1041 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1042 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
1043 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
1044 |
< |
sync.writeLock().unlock(); |
1045 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1046 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
1047 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1048 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
1049 |
< |
t1.join(); |
1294 |
< |
t2.join(); |
1295 |
< |
} catch(Exception e){ |
1296 |
< |
unexpectedException(); |
1297 |
< |
} |
1029 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
1030 |
> |
assertFalse(sync.hasQueuedThread(t2)); |
1031 |
> |
sync.writeLock().lock(); |
1032 |
> |
t1.start(); |
1033 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1034 |
> |
assertTrue(sync.hasQueuedThread(t1)); |
1035 |
> |
t2.start(); |
1036 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1037 |
> |
assertTrue(sync.hasQueuedThread(t1)); |
1038 |
> |
assertTrue(sync.hasQueuedThread(t2)); |
1039 |
> |
t1.interrupt(); |
1040 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1041 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
1042 |
> |
assertTrue(sync.hasQueuedThread(t2)); |
1043 |
> |
sync.writeLock().unlock(); |
1044 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1045 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
1046 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1047 |
> |
assertFalse(sync.hasQueuedThread(t2)); |
1048 |
> |
t1.join(); |
1049 |
> |
t2.join(); |
1050 |
|
} |
1051 |
|
|
1052 |
|
|
1053 |
|
/** |
1054 |
|
* getQueueLength reports number of waiting threads |
1055 |
|
*/ |
1056 |
< |
public void testGetQueueLength() { |
1057 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1056 |
> |
public void testGetQueueLength() throws InterruptedException { |
1057 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1058 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1059 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1060 |
< |
try { |
1061 |
< |
assertEquals(0, lock.getQueueLength()); |
1062 |
< |
lock.writeLock().lock(); |
1063 |
< |
t1.start(); |
1064 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1065 |
< |
assertEquals(1, lock.getQueueLength()); |
1066 |
< |
t2.start(); |
1067 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1068 |
< |
assertEquals(2, lock.getQueueLength()); |
1069 |
< |
t1.interrupt(); |
1070 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1071 |
< |
assertEquals(1, lock.getQueueLength()); |
1072 |
< |
lock.writeLock().unlock(); |
1073 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1074 |
< |
assertEquals(0, lock.getQueueLength()); |
1075 |
< |
t1.join(); |
1324 |
< |
t2.join(); |
1325 |
< |
} catch(Exception e){ |
1326 |
< |
unexpectedException(); |
1327 |
< |
} |
1060 |
> |
assertEquals(0, lock.getQueueLength()); |
1061 |
> |
lock.writeLock().lock(); |
1062 |
> |
t1.start(); |
1063 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1064 |
> |
assertEquals(1, lock.getQueueLength()); |
1065 |
> |
t2.start(); |
1066 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1067 |
> |
assertEquals(2, lock.getQueueLength()); |
1068 |
> |
t1.interrupt(); |
1069 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1070 |
> |
assertEquals(1, lock.getQueueLength()); |
1071 |
> |
lock.writeLock().unlock(); |
1072 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1073 |
> |
assertEquals(0, lock.getQueueLength()); |
1074 |
> |
t1.join(); |
1075 |
> |
t2.join(); |
1076 |
|
} |
1077 |
|
|
1078 |
|
/** |
1079 |
|
* getQueuedThreads includes waiting threads |
1080 |
|
*/ |
1081 |
< |
public void testGetQueuedThreads() { |
1082 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1081 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
1082 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1083 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1084 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1085 |
< |
try { |
1086 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1087 |
< |
lock.writeLock().lock(); |
1088 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1089 |
< |
t1.start(); |
1090 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1091 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1092 |
< |
t2.start(); |
1093 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1094 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1095 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1096 |
< |
t1.interrupt(); |
1097 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1098 |
< |
assertFalse(lock.getQueuedThreads().contains(t1)); |
1099 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1100 |
< |
lock.writeLock().unlock(); |
1101 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1102 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1103 |
< |
t1.join(); |
1356 |
< |
t2.join(); |
1357 |
< |
} catch(Exception e){ |
1358 |
< |
unexpectedException(); |
1359 |
< |
} |
1085 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1086 |
> |
lock.writeLock().lock(); |
1087 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1088 |
> |
t1.start(); |
1089 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1090 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1091 |
> |
t2.start(); |
1092 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1093 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1094 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1095 |
> |
t1.interrupt(); |
1096 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1097 |
> |
assertFalse(lock.getQueuedThreads().contains(t1)); |
1098 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1099 |
> |
lock.writeLock().unlock(); |
1100 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1101 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1102 |
> |
t1.join(); |
1103 |
> |
t2.join(); |
1104 |
|
} |
1105 |
|
|
1106 |
|
/** |
1107 |
|
* hasWaiters throws NPE if null |
1108 |
|
*/ |
1109 |
|
public void testHasWaitersNPE() { |
1110 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1110 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1111 |
|
try { |
1112 |
|
lock.hasWaiters(null); |
1113 |
|
shouldThrow(); |
1114 |
< |
} catch (NullPointerException success) { |
1371 |
< |
} catch (Exception ex) { |
1372 |
< |
unexpectedException(); |
1373 |
< |
} |
1114 |
> |
} catch (NullPointerException success) {} |
1115 |
|
} |
1116 |
|
|
1117 |
|
/** |
1118 |
|
* getWaitQueueLength throws NPE if null |
1119 |
|
*/ |
1120 |
|
public void testGetWaitQueueLengthNPE() { |
1121 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1121 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1122 |
|
try { |
1123 |
|
lock.getWaitQueueLength(null); |
1124 |
|
shouldThrow(); |
1125 |
< |
} catch (NullPointerException success) { |
1385 |
< |
} catch (Exception ex) { |
1386 |
< |
unexpectedException(); |
1387 |
< |
} |
1125 |
> |
} catch (NullPointerException success) {} |
1126 |
|
} |
1127 |
|
|
1128 |
|
|
1130 |
|
* getWaitingThreads throws NPE if null |
1131 |
|
*/ |
1132 |
|
public void testGetWaitingThreadsNPE() { |
1133 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1133 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1134 |
|
try { |
1135 |
|
lock.getWaitingThreads(null); |
1136 |
|
shouldThrow(); |
1137 |
< |
} catch (NullPointerException success) { |
1400 |
< |
} catch (Exception ex) { |
1401 |
< |
unexpectedException(); |
1402 |
< |
} |
1137 |
> |
} catch (NullPointerException success) {} |
1138 |
|
} |
1139 |
|
|
1140 |
|
/** |
1141 |
|
* hasWaiters throws IAE if not owned |
1142 |
|
*/ |
1143 |
|
public void testHasWaitersIAE() { |
1144 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1145 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1146 |
< |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1144 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1145 |
> |
final Condition c = lock.writeLock().newCondition(); |
1146 |
> |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1147 |
|
try { |
1148 |
|
lock2.hasWaiters(c); |
1149 |
|
shouldThrow(); |
1150 |
< |
} catch (IllegalArgumentException success) { |
1416 |
< |
} catch (Exception ex) { |
1417 |
< |
unexpectedException(); |
1418 |
< |
} |
1150 |
> |
} catch (IllegalArgumentException success) {} |
1151 |
|
} |
1152 |
|
|
1153 |
|
/** |
1154 |
|
* hasWaiters throws IMSE if not locked |
1155 |
|
*/ |
1156 |
|
public void testHasWaitersIMSE() { |
1157 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1158 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1157 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1158 |
> |
final Condition c = lock.writeLock().newCondition(); |
1159 |
|
try { |
1160 |
|
lock.hasWaiters(c); |
1161 |
|
shouldThrow(); |
1162 |
< |
} catch (IllegalMonitorStateException success) { |
1431 |
< |
} catch (Exception ex) { |
1432 |
< |
unexpectedException(); |
1433 |
< |
} |
1162 |
> |
} catch (IllegalMonitorStateException success) {} |
1163 |
|
} |
1164 |
|
|
1165 |
|
|
1167 |
|
* getWaitQueueLength throws IAE if not owned |
1168 |
|
*/ |
1169 |
|
public void testGetWaitQueueLengthIAE() { |
1170 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1171 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1172 |
< |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1170 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1171 |
> |
final Condition c = lock.writeLock().newCondition(); |
1172 |
> |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1173 |
|
try { |
1174 |
|
lock2.getWaitQueueLength(c); |
1175 |
|
shouldThrow(); |
1176 |
< |
} catch (IllegalArgumentException success) { |
1448 |
< |
} catch (Exception ex) { |
1449 |
< |
unexpectedException(); |
1450 |
< |
} |
1176 |
> |
} catch (IllegalArgumentException success) {} |
1177 |
|
} |
1178 |
|
|
1179 |
|
/** |
1180 |
|
* getWaitQueueLength throws IMSE if not locked |
1181 |
|
*/ |
1182 |
|
public void testGetWaitQueueLengthIMSE() { |
1183 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1184 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1183 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1184 |
> |
final Condition c = lock.writeLock().newCondition(); |
1185 |
|
try { |
1186 |
|
lock.getWaitQueueLength(c); |
1187 |
|
shouldThrow(); |
1188 |
< |
} catch (IllegalMonitorStateException success) { |
1463 |
< |
} catch (Exception ex) { |
1464 |
< |
unexpectedException(); |
1465 |
< |
} |
1188 |
> |
} catch (IllegalMonitorStateException success) {} |
1189 |
|
} |
1190 |
|
|
1191 |
|
|
1193 |
|
* getWaitingThreads throws IAE if not owned |
1194 |
|
*/ |
1195 |
|
public void testGetWaitingThreadsIAE() { |
1196 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1197 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1198 |
< |
final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); |
1196 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1197 |
> |
final Condition c = lock.writeLock().newCondition(); |
1198 |
> |
final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); |
1199 |
|
try { |
1200 |
|
lock2.getWaitingThreads(c); |
1201 |
|
shouldThrow(); |
1202 |
< |
} catch (IllegalArgumentException success) { |
1480 |
< |
} catch (Exception ex) { |
1481 |
< |
unexpectedException(); |
1482 |
< |
} |
1202 |
> |
} catch (IllegalArgumentException success) {} |
1203 |
|
} |
1204 |
|
|
1205 |
|
/** |
1206 |
|
* getWaitingThreads throws IMSE if not locked |
1207 |
|
*/ |
1208 |
|
public void testGetWaitingThreadsIMSE() { |
1209 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1210 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1209 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1210 |
> |
final Condition c = lock.writeLock().newCondition(); |
1211 |
|
try { |
1212 |
|
lock.getWaitingThreads(c); |
1213 |
|
shouldThrow(); |
1214 |
< |
} catch (IllegalMonitorStateException success) { |
1495 |
< |
} catch (Exception ex) { |
1496 |
< |
unexpectedException(); |
1497 |
< |
} |
1214 |
> |
} catch (IllegalMonitorStateException success) {} |
1215 |
|
} |
1216 |
|
|
1217 |
|
|
1218 |
|
/** |
1219 |
|
* hasWaiters returns true when a thread is waiting, else false |
1220 |
|
*/ |
1221 |
< |
public void testHasWaiters() { |
1222 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1223 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1224 |
< |
Thread t = new Thread(new Runnable() { |
1225 |
< |
public void run() { |
1226 |
< |
try { |
1227 |
< |
lock.writeLock().lock(); |
1228 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1229 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1230 |
< |
c.await(); |
1231 |
< |
lock.writeLock().unlock(); |
1515 |
< |
} |
1516 |
< |
catch(InterruptedException e) { |
1517 |
< |
threadUnexpectedException(); |
1518 |
< |
} |
1519 |
< |
} |
1520 |
< |
}); |
1221 |
> |
public void testHasWaiters() throws InterruptedException { |
1222 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1223 |
> |
final Condition c = lock.writeLock().newCondition(); |
1224 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1225 |
> |
public void realRun() throws InterruptedException { |
1226 |
> |
lock.writeLock().lock(); |
1227 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
1228 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1229 |
> |
c.await(); |
1230 |
> |
lock.writeLock().unlock(); |
1231 |
> |
}}); |
1232 |
|
|
1233 |
< |
try { |
1234 |
< |
t.start(); |
1235 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1236 |
< |
lock.writeLock().lock(); |
1237 |
< |
assertTrue(lock.hasWaiters(c)); |
1238 |
< |
assertEquals(1, lock.getWaitQueueLength(c)); |
1239 |
< |
c.signal(); |
1240 |
< |
lock.writeLock().unlock(); |
1241 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1242 |
< |
lock.writeLock().lock(); |
1243 |
< |
assertFalse(lock.hasWaiters(c)); |
1244 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
1245 |
< |
lock.writeLock().unlock(); |
1246 |
< |
t.join(SHORT_DELAY_MS); |
1536 |
< |
assertFalse(t.isAlive()); |
1537 |
< |
} |
1538 |
< |
catch (Exception ex) { |
1539 |
< |
unexpectedException(); |
1540 |
< |
} |
1233 |
> |
t.start(); |
1234 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1235 |
> |
lock.writeLock().lock(); |
1236 |
> |
assertTrue(lock.hasWaiters(c)); |
1237 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
1238 |
> |
c.signal(); |
1239 |
> |
lock.writeLock().unlock(); |
1240 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1241 |
> |
lock.writeLock().lock(); |
1242 |
> |
assertFalse(lock.hasWaiters(c)); |
1243 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1244 |
> |
lock.writeLock().unlock(); |
1245 |
> |
t.join(SHORT_DELAY_MS); |
1246 |
> |
assertFalse(t.isAlive()); |
1247 |
|
} |
1248 |
|
|
1249 |
|
/** |
1250 |
|
* getWaitQueueLength returns number of waiting threads |
1251 |
|
*/ |
1252 |
< |
public void testGetWaitQueueLength() { |
1253 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1254 |
< |
final Condition c = (lock.writeLock().newCondition()); |
1255 |
< |
Thread t = new Thread(new Runnable() { |
1256 |
< |
public void run() { |
1257 |
< |
try { |
1258 |
< |
lock.writeLock().lock(); |
1259 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1260 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1261 |
< |
c.await(); |
1262 |
< |
lock.writeLock().unlock(); |
1557 |
< |
} |
1558 |
< |
catch(InterruptedException e) { |
1559 |
< |
threadUnexpectedException(); |
1560 |
< |
} |
1561 |
< |
} |
1562 |
< |
}); |
1252 |
> |
public void testGetWaitQueueLength() throws InterruptedException { |
1253 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1254 |
> |
final Condition c = lock.writeLock().newCondition(); |
1255 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1256 |
> |
public void realRun() throws InterruptedException { |
1257 |
> |
lock.writeLock().lock(); |
1258 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
1259 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1260 |
> |
c.await(); |
1261 |
> |
lock.writeLock().unlock(); |
1262 |
> |
}}); |
1263 |
|
|
1264 |
< |
try { |
1265 |
< |
t.start(); |
1266 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1267 |
< |
lock.writeLock().lock(); |
1268 |
< |
assertTrue(lock.hasWaiters(c)); |
1269 |
< |
assertEquals(1, lock.getWaitQueueLength(c)); |
1270 |
< |
c.signal(); |
1271 |
< |
lock.writeLock().unlock(); |
1272 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1273 |
< |
lock.writeLock().lock(); |
1274 |
< |
assertFalse(lock.hasWaiters(c)); |
1275 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
1276 |
< |
lock.writeLock().unlock(); |
1277 |
< |
t.join(SHORT_DELAY_MS); |
1578 |
< |
assertFalse(t.isAlive()); |
1579 |
< |
} |
1580 |
< |
catch (Exception ex) { |
1581 |
< |
unexpectedException(); |
1582 |
< |
} |
1264 |
> |
t.start(); |
1265 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1266 |
> |
lock.writeLock().lock(); |
1267 |
> |
assertTrue(lock.hasWaiters(c)); |
1268 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
1269 |
> |
c.signal(); |
1270 |
> |
lock.writeLock().unlock(); |
1271 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1272 |
> |
lock.writeLock().lock(); |
1273 |
> |
assertFalse(lock.hasWaiters(c)); |
1274 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1275 |
> |
lock.writeLock().unlock(); |
1276 |
> |
t.join(SHORT_DELAY_MS); |
1277 |
> |
assertFalse(t.isAlive()); |
1278 |
|
} |
1279 |
|
|
1280 |
|
|
1281 |
|
/** |
1282 |
|
* getWaitingThreads returns only and all waiting threads |
1283 |
|
*/ |
1284 |
< |
public void testGetWaitingThreads() { |
1285 |
< |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1284 |
> |
public void testGetWaitingThreads() throws InterruptedException { |
1285 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1286 |
|
final Condition c = lock.writeLock().newCondition(); |
1287 |
< |
Thread t1 = new Thread(new Runnable() { |
1288 |
< |
public void run() { |
1289 |
< |
try { |
1290 |
< |
lock.writeLock().lock(); |
1291 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
1292 |
< |
c.await(); |
1293 |
< |
lock.writeLock().unlock(); |
1599 |
< |
} |
1600 |
< |
catch(InterruptedException e) { |
1601 |
< |
threadUnexpectedException(); |
1602 |
< |
} |
1603 |
< |
} |
1604 |
< |
}); |
1605 |
< |
|
1606 |
< |
Thread t2 = new Thread(new Runnable() { |
1607 |
< |
public void run() { |
1608 |
< |
try { |
1609 |
< |
lock.writeLock().lock(); |
1610 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
1611 |
< |
c.await(); |
1612 |
< |
lock.writeLock().unlock(); |
1613 |
< |
} |
1614 |
< |
catch(InterruptedException e) { |
1615 |
< |
threadUnexpectedException(); |
1616 |
< |
} |
1617 |
< |
} |
1618 |
< |
}); |
1287 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
1288 |
> |
public void realRun() throws InterruptedException { |
1289 |
> |
lock.writeLock().lock(); |
1290 |
> |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
1291 |
> |
c.await(); |
1292 |
> |
lock.writeLock().unlock(); |
1293 |
> |
}}); |
1294 |
|
|
1295 |
< |
try { |
1296 |
< |
lock.writeLock().lock(); |
1297 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1298 |
< |
lock.writeLock().unlock(); |
1299 |
< |
t1.start(); |
1300 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1301 |
< |
t2.start(); |
1302 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1303 |
< |
lock.writeLock().lock(); |
1304 |
< |
assertTrue(lock.hasWaiters(c)); |
1305 |
< |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
1306 |
< |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
1307 |
< |
c.signalAll(); |
1308 |
< |
lock.writeLock().unlock(); |
1309 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1310 |
< |
lock.writeLock().lock(); |
1311 |
< |
assertFalse(lock.hasWaiters(c)); |
1312 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1313 |
< |
lock.writeLock().unlock(); |
1314 |
< |
t1.join(SHORT_DELAY_MS); |
1315 |
< |
t2.join(SHORT_DELAY_MS); |
1316 |
< |
assertFalse(t1.isAlive()); |
1317 |
< |
assertFalse(t2.isAlive()); |
1318 |
< |
} |
1319 |
< |
catch (Exception ex) { |
1320 |
< |
unexpectedException(); |
1321 |
< |
} |
1295 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
1296 |
> |
public void realRun() throws InterruptedException { |
1297 |
> |
lock.writeLock().lock(); |
1298 |
> |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
1299 |
> |
c.await(); |
1300 |
> |
lock.writeLock().unlock(); |
1301 |
> |
}}); |
1302 |
> |
|
1303 |
> |
lock.writeLock().lock(); |
1304 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1305 |
> |
lock.writeLock().unlock(); |
1306 |
> |
t1.start(); |
1307 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1308 |
> |
t2.start(); |
1309 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1310 |
> |
lock.writeLock().lock(); |
1311 |
> |
assertTrue(lock.hasWaiters(c)); |
1312 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
1313 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
1314 |
> |
c.signalAll(); |
1315 |
> |
lock.writeLock().unlock(); |
1316 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1317 |
> |
lock.writeLock().lock(); |
1318 |
> |
assertFalse(lock.hasWaiters(c)); |
1319 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1320 |
> |
lock.writeLock().unlock(); |
1321 |
> |
t1.join(SHORT_DELAY_MS); |
1322 |
> |
t2.join(SHORT_DELAY_MS); |
1323 |
> |
assertFalse(t1.isAlive()); |
1324 |
> |
assertFalse(t2.isAlive()); |
1325 |
|
} |
1326 |
|
|
1327 |
|
/** |