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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.concurrent.locks.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.util.*; |
11 |
< |
import java.io.*; |
10 |
> |
|
11 |
> |
import java.util.Arrays; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.HashSet; |
14 |
> |
import java.util.concurrent.CountDownLatch; |
15 |
> |
import java.util.concurrent.CyclicBarrier; |
16 |
> |
import java.util.concurrent.locks.Condition; |
17 |
> |
import java.util.concurrent.locks.ReentrantLock; |
18 |
> |
|
19 |
> |
import junit.framework.AssertionFailedError; |
20 |
> |
import junit.framework.Test; |
21 |
> |
import junit.framework.TestSuite; |
22 |
|
|
23 |
|
public class ReentrantLockTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run (suite()); |
25 |
> |
junit.textui.TestRunner.run(suite()); |
26 |
|
} |
27 |
|
public static Test suite() { |
28 |
|
return new TestSuite(ReentrantLockTest.class); |
39 |
|
} |
40 |
|
} |
41 |
|
|
35 |
– |
|
42 |
|
/** |
43 |
|
* A runnable calling lockInterruptibly that expects to be |
44 |
|
* interrupted |
56 |
|
*/ |
57 |
|
static class PublicReentrantLock extends ReentrantLock { |
58 |
|
PublicReentrantLock() { super(); } |
59 |
+ |
PublicReentrantLock(boolean fair) { super(fair); } |
60 |
+ |
public Thread getOwner() { |
61 |
+ |
return super.getOwner(); |
62 |
+ |
} |
63 |
|
public Collection<Thread> getQueuedThreads() { |
64 |
|
return super.getQueuedThreads(); |
65 |
|
} |
66 |
|
public Collection<Thread> getWaitingThreads(Condition c) { |
67 |
|
return super.getWaitingThreads(c); |
68 |
|
} |
69 |
+ |
} |
70 |
|
|
71 |
+ |
/** |
72 |
+ |
* Releases write lock, checking that it had a hold count of 1. |
73 |
+ |
*/ |
74 |
+ |
void releaseLock(PublicReentrantLock lock) { |
75 |
+ |
assertLockedByMoi(lock); |
76 |
+ |
lock.unlock(); |
77 |
+ |
assertFalse(lock.isHeldByCurrentThread()); |
78 |
+ |
assertNotLocked(lock); |
79 |
+ |
} |
80 |
|
|
81 |
+ |
/** |
82 |
+ |
* Spin-waits until lock.hasQueuedThread(t) becomes true. |
83 |
+ |
*/ |
84 |
+ |
void waitForQueuedThread(PublicReentrantLock lock, Thread t) { |
85 |
+ |
long startTime = System.nanoTime(); |
86 |
+ |
while (!lock.hasQueuedThread(t)) { |
87 |
+ |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
88 |
+ |
throw new AssertionFailedError("timed out"); |
89 |
+ |
Thread.yield(); |
90 |
+ |
} |
91 |
+ |
assertTrue(t.isAlive()); |
92 |
+ |
assertNotSame(t, lock.getOwner()); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
< |
* Constructor sets given fairness |
96 |
> |
* Checks that lock is not locked. |
97 |
|
*/ |
98 |
< |
public void testConstructor() { |
99 |
< |
assertFalse(new ReentrantLock().isFair()); |
100 |
< |
assertFalse(new ReentrantLock(false).isFair()); |
101 |
< |
assertTrue(new ReentrantLock(true).isFair()); |
98 |
> |
void assertNotLocked(PublicReentrantLock lock) { |
99 |
> |
assertFalse(lock.isLocked()); |
100 |
> |
assertFalse(lock.isHeldByCurrentThread()); |
101 |
> |
assertNull(lock.getOwner()); |
102 |
> |
assertEquals(0, lock.getHoldCount()); |
103 |
|
} |
104 |
|
|
105 |
|
/** |
106 |
< |
* locking an unlocked lock succeeds |
106 |
> |
* Checks that lock is locked by the given thread. |
107 |
> |
*/ |
108 |
> |
void assertLockedBy(PublicReentrantLock lock, Thread t) { |
109 |
> |
assertTrue(lock.isLocked()); |
110 |
> |
assertSame(t, lock.getOwner()); |
111 |
> |
assertEquals(t == Thread.currentThread(), |
112 |
> |
lock.isHeldByCurrentThread()); |
113 |
> |
assertEquals(t == Thread.currentThread(), |
114 |
> |
lock.getHoldCount() > 0); |
115 |
> |
} |
116 |
> |
|
117 |
> |
/** |
118 |
> |
* Checks that lock is locked by the current thread. |
119 |
> |
*/ |
120 |
> |
void assertLockedByMoi(PublicReentrantLock lock) { |
121 |
> |
assertLockedBy(lock, Thread.currentThread()); |
122 |
> |
} |
123 |
> |
|
124 |
> |
/** |
125 |
> |
* Checks that condition c has no waiters. |
126 |
> |
*/ |
127 |
> |
void assertHasNoWaiters(PublicReentrantLock lock, Condition c) { |
128 |
> |
assertHasWaiters(lock, c, new Thread[] {}); |
129 |
> |
} |
130 |
> |
|
131 |
> |
/** |
132 |
> |
* Checks that condition c has exactly the given waiter threads. |
133 |
> |
*/ |
134 |
> |
void assertHasWaiters(PublicReentrantLock lock, Condition c, |
135 |
> |
Thread... threads) { |
136 |
> |
lock.lock(); |
137 |
> |
assertEquals(threads.length > 0, lock.hasWaiters(c)); |
138 |
> |
assertEquals(threads.length, lock.getWaitQueueLength(c)); |
139 |
> |
assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty()); |
140 |
> |
assertEquals(threads.length, lock.getWaitingThreads(c).size()); |
141 |
> |
assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)), |
142 |
> |
new HashSet<Thread>(Arrays.asList(threads))); |
143 |
> |
lock.unlock(); |
144 |
> |
} |
145 |
> |
|
146 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
147 |
> |
|
148 |
> |
/** |
149 |
> |
* Awaits condition using the specified AwaitMethod. |
150 |
|
*/ |
151 |
< |
public void testLock() { |
152 |
< |
ReentrantLock rl = new ReentrantLock(); |
153 |
< |
rl.lock(); |
154 |
< |
assertTrue(rl.isLocked()); |
155 |
< |
rl.unlock(); |
156 |
< |
assertFalse(rl.isLocked()); |
151 |
> |
void await(Condition c, AwaitMethod awaitMethod) |
152 |
> |
throws InterruptedException { |
153 |
> |
long timeoutMillis = 2 * LONG_DELAY_MS; |
154 |
> |
switch (awaitMethod) { |
155 |
> |
case await: |
156 |
> |
c.await(); |
157 |
> |
break; |
158 |
> |
case awaitTimed: |
159 |
> |
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
160 |
> |
break; |
161 |
> |
case awaitNanos: |
162 |
> |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
163 |
> |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
164 |
> |
assertTrue(nanosRemaining > 0); |
165 |
> |
break; |
166 |
> |
case awaitUntil: |
167 |
> |
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
168 |
> |
break; |
169 |
> |
} |
170 |
|
} |
171 |
|
|
172 |
|
/** |
173 |
< |
* locking an unlocked fair lock succeeds |
174 |
< |
*/ |
175 |
< |
public void testFairLock() { |
176 |
< |
ReentrantLock rl = new ReentrantLock(true); |
177 |
< |
rl.lock(); |
178 |
< |
assertTrue(rl.isLocked()); |
179 |
< |
rl.unlock(); |
173 |
> |
* Constructor sets given fairness, and is in unlocked state |
174 |
> |
*/ |
175 |
> |
public void testConstructor() { |
176 |
> |
PublicReentrantLock lock; |
177 |
> |
|
178 |
> |
lock = new PublicReentrantLock(); |
179 |
> |
assertFalse(lock.isFair()); |
180 |
> |
assertNotLocked(lock); |
181 |
> |
|
182 |
> |
lock = new PublicReentrantLock(true); |
183 |
> |
assertTrue(lock.isFair()); |
184 |
> |
assertNotLocked(lock); |
185 |
> |
|
186 |
> |
lock = new PublicReentrantLock(false); |
187 |
> |
assertFalse(lock.isFair()); |
188 |
> |
assertNotLocked(lock); |
189 |
> |
} |
190 |
> |
|
191 |
> |
/** |
192 |
> |
* locking an unlocked lock succeeds |
193 |
> |
*/ |
194 |
> |
public void testLock() { testLock(false); } |
195 |
> |
public void testLock_fair() { testLock(true); } |
196 |
> |
public void testLock(boolean fair) { |
197 |
> |
PublicReentrantLock lock = new PublicReentrantLock(fair); |
198 |
> |
lock.lock(); |
199 |
> |
assertLockedByMoi(lock); |
200 |
> |
releaseLock(lock); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
|
* Unlocking an unlocked lock throws IllegalMonitorStateException |
205 |
|
*/ |
206 |
< |
public void testUnlock_IllegalMonitorStateException() { |
207 |
< |
ReentrantLock rl = new ReentrantLock(); |
206 |
> |
public void testUnlock_IMSE() { testUnlock_IMSE(false); } |
207 |
> |
public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); } |
208 |
> |
public void testUnlock_IMSE(boolean fair) { |
209 |
> |
ReentrantLock lock = new ReentrantLock(fair); |
210 |
|
try { |
211 |
< |
rl.unlock(); |
211 |
> |
lock.unlock(); |
212 |
|
shouldThrow(); |
213 |
|
} catch (IllegalMonitorStateException success) {} |
214 |
|
} |
216 |
|
/** |
217 |
|
* tryLock on an unlocked lock succeeds |
218 |
|
*/ |
219 |
< |
public void testTryLock() { |
220 |
< |
ReentrantLock rl = new ReentrantLock(); |
221 |
< |
assertTrue(rl.tryLock()); |
222 |
< |
assertTrue(rl.isLocked()); |
223 |
< |
rl.unlock(); |
219 |
> |
public void testTryLock() { testTryLock(false); } |
220 |
> |
public void testTryLock_fair() { testTryLock(true); } |
221 |
> |
public void testTryLock(boolean fair) { |
222 |
> |
PublicReentrantLock lock = new PublicReentrantLock(fair); |
223 |
> |
assertTrue(lock.tryLock()); |
224 |
> |
assertLockedByMoi(lock); |
225 |
> |
assertTrue(lock.tryLock()); |
226 |
> |
assertLockedByMoi(lock); |
227 |
> |
lock.unlock(); |
228 |
> |
releaseLock(lock); |
229 |
|
} |
230 |
|
|
114 |
– |
|
231 |
|
/** |
232 |
|
* hasQueuedThreads reports whether there are waiting threads |
233 |
|
*/ |
234 |
< |
public void testhasQueuedThreads() throws InterruptedException { |
235 |
< |
final ReentrantLock lock = new ReentrantLock(); |
234 |
> |
public void testHasQueuedThreads() { testHasQueuedThreads(false); } |
235 |
> |
public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); } |
236 |
> |
public void testHasQueuedThreads(boolean fair) { |
237 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
238 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
239 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
240 |
|
assertFalse(lock.hasQueuedThreads()); |
241 |
|
lock.lock(); |
242 |
+ |
assertFalse(lock.hasQueuedThreads()); |
243 |
|
t1.start(); |
244 |
< |
Thread.sleep(SHORT_DELAY_MS); |
244 |
> |
waitForQueuedThread(lock, t1); |
245 |
|
assertTrue(lock.hasQueuedThreads()); |
246 |
|
t2.start(); |
247 |
< |
Thread.sleep(SHORT_DELAY_MS); |
247 |
> |
waitForQueuedThread(lock, t2); |
248 |
|
assertTrue(lock.hasQueuedThreads()); |
249 |
|
t1.interrupt(); |
250 |
< |
Thread.sleep(SHORT_DELAY_MS); |
250 |
> |
awaitTermination(t1); |
251 |
|
assertTrue(lock.hasQueuedThreads()); |
252 |
|
lock.unlock(); |
253 |
< |
Thread.sleep(SHORT_DELAY_MS); |
253 |
> |
awaitTermination(t2); |
254 |
|
assertFalse(lock.hasQueuedThreads()); |
136 |
– |
t1.join(); |
137 |
– |
t2.join(); |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
|
* getQueueLength reports number of waiting threads |
259 |
|
*/ |
260 |
< |
public void testGetQueueLength() throws InterruptedException { |
261 |
< |
final ReentrantLock lock = new ReentrantLock(); |
260 |
> |
public void testGetQueueLength() { testGetQueueLength(false); } |
261 |
> |
public void testGetQueueLength_fair() { testGetQueueLength(true); } |
262 |
> |
public void testGetQueueLength(boolean fair) { |
263 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
264 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
265 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
266 |
|
assertEquals(0, lock.getQueueLength()); |
267 |
|
lock.lock(); |
268 |
|
t1.start(); |
269 |
< |
Thread.sleep(SHORT_DELAY_MS); |
269 |
> |
waitForQueuedThread(lock, t1); |
270 |
|
assertEquals(1, lock.getQueueLength()); |
271 |
|
t2.start(); |
272 |
< |
Thread.sleep(SHORT_DELAY_MS); |
272 |
> |
waitForQueuedThread(lock, t2); |
273 |
|
assertEquals(2, lock.getQueueLength()); |
274 |
|
t1.interrupt(); |
275 |
< |
Thread.sleep(SHORT_DELAY_MS); |
275 |
> |
awaitTermination(t1); |
276 |
|
assertEquals(1, lock.getQueueLength()); |
277 |
|
lock.unlock(); |
278 |
< |
Thread.sleep(SHORT_DELAY_MS); |
278 |
> |
awaitTermination(t2); |
279 |
|
assertEquals(0, lock.getQueueLength()); |
161 |
– |
t1.join(); |
162 |
– |
t2.join(); |
163 |
– |
} |
164 |
– |
|
165 |
– |
/** |
166 |
– |
* getQueueLength reports number of waiting threads |
167 |
– |
*/ |
168 |
– |
public void testGetQueueLength_fair() throws InterruptedException { |
169 |
– |
final ReentrantLock lock = new ReentrantLock(true); |
170 |
– |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
171 |
– |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
172 |
– |
assertEquals(0, lock.getQueueLength()); |
173 |
– |
lock.lock(); |
174 |
– |
t1.start(); |
175 |
– |
Thread.sleep(SHORT_DELAY_MS); |
176 |
– |
assertEquals(1, lock.getQueueLength()); |
177 |
– |
t2.start(); |
178 |
– |
Thread.sleep(SHORT_DELAY_MS); |
179 |
– |
assertEquals(2, lock.getQueueLength()); |
180 |
– |
t1.interrupt(); |
181 |
– |
Thread.sleep(SHORT_DELAY_MS); |
182 |
– |
assertEquals(1, lock.getQueueLength()); |
183 |
– |
lock.unlock(); |
184 |
– |
Thread.sleep(SHORT_DELAY_MS); |
185 |
– |
assertEquals(0, lock.getQueueLength()); |
186 |
– |
t1.join(); |
187 |
– |
t2.join(); |
280 |
|
} |
281 |
|
|
282 |
|
/** |
283 |
|
* hasQueuedThread(null) throws NPE |
284 |
|
*/ |
285 |
< |
public void testHasQueuedThreadNPE() { |
286 |
< |
final ReentrantLock sync = new ReentrantLock(); |
285 |
> |
public void testHasQueuedThreadNPE() { testHasQueuedThreadNPE(false); } |
286 |
> |
public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); } |
287 |
> |
public void testHasQueuedThreadNPE(boolean fair) { |
288 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
289 |
|
try { |
290 |
< |
sync.hasQueuedThread(null); |
290 |
> |
lock.hasQueuedThread(null); |
291 |
|
shouldThrow(); |
292 |
|
} catch (NullPointerException success) {} |
293 |
|
} |
294 |
|
|
295 |
|
/** |
296 |
< |
* hasQueuedThread reports whether a thread is queued. |
296 |
> |
* hasQueuedThread reports whether a thread is queued |
297 |
|
*/ |
298 |
< |
public void testHasQueuedThread() throws InterruptedException { |
299 |
< |
final ReentrantLock sync = new ReentrantLock(); |
300 |
< |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
301 |
< |
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
302 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
303 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
304 |
< |
sync.lock(); |
298 |
> |
public void testHasQueuedThread() { testHasQueuedThread(false); } |
299 |
> |
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
300 |
> |
public void testHasQueuedThread(boolean fair) { |
301 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
302 |
> |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
303 |
> |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
304 |
> |
assertFalse(lock.hasQueuedThread(t1)); |
305 |
> |
assertFalse(lock.hasQueuedThread(t2)); |
306 |
> |
lock.lock(); |
307 |
|
t1.start(); |
308 |
< |
Thread.sleep(SHORT_DELAY_MS); |
309 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
308 |
> |
waitForQueuedThread(lock, t1); |
309 |
> |
assertTrue(lock.hasQueuedThread(t1)); |
310 |
> |
assertFalse(lock.hasQueuedThread(t2)); |
311 |
|
t2.start(); |
312 |
< |
Thread.sleep(SHORT_DELAY_MS); |
313 |
< |
assertTrue(sync.hasQueuedThread(t1)); |
314 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
312 |
> |
waitForQueuedThread(lock, t2); |
313 |
> |
assertTrue(lock.hasQueuedThread(t1)); |
314 |
> |
assertTrue(lock.hasQueuedThread(t2)); |
315 |
|
t1.interrupt(); |
316 |
< |
Thread.sleep(SHORT_DELAY_MS); |
317 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
318 |
< |
assertTrue(sync.hasQueuedThread(t2)); |
319 |
< |
sync.unlock(); |
320 |
< |
Thread.sleep(SHORT_DELAY_MS); |
321 |
< |
assertFalse(sync.hasQueuedThread(t1)); |
322 |
< |
Thread.sleep(SHORT_DELAY_MS); |
226 |
< |
assertFalse(sync.hasQueuedThread(t2)); |
227 |
< |
t1.join(); |
228 |
< |
t2.join(); |
316 |
> |
awaitTermination(t1); |
317 |
> |
assertFalse(lock.hasQueuedThread(t1)); |
318 |
> |
assertTrue(lock.hasQueuedThread(t2)); |
319 |
> |
lock.unlock(); |
320 |
> |
awaitTermination(t2); |
321 |
> |
assertFalse(lock.hasQueuedThread(t1)); |
322 |
> |
assertFalse(lock.hasQueuedThread(t2)); |
323 |
|
} |
324 |
|
|
231 |
– |
|
325 |
|
/** |
326 |
|
* getQueuedThreads includes waiting threads |
327 |
|
*/ |
328 |
< |
public void testGetQueuedThreads() throws InterruptedException { |
329 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
328 |
> |
public void testGetQueuedThreads() { testGetQueuedThreads(false); } |
329 |
> |
public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); } |
330 |
> |
public void testGetQueuedThreads(boolean fair) { |
331 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
332 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
333 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
334 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
335 |
|
lock.lock(); |
336 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
337 |
|
t1.start(); |
338 |
< |
Thread.sleep(SHORT_DELAY_MS); |
338 |
> |
waitForQueuedThread(lock, t1); |
339 |
> |
assertEquals(1, lock.getQueuedThreads().size()); |
340 |
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
341 |
|
t2.start(); |
342 |
< |
Thread.sleep(SHORT_DELAY_MS); |
342 |
> |
waitForQueuedThread(lock, t2); |
343 |
> |
assertEquals(2, lock.getQueuedThreads().size()); |
344 |
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
345 |
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
346 |
|
t1.interrupt(); |
347 |
< |
Thread.sleep(SHORT_DELAY_MS); |
347 |
> |
awaitTermination(t1); |
348 |
|
assertFalse(lock.getQueuedThreads().contains(t1)); |
349 |
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
350 |
+ |
assertEquals(1, lock.getQueuedThreads().size()); |
351 |
|
lock.unlock(); |
352 |
< |
Thread.sleep(SHORT_DELAY_MS); |
352 |
> |
awaitTermination(t2); |
353 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
256 |
– |
t1.join(); |
257 |
– |
t2.join(); |
354 |
|
} |
355 |
|
|
260 |
– |
|
356 |
|
/** |
357 |
< |
* timed tryLock is interruptible. |
357 |
> |
* timed tryLock is interruptible |
358 |
|
*/ |
359 |
< |
public void testInterruptedException2() throws InterruptedException { |
360 |
< |
final ReentrantLock lock = new ReentrantLock(); |
359 |
> |
public void testTryLock_Interruptible() { testTryLock_Interruptible(false); } |
360 |
> |
public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); } |
361 |
> |
public void testTryLock_Interruptible(boolean fair) { |
362 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
363 |
|
lock.lock(); |
364 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
364 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
365 |
|
public void realRun() throws InterruptedException { |
366 |
< |
lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS); |
366 |
> |
lock.tryLock(2 * LONG_DELAY_MS, MILLISECONDS); |
367 |
|
}}); |
368 |
|
|
369 |
< |
t.start(); |
369 |
> |
waitForQueuedThread(lock, t); |
370 |
|
t.interrupt(); |
371 |
< |
t.join(); |
371 |
> |
awaitTermination(t); |
372 |
> |
releaseLock(lock); |
373 |
|
} |
374 |
|
|
277 |
– |
|
375 |
|
/** |
376 |
< |
* TryLock on a locked lock fails |
376 |
> |
* tryLock on a locked lock fails |
377 |
|
*/ |
378 |
< |
public void testTryLockWhenLocked() throws InterruptedException { |
379 |
< |
final ReentrantLock lock = new ReentrantLock(); |
378 |
> |
public void testTryLockWhenLocked() { testTryLockWhenLocked(false); } |
379 |
> |
public void testTryLockWhenLocked_fair() { testTryLockWhenLocked(true); } |
380 |
> |
public void testTryLockWhenLocked(boolean fair) { |
381 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
382 |
|
lock.lock(); |
383 |
< |
Thread t = new Thread(new CheckedRunnable() { |
383 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
384 |
|
public void realRun() { |
385 |
< |
threadAssertFalse(lock.tryLock()); |
385 |
> |
assertFalse(lock.tryLock()); |
386 |
|
}}); |
387 |
|
|
388 |
< |
t.start(); |
389 |
< |
t.join(); |
291 |
< |
lock.unlock(); |
388 |
> |
awaitTermination(t); |
389 |
> |
releaseLock(lock); |
390 |
|
} |
391 |
|
|
392 |
|
/** |
393 |
|
* Timed tryLock on a locked lock times out |
394 |
|
*/ |
395 |
< |
public void testTryLock_Timeout() throws InterruptedException { |
396 |
< |
final ReentrantLock lock = new ReentrantLock(); |
395 |
> |
public void testTryLock_Timeout() { testTryLock_Timeout(false); } |
396 |
> |
public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); } |
397 |
> |
public void testTryLock_Timeout(boolean fair) { |
398 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
399 |
|
lock.lock(); |
400 |
< |
Thread t = new Thread(new CheckedRunnable() { |
400 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
401 |
|
public void realRun() throws InterruptedException { |
402 |
< |
threadAssertFalse(lock.tryLock(1, MILLISECONDS)); |
402 |
> |
long startTime = System.nanoTime(); |
403 |
> |
long timeoutMillis = 10; |
404 |
> |
assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS)); |
405 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
406 |
|
}}); |
407 |
|
|
408 |
< |
t.start(); |
409 |
< |
t.join(); |
307 |
< |
lock.unlock(); |
408 |
> |
awaitTermination(t); |
409 |
> |
releaseLock(lock); |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
|
* getHoldCount returns number of recursive holds |
414 |
|
*/ |
415 |
< |
public void testGetHoldCount() { |
416 |
< |
ReentrantLock lock = new ReentrantLock(); |
415 |
> |
public void testGetHoldCount() { testGetHoldCount(false); } |
416 |
> |
public void testGetHoldCount_fair() { testGetHoldCount(true); } |
417 |
> |
public void testGetHoldCount(boolean fair) { |
418 |
> |
ReentrantLock lock = new ReentrantLock(fair); |
419 |
|
for (int i = 1; i <= SIZE; i++) { |
420 |
|
lock.lock(); |
421 |
|
assertEquals(i, lock.getHoldCount()); |
426 |
|
} |
427 |
|
} |
428 |
|
|
325 |
– |
|
429 |
|
/** |
430 |
|
* isLocked is true when locked and false when not |
431 |
|
*/ |
432 |
< |
public void testIsLocked() throws InterruptedException { |
433 |
< |
final ReentrantLock lock = new ReentrantLock(); |
434 |
< |
lock.lock(); |
435 |
< |
assertTrue(lock.isLocked()); |
436 |
< |
lock.unlock(); |
437 |
< |
assertFalse(lock.isLocked()); |
438 |
< |
Thread t = new Thread(new CheckedRunnable() { |
439 |
< |
public void realRun() throws InterruptedException { |
440 |
< |
lock.lock(); |
441 |
< |
Thread.sleep(SMALL_DELAY_MS); |
442 |
< |
lock.unlock(); |
443 |
< |
}}); |
444 |
< |
|
445 |
< |
t.start(); |
446 |
< |
Thread.sleep(SHORT_DELAY_MS); |
447 |
< |
assertTrue(lock.isLocked()); |
448 |
< |
t.join(); |
449 |
< |
assertFalse(lock.isLocked()); |
450 |
< |
} |
451 |
< |
|
452 |
< |
|
453 |
< |
/** |
454 |
< |
* lockInterruptibly is interruptible. |
455 |
< |
*/ |
456 |
< |
public void testLockInterruptibly1() throws InterruptedException { |
457 |
< |
final ReentrantLock lock = new ReentrantLock(); |
458 |
< |
lock.lock(); |
459 |
< |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
460 |
< |
t.start(); |
461 |
< |
Thread.sleep(SHORT_DELAY_MS); |
462 |
< |
t.interrupt(); |
463 |
< |
Thread.sleep(SHORT_DELAY_MS); |
361 |
< |
lock.unlock(); |
362 |
< |
t.join(); |
432 |
> |
public void testIsLocked() { testIsLocked(false); } |
433 |
> |
public void testIsLocked_fair() { testIsLocked(true); } |
434 |
> |
public void testIsLocked(boolean fair) { |
435 |
> |
try { |
436 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
437 |
> |
assertFalse(lock.isLocked()); |
438 |
> |
lock.lock(); |
439 |
> |
assertTrue(lock.isLocked()); |
440 |
> |
lock.lock(); |
441 |
> |
assertTrue(lock.isLocked()); |
442 |
> |
lock.unlock(); |
443 |
> |
assertTrue(lock.isLocked()); |
444 |
> |
lock.unlock(); |
445 |
> |
assertFalse(lock.isLocked()); |
446 |
> |
final CyclicBarrier barrier = new CyclicBarrier(2); |
447 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
448 |
> |
public void realRun() throws Exception { |
449 |
> |
lock.lock(); |
450 |
> |
assertTrue(lock.isLocked()); |
451 |
> |
barrier.await(); |
452 |
> |
barrier.await(); |
453 |
> |
lock.unlock(); |
454 |
> |
}}); |
455 |
> |
|
456 |
> |
barrier.await(); |
457 |
> |
assertTrue(lock.isLocked()); |
458 |
> |
barrier.await(); |
459 |
> |
awaitTermination(t); |
460 |
> |
assertFalse(lock.isLocked()); |
461 |
> |
} catch (Exception e) { |
462 |
> |
threadUnexpectedException(e); |
463 |
> |
} |
464 |
|
} |
465 |
|
|
466 |
|
/** |
467 |
|
* lockInterruptibly succeeds when unlocked, else is interruptible |
468 |
|
*/ |
469 |
< |
public void testLockInterruptibly2() throws InterruptedException { |
470 |
< |
final ReentrantLock lock = new ReentrantLock(); |
471 |
< |
lock.lockInterruptibly(); |
472 |
< |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
473 |
< |
t.start(); |
469 |
> |
public void testLockInterruptibly() { testLockInterruptibly(false); } |
470 |
> |
public void testLockInterruptibly_fair() { testLockInterruptibly(true); } |
471 |
> |
public void testLockInterruptibly(boolean fair) { |
472 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
473 |
> |
try { |
474 |
> |
lock.lockInterruptibly(); |
475 |
> |
} catch (InterruptedException ie) { |
476 |
> |
threadUnexpectedException(ie); |
477 |
> |
} |
478 |
> |
assertLockedByMoi(lock); |
479 |
> |
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
480 |
> |
waitForQueuedThread(lock, t); |
481 |
|
t.interrupt(); |
482 |
|
assertTrue(lock.isLocked()); |
483 |
|
assertTrue(lock.isHeldByCurrentThread()); |
484 |
< |
t.join(); |
484 |
> |
awaitTermination(t); |
485 |
> |
releaseLock(lock); |
486 |
|
} |
487 |
|
|
488 |
|
/** |
489 |
|
* Calling await without holding lock throws IllegalMonitorStateException |
490 |
|
*/ |
491 |
< |
public void testAwait_IllegalMonitor() throws InterruptedException { |
492 |
< |
final ReentrantLock lock = new ReentrantLock(); |
493 |
< |
final Condition c = lock.newCondition(); |
494 |
< |
try { |
495 |
< |
c.await(); |
496 |
< |
shouldThrow(); |
497 |
< |
} catch (IllegalMonitorStateException success) {} |
491 |
> |
public void testAwait_IMSE() { testAwait_IMSE(false); } |
492 |
> |
public void testAwait_IMSE_fair() { testAwait_IMSE(true); } |
493 |
> |
public void testAwait_IMSE(boolean fair) { |
494 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
495 |
> |
final Condition c = lock.newCondition(); |
496 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
497 |
> |
long startTime = System.nanoTime(); |
498 |
> |
try { |
499 |
> |
await(c, awaitMethod); |
500 |
> |
shouldThrow(); |
501 |
> |
} catch (IllegalMonitorStateException success) { |
502 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
503 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
504 |
> |
} |
505 |
|
} |
506 |
|
|
507 |
|
/** |
508 |
|
* Calling signal without holding lock throws IllegalMonitorStateException |
509 |
|
*/ |
510 |
< |
public void testSignal_IllegalMonitor() { |
511 |
< |
final ReentrantLock lock = new ReentrantLock(); |
510 |
> |
public void testSignal_IMSE() { testSignal_IMSE(false); } |
511 |
> |
public void testSignal_IMSE_fair() { testSignal_IMSE(true); } |
512 |
> |
public void testSignal_IMSE(boolean fair) { |
513 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
514 |
|
final Condition c = lock.newCondition(); |
515 |
|
try { |
516 |
|
c.signal(); |
521 |
|
/** |
522 |
|
* awaitNanos without a signal times out |
523 |
|
*/ |
524 |
< |
public void testAwaitNanos_Timeout() throws InterruptedException { |
525 |
< |
final ReentrantLock lock = new ReentrantLock(); |
526 |
< |
final Condition c = lock.newCondition(); |
527 |
< |
lock.lock(); |
528 |
< |
long t = c.awaitNanos(100); |
529 |
< |
assertTrue(t <= 0); |
530 |
< |
lock.unlock(); |
524 |
> |
public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); } |
525 |
> |
public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); } |
526 |
> |
public void testAwaitNanos_Timeout(boolean fair) { |
527 |
> |
try { |
528 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
529 |
> |
final Condition c = lock.newCondition(); |
530 |
> |
lock.lock(); |
531 |
> |
long startTime = System.nanoTime(); |
532 |
> |
long timeoutMillis = 10; |
533 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
534 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
535 |
> |
assertTrue(nanosRemaining <= 0); |
536 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
537 |
> |
lock.unlock(); |
538 |
> |
} catch (InterruptedException e) { |
539 |
> |
threadUnexpectedException(e); |
540 |
> |
} |
541 |
|
} |
542 |
|
|
543 |
|
/** |
544 |
< |
* timed await without a signal times out |
544 |
> |
* timed await without a signal times out |
545 |
|
*/ |
546 |
< |
public void testAwait_Timeout() throws InterruptedException { |
547 |
< |
final ReentrantLock lock = new ReentrantLock(); |
548 |
< |
final Condition c = lock.newCondition(); |
549 |
< |
lock.lock(); |
550 |
< |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
551 |
< |
lock.unlock(); |
546 |
> |
public void testAwait_Timeout() { testAwait_Timeout(false); } |
547 |
> |
public void testAwait_Timeout_fair() { testAwait_Timeout(true); } |
548 |
> |
public void testAwait_Timeout(boolean fair) { |
549 |
> |
try { |
550 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
551 |
> |
final Condition c = lock.newCondition(); |
552 |
> |
lock.lock(); |
553 |
> |
long startTime = System.nanoTime(); |
554 |
> |
long timeoutMillis = 10; |
555 |
> |
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
556 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
557 |
> |
lock.unlock(); |
558 |
> |
} catch (InterruptedException e) { |
559 |
> |
threadUnexpectedException(e); |
560 |
> |
} |
561 |
|
} |
562 |
|
|
563 |
|
/** |
564 |
|
* awaitUntil without a signal times out |
565 |
|
*/ |
566 |
< |
public void testAwaitUntil_Timeout() throws InterruptedException { |
567 |
< |
final ReentrantLock lock = new ReentrantLock(); |
568 |
< |
final Condition c = lock.newCondition(); |
569 |
< |
lock.lock(); |
570 |
< |
java.util.Date d = new java.util.Date(); |
571 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
572 |
< |
lock.unlock(); |
566 |
> |
public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); } |
567 |
> |
public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); } |
568 |
> |
public void testAwaitUntil_Timeout(boolean fair) { |
569 |
> |
try { |
570 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
571 |
> |
final Condition c = lock.newCondition(); |
572 |
> |
lock.lock(); |
573 |
> |
long startTime = System.nanoTime(); |
574 |
> |
long timeoutMillis = 10; |
575 |
> |
java.util.Date d = new java.util.Date(); |
576 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
577 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
578 |
> |
lock.unlock(); |
579 |
> |
} catch (InterruptedException e) { |
580 |
> |
threadUnexpectedException(e); |
581 |
> |
} |
582 |
|
} |
583 |
|
|
584 |
|
/** |
585 |
|
* await returns when signalled |
586 |
|
*/ |
587 |
< |
public void testAwait() throws InterruptedException { |
588 |
< |
final ReentrantLock lock = new ReentrantLock(); |
587 |
> |
public void testAwait() { testAwait(false); } |
588 |
> |
public void testAwait_fair() { testAwait(true); } |
589 |
> |
public void testAwait(boolean fair) { |
590 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
591 |
|
final Condition c = lock.newCondition(); |
592 |
< |
Thread t = new Thread(new CheckedRunnable() { |
592 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
593 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
594 |
|
public void realRun() throws InterruptedException { |
595 |
|
lock.lock(); |
596 |
+ |
locked.countDown(); |
597 |
|
c.await(); |
598 |
|
lock.unlock(); |
599 |
|
}}); |
600 |
|
|
601 |
< |
t.start(); |
452 |
< |
Thread.sleep(SHORT_DELAY_MS); |
601 |
> |
await(locked); |
602 |
|
lock.lock(); |
603 |
+ |
assertHasWaiters(lock, c, t); |
604 |
|
c.signal(); |
605 |
+ |
assertHasNoWaiters(lock, c); |
606 |
+ |
assertTrue(t.isAlive()); |
607 |
|
lock.unlock(); |
608 |
< |
t.join(SHORT_DELAY_MS); |
457 |
< |
assertFalse(t.isAlive()); |
608 |
> |
awaitTermination(t); |
609 |
|
} |
610 |
|
|
611 |
|
/** |
612 |
|
* hasWaiters throws NPE if null |
613 |
|
*/ |
614 |
< |
public void testHasWaitersNPE() { |
615 |
< |
final ReentrantLock lock = new ReentrantLock(); |
614 |
> |
public void testHasWaitersNPE() { testHasWaitersNPE(false); } |
615 |
> |
public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); } |
616 |
> |
public void testHasWaitersNPE(boolean fair) { |
617 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
618 |
|
try { |
619 |
|
lock.hasWaiters(null); |
620 |
|
shouldThrow(); |
624 |
|
/** |
625 |
|
* getWaitQueueLength throws NPE if null |
626 |
|
*/ |
627 |
< |
public void testGetWaitQueueLengthNPE() { |
628 |
< |
final ReentrantLock lock = new ReentrantLock(); |
627 |
> |
public void testGetWaitQueueLengthNPE() { testGetWaitQueueLengthNPE(false); } |
628 |
> |
public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); } |
629 |
> |
public void testGetWaitQueueLengthNPE(boolean fair) { |
630 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
631 |
|
try { |
632 |
|
lock.getWaitQueueLength(null); |
633 |
|
shouldThrow(); |
634 |
|
} catch (NullPointerException success) {} |
635 |
|
} |
636 |
|
|
482 |
– |
|
637 |
|
/** |
638 |
|
* getWaitingThreads throws NPE if null |
639 |
|
*/ |
640 |
< |
public void testGetWaitingThreadsNPE() { |
641 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
640 |
> |
public void testGetWaitingThreadsNPE() { testGetWaitingThreadsNPE(false); } |
641 |
> |
public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); } |
642 |
> |
public void testGetWaitingThreadsNPE(boolean fair) { |
643 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
644 |
|
try { |
645 |
|
lock.getWaitingThreads(null); |
646 |
|
shouldThrow(); |
647 |
|
} catch (NullPointerException success) {} |
648 |
|
} |
649 |
|
|
494 |
– |
|
650 |
|
/** |
651 |
< |
* hasWaiters throws IAE if not owned |
651 |
> |
* hasWaiters throws IllegalArgumentException if not owned |
652 |
|
*/ |
653 |
< |
public void testHasWaitersIAE() { |
654 |
< |
final ReentrantLock lock = new ReentrantLock(); |
653 |
> |
public void testHasWaitersIAE() { testHasWaitersIAE(false); } |
654 |
> |
public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); } |
655 |
> |
public void testHasWaitersIAE(boolean fair) { |
656 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
657 |
|
final Condition c = lock.newCondition(); |
658 |
< |
final ReentrantLock lock2 = new ReentrantLock(); |
658 |
> |
final ReentrantLock lock2 = new ReentrantLock(fair); |
659 |
|
try { |
660 |
|
lock2.hasWaiters(c); |
661 |
|
shouldThrow(); |
663 |
|
} |
664 |
|
|
665 |
|
/** |
666 |
< |
* hasWaiters throws IMSE if not locked |
666 |
> |
* hasWaiters throws IllegalMonitorStateException if not locked |
667 |
|
*/ |
668 |
< |
public void testHasWaitersIMSE() { |
669 |
< |
final ReentrantLock lock = new ReentrantLock(); |
668 |
> |
public void testHasWaitersIMSE() { testHasWaitersIMSE(false); } |
669 |
> |
public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); } |
670 |
> |
public void testHasWaitersIMSE(boolean fair) { |
671 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
672 |
|
final Condition c = lock.newCondition(); |
673 |
|
try { |
674 |
|
lock.hasWaiters(c); |
676 |
|
} catch (IllegalMonitorStateException success) {} |
677 |
|
} |
678 |
|
|
520 |
– |
|
679 |
|
/** |
680 |
< |
* getWaitQueueLength throws IAE if not owned |
680 |
> |
* getWaitQueueLength throws IllegalArgumentException if not owned |
681 |
|
*/ |
682 |
< |
public void testGetWaitQueueLengthIAE() { |
683 |
< |
final ReentrantLock lock = new ReentrantLock(); |
682 |
> |
public void testGetWaitQueueLengthIAE() { testGetWaitQueueLengthIAE(false); } |
683 |
> |
public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); } |
684 |
> |
public void testGetWaitQueueLengthIAE(boolean fair) { |
685 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
686 |
|
final Condition c = lock.newCondition(); |
687 |
< |
final ReentrantLock lock2 = new ReentrantLock(); |
687 |
> |
final ReentrantLock lock2 = new ReentrantLock(fair); |
688 |
|
try { |
689 |
|
lock2.getWaitQueueLength(c); |
690 |
|
shouldThrow(); |
692 |
|
} |
693 |
|
|
694 |
|
/** |
695 |
< |
* getWaitQueueLength throws IMSE if not locked |
695 |
> |
* getWaitQueueLength throws IllegalMonitorStateException if not locked |
696 |
|
*/ |
697 |
< |
public void testGetWaitQueueLengthIMSE() { |
698 |
< |
final ReentrantLock lock = new ReentrantLock(); |
697 |
> |
public void testGetWaitQueueLengthIMSE() { testGetWaitQueueLengthIMSE(false); } |
698 |
> |
public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); } |
699 |
> |
public void testGetWaitQueueLengthIMSE(boolean fair) { |
700 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
701 |
|
final Condition c = lock.newCondition(); |
702 |
|
try { |
703 |
|
lock.getWaitQueueLength(c); |
705 |
|
} catch (IllegalMonitorStateException success) {} |
706 |
|
} |
707 |
|
|
546 |
– |
|
708 |
|
/** |
709 |
< |
* getWaitingThreads throws IAE if not owned |
709 |
> |
* getWaitingThreads throws IllegalArgumentException if not owned |
710 |
|
*/ |
711 |
< |
public void testGetWaitingThreadsIAE() { |
712 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
711 |
> |
public void testGetWaitingThreadsIAE() { testGetWaitingThreadsIAE(false); } |
712 |
> |
public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); } |
713 |
> |
public void testGetWaitingThreadsIAE(boolean fair) { |
714 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
715 |
|
final Condition c = lock.newCondition(); |
716 |
< |
final PublicReentrantLock lock2 = new PublicReentrantLock(); |
716 |
> |
final PublicReentrantLock lock2 = new PublicReentrantLock(fair); |
717 |
|
try { |
718 |
|
lock2.getWaitingThreads(c); |
719 |
|
shouldThrow(); |
721 |
|
} |
722 |
|
|
723 |
|
/** |
724 |
< |
* getWaitingThreads throws IMSE if not locked |
724 |
> |
* getWaitingThreads throws IllegalMonitorStateException if not locked |
725 |
|
*/ |
726 |
< |
public void testGetWaitingThreadsIMSE() { |
727 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
726 |
> |
public void testGetWaitingThreadsIMSE() { testGetWaitingThreadsIMSE(false); } |
727 |
> |
public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); } |
728 |
> |
public void testGetWaitingThreadsIMSE(boolean fair) { |
729 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
730 |
|
final Condition c = lock.newCondition(); |
731 |
|
try { |
732 |
|
lock.getWaitingThreads(c); |
734 |
|
} catch (IllegalMonitorStateException success) {} |
735 |
|
} |
736 |
|
|
572 |
– |
|
737 |
|
/** |
738 |
|
* hasWaiters returns true when a thread is waiting, else false |
739 |
|
*/ |
740 |
< |
public void testHasWaiters() throws InterruptedException { |
741 |
< |
final ReentrantLock lock = new ReentrantLock(); |
740 |
> |
public void testHasWaiters() { testHasWaiters(false); } |
741 |
> |
public void testHasWaiters_fair() { testHasWaiters(true); } |
742 |
> |
public void testHasWaiters(boolean fair) { |
743 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
744 |
|
final Condition c = lock.newCondition(); |
745 |
< |
Thread t = new Thread(new CheckedRunnable() { |
745 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(1); |
746 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
747 |
|
public void realRun() throws InterruptedException { |
748 |
|
lock.lock(); |
749 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
750 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
749 |
> |
assertHasNoWaiters(lock, c); |
750 |
> |
assertFalse(lock.hasWaiters(c)); |
751 |
> |
pleaseSignal.countDown(); |
752 |
|
c.await(); |
753 |
+ |
assertHasNoWaiters(lock, c); |
754 |
+ |
assertFalse(lock.hasWaiters(c)); |
755 |
|
lock.unlock(); |
756 |
|
}}); |
757 |
|
|
758 |
< |
t.start(); |
589 |
< |
Thread.sleep(SHORT_DELAY_MS); |
758 |
> |
await(pleaseSignal); |
759 |
|
lock.lock(); |
760 |
+ |
assertHasWaiters(lock, c, t); |
761 |
|
assertTrue(lock.hasWaiters(c)); |
592 |
– |
assertEquals(1, lock.getWaitQueueLength(c)); |
762 |
|
c.signal(); |
763 |
< |
lock.unlock(); |
595 |
< |
Thread.sleep(SHORT_DELAY_MS); |
596 |
< |
lock.lock(); |
763 |
> |
assertHasNoWaiters(lock, c); |
764 |
|
assertFalse(lock.hasWaiters(c)); |
598 |
– |
assertEquals(0, lock.getWaitQueueLength(c)); |
765 |
|
lock.unlock(); |
766 |
< |
t.join(SHORT_DELAY_MS); |
767 |
< |
assertFalse(t.isAlive()); |
766 |
> |
awaitTermination(t); |
767 |
> |
assertHasNoWaiters(lock, c); |
768 |
|
} |
769 |
|
|
770 |
|
/** |
771 |
|
* getWaitQueueLength returns number of waiting threads |
772 |
|
*/ |
773 |
< |
public void testGetWaitQueueLength() throws InterruptedException { |
774 |
< |
final ReentrantLock lock = new ReentrantLock(); |
773 |
> |
public void testGetWaitQueueLength() { testGetWaitQueueLength(false); } |
774 |
> |
public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); } |
775 |
> |
public void testGetWaitQueueLength(boolean fair) { |
776 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
777 |
|
final Condition c = lock.newCondition(); |
778 |
+ |
final CountDownLatch locked1 = new CountDownLatch(1); |
779 |
+ |
final CountDownLatch locked2 = new CountDownLatch(1); |
780 |
|
Thread t1 = new Thread(new CheckedRunnable() { |
781 |
|
public void realRun() throws InterruptedException { |
782 |
|
lock.lock(); |
783 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
784 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
783 |
> |
assertFalse(lock.hasWaiters(c)); |
784 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
785 |
> |
locked1.countDown(); |
786 |
|
c.await(); |
787 |
|
lock.unlock(); |
788 |
|
}}); |
790 |
|
Thread t2 = new Thread(new CheckedRunnable() { |
791 |
|
public void realRun() throws InterruptedException { |
792 |
|
lock.lock(); |
793 |
< |
threadAssertTrue(lock.hasWaiters(c)); |
794 |
< |
threadAssertEquals(1, lock.getWaitQueueLength(c)); |
793 |
> |
assertTrue(lock.hasWaiters(c)); |
794 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
795 |
> |
locked2.countDown(); |
796 |
|
c.await(); |
797 |
|
lock.unlock(); |
798 |
|
}}); |
799 |
|
|
800 |
+ |
lock.lock(); |
801 |
+ |
assertEquals(0, lock.getWaitQueueLength(c)); |
802 |
+ |
lock.unlock(); |
803 |
+ |
|
804 |
|
t1.start(); |
805 |
< |
Thread.sleep(SHORT_DELAY_MS); |
805 |
> |
await(locked1); |
806 |
> |
|
807 |
> |
lock.lock(); |
808 |
> |
assertHasWaiters(lock, c, t1); |
809 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
810 |
> |
lock.unlock(); |
811 |
> |
|
812 |
|
t2.start(); |
813 |
< |
Thread.sleep(SHORT_DELAY_MS); |
813 |
> |
await(locked2); |
814 |
> |
|
815 |
|
lock.lock(); |
816 |
< |
assertTrue(lock.hasWaiters(c)); |
816 |
> |
assertHasWaiters(lock, c, t1, t2); |
817 |
|
assertEquals(2, lock.getWaitQueueLength(c)); |
818 |
|
c.signalAll(); |
819 |
+ |
assertHasNoWaiters(lock, c); |
820 |
|
lock.unlock(); |
821 |
< |
Thread.sleep(SHORT_DELAY_MS); |
822 |
< |
lock.lock(); |
823 |
< |
assertFalse(lock.hasWaiters(c)); |
824 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
825 |
< |
lock.unlock(); |
642 |
< |
t1.join(SHORT_DELAY_MS); |
643 |
< |
t2.join(SHORT_DELAY_MS); |
644 |
< |
assertFalse(t1.isAlive()); |
645 |
< |
assertFalse(t2.isAlive()); |
821 |
> |
|
822 |
> |
awaitTermination(t1); |
823 |
> |
awaitTermination(t2); |
824 |
> |
|
825 |
> |
assertHasNoWaiters(lock, c); |
826 |
|
} |
827 |
|
|
828 |
|
/** |
829 |
|
* getWaitingThreads returns only and all waiting threads |
830 |
|
*/ |
831 |
< |
public void testGetWaitingThreads() throws InterruptedException { |
832 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
831 |
> |
public void testGetWaitingThreads() { testGetWaitingThreads(false); } |
832 |
> |
public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); } |
833 |
> |
public void testGetWaitingThreads(boolean fair) { |
834 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
835 |
|
final Condition c = lock.newCondition(); |
836 |
+ |
final CountDownLatch locked1 = new CountDownLatch(1); |
837 |
+ |
final CountDownLatch locked2 = new CountDownLatch(1); |
838 |
|
Thread t1 = new Thread(new CheckedRunnable() { |
839 |
|
public void realRun() throws InterruptedException { |
840 |
|
lock.lock(); |
841 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
841 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
842 |
> |
locked1.countDown(); |
843 |
|
c.await(); |
844 |
|
lock.unlock(); |
845 |
|
}}); |
847 |
|
Thread t2 = new Thread(new CheckedRunnable() { |
848 |
|
public void realRun() throws InterruptedException { |
849 |
|
lock.lock(); |
850 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
850 |
> |
assertFalse(lock.getWaitingThreads(c).isEmpty()); |
851 |
> |
locked2.countDown(); |
852 |
|
c.await(); |
853 |
|
lock.unlock(); |
854 |
|
}}); |
856 |
|
lock.lock(); |
857 |
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
858 |
|
lock.unlock(); |
859 |
+ |
|
860 |
|
t1.start(); |
861 |
< |
Thread.sleep(SHORT_DELAY_MS); |
861 |
> |
await(locked1); |
862 |
> |
|
863 |
> |
lock.lock(); |
864 |
> |
assertHasWaiters(lock, c, t1); |
865 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
866 |
> |
assertFalse(lock.getWaitingThreads(c).contains(t2)); |
867 |
> |
assertEquals(1, lock.getWaitingThreads(c).size()); |
868 |
> |
lock.unlock(); |
869 |
> |
|
870 |
|
t2.start(); |
871 |
< |
Thread.sleep(SHORT_DELAY_MS); |
871 |
> |
await(locked2); |
872 |
> |
|
873 |
|
lock.lock(); |
874 |
< |
assertTrue(lock.hasWaiters(c)); |
874 |
> |
assertHasWaiters(lock, c, t1, t2); |
875 |
|
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
876 |
|
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
877 |
+ |
assertEquals(2, lock.getWaitingThreads(c).size()); |
878 |
|
c.signalAll(); |
879 |
+ |
assertHasNoWaiters(lock, c); |
880 |
|
lock.unlock(); |
683 |
– |
Thread.sleep(SHORT_DELAY_MS); |
684 |
– |
lock.lock(); |
685 |
– |
assertFalse(lock.hasWaiters(c)); |
686 |
– |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
687 |
– |
lock.unlock(); |
688 |
– |
t1.join(SHORT_DELAY_MS); |
689 |
– |
t2.join(SHORT_DELAY_MS); |
690 |
– |
assertFalse(t1.isAlive()); |
691 |
– |
assertFalse(t2.isAlive()); |
692 |
– |
} |
693 |
– |
|
694 |
– |
/** A helper class for uninterruptible wait tests */ |
695 |
– |
class UninterruptibleThread extends Thread { |
696 |
– |
private ReentrantLock lock; |
697 |
– |
private Condition c; |
881 |
|
|
882 |
< |
public volatile boolean canAwake = false; |
883 |
< |
public volatile boolean interrupted = false; |
701 |
< |
public volatile boolean lockStarted = false; |
702 |
< |
|
703 |
< |
public UninterruptibleThread(ReentrantLock lock, Condition c) { |
704 |
< |
this.lock = lock; |
705 |
< |
this.c = c; |
706 |
< |
} |
882 |
> |
awaitTermination(t1); |
883 |
> |
awaitTermination(t2); |
884 |
|
|
885 |
< |
public synchronized void run() { |
709 |
< |
lock.lock(); |
710 |
< |
lockStarted = true; |
711 |
< |
|
712 |
< |
while (!canAwake) { |
713 |
< |
c.awaitUninterruptibly(); |
714 |
< |
} |
715 |
< |
|
716 |
< |
interrupted = isInterrupted(); |
717 |
< |
lock.unlock(); |
718 |
< |
} |
885 |
> |
assertHasNoWaiters(lock, c); |
886 |
|
} |
887 |
|
|
888 |
|
/** |
889 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
889 |
> |
* awaitUninterruptibly is uninterruptible |
890 |
|
*/ |
891 |
< |
public void testAwaitUninterruptibly() throws InterruptedException { |
892 |
< |
final ReentrantLock lock = new ReentrantLock(); |
891 |
> |
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
892 |
> |
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
893 |
> |
public void testAwaitUninterruptibly(boolean fair) { |
894 |
> |
final ReentrantLock lock = new ReentrantLock(fair); |
895 |
|
final Condition c = lock.newCondition(); |
896 |
< |
UninterruptibleThread thread = new UninterruptibleThread(lock, c); |
896 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
897 |
|
|
898 |
< |
thread.start(); |
898 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
899 |
> |
public void realRun() { |
900 |
> |
// Interrupt before awaitUninterruptibly |
901 |
> |
lock.lock(); |
902 |
> |
pleaseInterrupt.countDown(); |
903 |
> |
Thread.currentThread().interrupt(); |
904 |
> |
c.awaitUninterruptibly(); |
905 |
> |
assertTrue(Thread.interrupted()); |
906 |
> |
lock.unlock(); |
907 |
> |
}}); |
908 |
|
|
909 |
< |
while (!thread.lockStarted) { |
910 |
< |
Thread.sleep(100); |
911 |
< |
} |
909 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
910 |
> |
public void realRun() { |
911 |
> |
// Interrupt during awaitUninterruptibly |
912 |
> |
lock.lock(); |
913 |
> |
pleaseInterrupt.countDown(); |
914 |
> |
c.awaitUninterruptibly(); |
915 |
> |
assertTrue(Thread.interrupted()); |
916 |
> |
lock.unlock(); |
917 |
> |
}}); |
918 |
|
|
919 |
+ |
await(pleaseInterrupt); |
920 |
|
lock.lock(); |
921 |
< |
try { |
922 |
< |
thread.interrupt(); |
738 |
< |
thread.canAwake = true; |
739 |
< |
c.signal(); |
740 |
< |
} finally { |
741 |
< |
lock.unlock(); |
742 |
< |
} |
921 |
> |
lock.unlock(); |
922 |
> |
t2.interrupt(); |
923 |
|
|
924 |
< |
thread.join(); |
925 |
< |
assertTrue(thread.interrupted); |
746 |
< |
assertFalse(thread.isAlive()); |
747 |
< |
} |
924 |
> |
assertThreadStaysAlive(t1); |
925 |
> |
assertTrue(t2.isAlive()); |
926 |
|
|
927 |
< |
/** |
928 |
< |
* await is interruptible |
929 |
< |
*/ |
752 |
< |
public void testAwait_Interrupt() throws InterruptedException { |
753 |
< |
final ReentrantLock lock = new ReentrantLock(); |
754 |
< |
final Condition c = lock.newCondition(); |
755 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
756 |
< |
public void realRun() throws InterruptedException { |
757 |
< |
lock.lock(); |
758 |
< |
c.await(); |
759 |
< |
}}); |
927 |
> |
lock.lock(); |
928 |
> |
c.signalAll(); |
929 |
> |
lock.unlock(); |
930 |
|
|
931 |
< |
t.start(); |
932 |
< |
Thread.sleep(SHORT_DELAY_MS); |
763 |
< |
t.interrupt(); |
764 |
< |
t.join(SHORT_DELAY_MS); |
765 |
< |
assertFalse(t.isAlive()); |
931 |
> |
awaitTermination(t1); |
932 |
> |
awaitTermination(t2); |
933 |
|
} |
934 |
|
|
935 |
|
/** |
936 |
< |
* awaitNanos is interruptible |
936 |
> |
* await/awaitNanos/awaitUntil is interruptible |
937 |
|
*/ |
938 |
< |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
939 |
< |
final ReentrantLock lock = new ReentrantLock(); |
938 |
> |
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
939 |
> |
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
940 |
> |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
941 |
> |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
942 |
> |
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
943 |
> |
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
944 |
> |
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
945 |
> |
public void testInterruptible_awaitUntil_fair() { testInterruptible(true, AwaitMethod.awaitUntil); } |
946 |
> |
public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) { |
947 |
> |
final PublicReentrantLock lock = |
948 |
> |
new PublicReentrantLock(fair); |
949 |
|
final Condition c = lock.newCondition(); |
950 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
950 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
951 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
952 |
|
public void realRun() throws InterruptedException { |
953 |
|
lock.lock(); |
954 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
954 |
> |
assertLockedByMoi(lock); |
955 |
> |
assertHasNoWaiters(lock, c); |
956 |
> |
pleaseInterrupt.countDown(); |
957 |
> |
try { |
958 |
> |
await(c, awaitMethod); |
959 |
> |
} finally { |
960 |
> |
assertLockedByMoi(lock); |
961 |
> |
assertHasNoWaiters(lock, c); |
962 |
> |
lock.unlock(); |
963 |
> |
assertFalse(Thread.interrupted()); |
964 |
> |
} |
965 |
|
}}); |
966 |
|
|
967 |
< |
t.start(); |
968 |
< |
Thread.sleep(SHORT_DELAY_MS); |
967 |
> |
await(pleaseInterrupt); |
968 |
> |
assertHasWaiters(lock, c, t); |
969 |
|
t.interrupt(); |
970 |
< |
t.join(SHORT_DELAY_MS); |
971 |
< |
assertFalse(t.isAlive()); |
970 |
> |
awaitTermination(t); |
971 |
> |
assertNotLocked(lock); |
972 |
|
} |
973 |
|
|
974 |
|
/** |
975 |
< |
* awaitUntil is interruptible |
975 |
> |
* signalAll wakes up all threads |
976 |
|
*/ |
977 |
< |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
978 |
< |
final ReentrantLock lock = new ReentrantLock(); |
977 |
> |
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
978 |
> |
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
979 |
> |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
980 |
> |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
981 |
> |
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
982 |
> |
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
983 |
> |
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
984 |
> |
public void testSignalAll_awaitUntil_fair() { testSignalAll(true, AwaitMethod.awaitUntil); } |
985 |
> |
public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) { |
986 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
987 |
|
final Condition c = lock.newCondition(); |
988 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
988 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
989 |
> |
class Awaiter extends CheckedRunnable { |
990 |
|
public void realRun() throws InterruptedException { |
991 |
|
lock.lock(); |
992 |
< |
java.util.Date d = new java.util.Date(); |
993 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
994 |
< |
}}); |
992 |
> |
pleaseSignal.countDown(); |
993 |
> |
await(c, awaitMethod); |
994 |
> |
lock.unlock(); |
995 |
> |
} |
996 |
> |
} |
997 |
|
|
998 |
< |
t.start(); |
999 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1000 |
< |
t.interrupt(); |
1001 |
< |
t.join(SHORT_DELAY_MS); |
1002 |
< |
assertFalse(t.isAlive()); |
998 |
> |
Thread t1 = newStartedThread(new Awaiter()); |
999 |
> |
Thread t2 = newStartedThread(new Awaiter()); |
1000 |
> |
|
1001 |
> |
await(pleaseSignal); |
1002 |
> |
lock.lock(); |
1003 |
> |
assertHasWaiters(lock, c, t1, t2); |
1004 |
> |
c.signalAll(); |
1005 |
> |
assertHasNoWaiters(lock, c); |
1006 |
> |
lock.unlock(); |
1007 |
> |
awaitTermination(t1); |
1008 |
> |
awaitTermination(t2); |
1009 |
|
} |
1010 |
|
|
1011 |
|
/** |
1012 |
< |
* signalAll wakes up all threads |
1012 |
> |
* signal wakes up waiting threads in FIFO order |
1013 |
|
*/ |
1014 |
< |
public void testSignalAll() throws InterruptedException { |
1015 |
< |
final ReentrantLock lock = new ReentrantLock(); |
1014 |
> |
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
1015 |
> |
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1016 |
> |
public void testSignalWakesFifo(boolean fair) { |
1017 |
> |
final PublicReentrantLock lock = |
1018 |
> |
new PublicReentrantLock(fair); |
1019 |
|
final Condition c = lock.newCondition(); |
1020 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
1020 |
> |
final CountDownLatch locked1 = new CountDownLatch(1); |
1021 |
> |
final CountDownLatch locked2 = new CountDownLatch(1); |
1022 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1023 |
|
public void realRun() throws InterruptedException { |
1024 |
|
lock.lock(); |
1025 |
+ |
locked1.countDown(); |
1026 |
|
c.await(); |
1027 |
|
lock.unlock(); |
1028 |
|
}}); |
1029 |
|
|
1030 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
1030 |
> |
await(locked1); |
1031 |
> |
|
1032 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1033 |
|
public void realRun() throws InterruptedException { |
1034 |
|
lock.lock(); |
1035 |
+ |
locked2.countDown(); |
1036 |
|
c.await(); |
1037 |
|
lock.unlock(); |
1038 |
|
}}); |
1039 |
|
|
1040 |
< |
t1.start(); |
1041 |
< |
t2.start(); |
829 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1040 |
> |
await(locked2); |
1041 |
> |
|
1042 |
|
lock.lock(); |
1043 |
< |
c.signalAll(); |
1043 |
> |
assertHasWaiters(lock, c, t1, t2); |
1044 |
> |
assertFalse(lock.hasQueuedThreads()); |
1045 |
> |
c.signal(); |
1046 |
> |
assertHasWaiters(lock, c, t2); |
1047 |
> |
assertTrue(lock.hasQueuedThread(t1)); |
1048 |
> |
assertFalse(lock.hasQueuedThread(t2)); |
1049 |
> |
c.signal(); |
1050 |
> |
assertHasNoWaiters(lock, c); |
1051 |
> |
assertTrue(lock.hasQueuedThread(t1)); |
1052 |
> |
assertTrue(lock.hasQueuedThread(t2)); |
1053 |
|
lock.unlock(); |
1054 |
< |
t1.join(SHORT_DELAY_MS); |
1055 |
< |
t2.join(SHORT_DELAY_MS); |
835 |
< |
assertFalse(t1.isAlive()); |
836 |
< |
assertFalse(t2.isAlive()); |
1054 |
> |
awaitTermination(t1); |
1055 |
> |
awaitTermination(t2); |
1056 |
|
} |
1057 |
|
|
1058 |
|
/** |
1059 |
|
* await after multiple reentrant locking preserves lock count |
1060 |
|
*/ |
1061 |
< |
public void testAwaitLockCount() throws InterruptedException { |
1062 |
< |
final ReentrantLock lock = new ReentrantLock(); |
1061 |
> |
public void testAwaitLockCount() { testAwaitLockCount(false); } |
1062 |
> |
public void testAwaitLockCount_fair() { testAwaitLockCount(true); } |
1063 |
> |
public void testAwaitLockCount(boolean fair) { |
1064 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
1065 |
|
final Condition c = lock.newCondition(); |
1066 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
1066 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
1067 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1068 |
|
public void realRun() throws InterruptedException { |
1069 |
|
lock.lock(); |
1070 |
< |
threadAssertEquals(1, lock.getHoldCount()); |
1070 |
> |
assertLockedByMoi(lock); |
1071 |
> |
assertEquals(1, lock.getHoldCount()); |
1072 |
> |
pleaseSignal.countDown(); |
1073 |
|
c.await(); |
1074 |
< |
threadAssertEquals(1, lock.getHoldCount()); |
1074 |
> |
assertLockedByMoi(lock); |
1075 |
> |
assertEquals(1, lock.getHoldCount()); |
1076 |
|
lock.unlock(); |
1077 |
|
}}); |
1078 |
|
|
1079 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
1079 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1080 |
|
public void realRun() throws InterruptedException { |
1081 |
|
lock.lock(); |
1082 |
|
lock.lock(); |
1083 |
< |
threadAssertEquals(2, lock.getHoldCount()); |
1083 |
> |
assertLockedByMoi(lock); |
1084 |
> |
assertEquals(2, lock.getHoldCount()); |
1085 |
> |
pleaseSignal.countDown(); |
1086 |
|
c.await(); |
1087 |
< |
threadAssertEquals(2, lock.getHoldCount()); |
1087 |
> |
assertLockedByMoi(lock); |
1088 |
> |
assertEquals(2, lock.getHoldCount()); |
1089 |
|
lock.unlock(); |
1090 |
|
lock.unlock(); |
1091 |
|
}}); |
1092 |
|
|
1093 |
< |
t1.start(); |
866 |
< |
t2.start(); |
867 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1093 |
> |
await(pleaseSignal); |
1094 |
|
lock.lock(); |
1095 |
+ |
assertHasWaiters(lock, c, t1, t2); |
1096 |
+ |
assertEquals(1, lock.getHoldCount()); |
1097 |
|
c.signalAll(); |
1098 |
+ |
assertHasNoWaiters(lock, c); |
1099 |
|
lock.unlock(); |
1100 |
< |
t1.join(SHORT_DELAY_MS); |
1101 |
< |
t2.join(SHORT_DELAY_MS); |
873 |
< |
assertFalse(t1.isAlive()); |
874 |
< |
assertFalse(t2.isAlive()); |
1100 |
> |
awaitTermination(t1); |
1101 |
> |
awaitTermination(t2); |
1102 |
|
} |
1103 |
|
|
1104 |
|
/** |
1105 |
|
* A serialized lock deserializes as unlocked |
1106 |
|
*/ |
1107 |
< |
public void testSerialization() throws Exception { |
1108 |
< |
ReentrantLock l = new ReentrantLock(); |
1109 |
< |
l.lock(); |
1110 |
< |
l.unlock(); |
1111 |
< |
|
1112 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1113 |
< |
ObjectOutputStream out = |
1114 |
< |
new ObjectOutputStream(new BufferedOutputStream(bout)); |
1115 |
< |
out.writeObject(l); |
1116 |
< |
out.close(); |
1117 |
< |
|
1118 |
< |
ByteArrayInputStream bin = |
1119 |
< |
new ByteArrayInputStream(bout.toByteArray()); |
1120 |
< |
ObjectInputStream in = |
1121 |
< |
new ObjectInputStream(new BufferedInputStream(bin)); |
1122 |
< |
ReentrantLock r = (ReentrantLock) in.readObject(); |
1123 |
< |
r.lock(); |
1124 |
< |
r.unlock(); |
1107 |
> |
public void testSerialization() { testSerialization(false); } |
1108 |
> |
public void testSerialization_fair() { testSerialization(true); } |
1109 |
> |
public void testSerialization(boolean fair) { |
1110 |
> |
ReentrantLock lock = new ReentrantLock(fair); |
1111 |
> |
lock.lock(); |
1112 |
> |
|
1113 |
> |
ReentrantLock clone = serialClone(lock); |
1114 |
> |
assertEquals(lock.isFair(), clone.isFair()); |
1115 |
> |
assertTrue(lock.isLocked()); |
1116 |
> |
assertFalse(clone.isLocked()); |
1117 |
> |
assertEquals(1, lock.getHoldCount()); |
1118 |
> |
assertEquals(0, clone.getHoldCount()); |
1119 |
> |
clone.lock(); |
1120 |
> |
clone.lock(); |
1121 |
> |
assertTrue(clone.isLocked()); |
1122 |
> |
assertEquals(2, clone.getHoldCount()); |
1123 |
> |
assertEquals(1, lock.getHoldCount()); |
1124 |
> |
clone.unlock(); |
1125 |
> |
clone.unlock(); |
1126 |
> |
assertTrue(lock.isLocked()); |
1127 |
> |
assertFalse(clone.isLocked()); |
1128 |
|
} |
1129 |
|
|
1130 |
|
/** |
1131 |
|
* toString indicates current lock state |
1132 |
|
*/ |
1133 |
< |
public void testToString() { |
1134 |
< |
ReentrantLock lock = new ReentrantLock(); |
1135 |
< |
String us = lock.toString(); |
1136 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1133 |
> |
public void testToString() { testToString(false); } |
1134 |
> |
public void testToString_fair() { testToString(true); } |
1135 |
> |
public void testToString(boolean fair) { |
1136 |
> |
ReentrantLock lock = new ReentrantLock(fair); |
1137 |
> |
assertTrue(lock.toString().contains("Unlocked")); |
1138 |
|
lock.lock(); |
1139 |
< |
String ls = lock.toString(); |
1140 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
1139 |
> |
assertTrue(lock.toString().contains("Locked")); |
1140 |
> |
lock.unlock(); |
1141 |
> |
assertTrue(lock.toString().contains("Unlocked")); |
1142 |
|
} |
911 |
– |
|
1143 |
|
} |