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