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