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