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