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