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