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