1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.concurrent.locks.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.util.*; |
14 |
|
import java.io.*; |
15 |
|
|
16 |
|
public class ReentrantLockTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(ReentrantLockTest.class); |
21 |
> |
return new TestSuite(ReentrantLockTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
/** |
25 |
|
* A runnable calling lockInterruptibly |
26 |
|
*/ |
27 |
< |
class InterruptibleLockRunnable implements Runnable { |
27 |
> |
class InterruptibleLockRunnable extends CheckedRunnable { |
28 |
|
final ReentrantLock lock; |
29 |
|
InterruptibleLockRunnable(ReentrantLock l) { lock = l; } |
30 |
< |
public void run() { |
31 |
< |
try { |
31 |
< |
lock.lockInterruptibly(); |
32 |
< |
} catch(InterruptedException success){} |
30 |
> |
public void realRun() throws InterruptedException { |
31 |
> |
lock.lockInterruptibly(); |
32 |
|
} |
33 |
|
} |
34 |
|
|
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 |
> |
public Collection<Thread> getQueuedThreads() { |
54 |
> |
return super.getQueuedThreads(); |
55 |
|
} |
56 |
< |
public Collection<Thread> getWaitingThreads(Condition c) { |
57 |
< |
return super.getWaitingThreads(c); |
56 |
> |
public Collection<Thread> getWaitingThreads(Condition c) { |
57 |
> |
return super.getWaitingThreads(c); |
58 |
|
} |
59 |
+ |
} |
60 |
|
|
61 |
+ |
/** |
62 |
+ |
* Checks that condition c has no waiters. |
63 |
+ |
*/ |
64 |
+ |
void assertHasNoWaiters(PublicReentrantLock lock, Condition c) { |
65 |
+ |
assertHasWaiters(lock, c, new Thread[] {}); |
66 |
+ |
} |
67 |
|
|
68 |
+ |
/** |
69 |
+ |
* Checks that condition c has exactly the given waiter threads. |
70 |
+ |
*/ |
71 |
+ |
void assertHasWaiters(PublicReentrantLock lock, Condition c, |
72 |
+ |
Thread... threads) { |
73 |
+ |
lock.lock(); |
74 |
+ |
assertEquals(threads.length > 0, lock.hasWaiters(c)); |
75 |
+ |
assertEquals(threads.length, lock.getWaitQueueLength(c)); |
76 |
+ |
assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty()); |
77 |
+ |
assertEquals(threads.length, lock.getWaitingThreads(c).size()); |
78 |
+ |
assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)), |
79 |
+ |
new HashSet<Thread>(Arrays.asList(threads))); |
80 |
+ |
lock.unlock(); |
81 |
|
} |
82 |
|
|
83 |
|
/** |
84 |
|
* Constructor sets given fairness |
85 |
|
*/ |
86 |
< |
public void testConstructor() { |
87 |
< |
ReentrantLock rl = new ReentrantLock(); |
88 |
< |
assertFalse(rl.isFair()); |
89 |
< |
ReentrantLock r2 = new ReentrantLock(true); |
74 |
< |
assertTrue(r2.isFair()); |
86 |
> |
public void testConstructor() { |
87 |
> |
assertFalse(new ReentrantLock().isFair()); |
88 |
> |
assertFalse(new ReentrantLock(false).isFair()); |
89 |
> |
assertTrue(new ReentrantLock(true).isFair()); |
90 |
|
} |
91 |
|
|
92 |
|
/** |
93 |
|
* locking an unlocked lock succeeds |
94 |
|
*/ |
95 |
< |
public void testLock() { |
96 |
< |
ReentrantLock rl = new ReentrantLock(); |
95 |
> |
public void testLock() { |
96 |
> |
ReentrantLock rl = new ReentrantLock(); |
97 |
|
rl.lock(); |
98 |
|
assertTrue(rl.isLocked()); |
99 |
|
rl.unlock(); |
100 |
+ |
assertFalse(rl.isLocked()); |
101 |
|
} |
102 |
|
|
103 |
|
/** |
104 |
|
* locking an unlocked fair lock succeeds |
105 |
|
*/ |
106 |
< |
public void testFairLock() { |
107 |
< |
ReentrantLock rl = new ReentrantLock(true); |
106 |
> |
public void testFairLock() { |
107 |
> |
ReentrantLock rl = new ReentrantLock(true); |
108 |
|
rl.lock(); |
109 |
|
assertTrue(rl.isLocked()); |
110 |
|
rl.unlock(); |
113 |
|
/** |
114 |
|
* Unlocking an unlocked lock throws IllegalMonitorStateException |
115 |
|
*/ |
116 |
< |
public void testUnlock_IllegalMonitorStateException() { |
117 |
< |
ReentrantLock rl = new ReentrantLock(); |
118 |
< |
try { |
119 |
< |
rl.unlock(); |
120 |
< |
shouldThrow(); |
121 |
< |
|
106 |
< |
} catch(IllegalMonitorStateException success){} |
116 |
> |
public void testUnlock_IllegalMonitorStateException() { |
117 |
> |
ReentrantLock rl = new ReentrantLock(); |
118 |
> |
try { |
119 |
> |
rl.unlock(); |
120 |
> |
shouldThrow(); |
121 |
> |
} catch (IllegalMonitorStateException success) {} |
122 |
|
} |
123 |
|
|
124 |
|
/** |
125 |
|
* tryLock on an unlocked lock succeeds |
126 |
|
*/ |
127 |
< |
public void testTryLock() { |
128 |
< |
ReentrantLock rl = new ReentrantLock(); |
127 |
> |
public void testTryLock() { |
128 |
> |
ReentrantLock rl = new ReentrantLock(); |
129 |
|
assertTrue(rl.tryLock()); |
130 |
|
assertTrue(rl.isLocked()); |
131 |
|
rl.unlock(); |
135 |
|
/** |
136 |
|
* hasQueuedThreads reports whether there are waiting threads |
137 |
|
*/ |
138 |
< |
public void testhasQueuedThreads() { |
139 |
< |
final ReentrantLock lock = new ReentrantLock(); |
138 |
> |
public void testhasQueuedThreads() throws InterruptedException { |
139 |
> |
final ReentrantLock lock = new ReentrantLock(); |
140 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
141 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
142 |
< |
try { |
143 |
< |
assertFalse(lock.hasQueuedThreads()); |
144 |
< |
lock.lock(); |
145 |
< |
t1.start(); |
146 |
< |
Thread.sleep(SHORT_DELAY_MS); |
147 |
< |
assertTrue(lock.hasQueuedThreads()); |
148 |
< |
t2.start(); |
149 |
< |
Thread.sleep(SHORT_DELAY_MS); |
150 |
< |
assertTrue(lock.hasQueuedThreads()); |
151 |
< |
t1.interrupt(); |
152 |
< |
Thread.sleep(SHORT_DELAY_MS); |
153 |
< |
assertTrue(lock.hasQueuedThreads()); |
154 |
< |
lock.unlock(); |
155 |
< |
Thread.sleep(SHORT_DELAY_MS); |
156 |
< |
assertFalse(lock.hasQueuedThreads()); |
157 |
< |
t1.join(); |
158 |
< |
t2.join(); |
144 |
< |
} catch(Exception e){ |
145 |
< |
unexpectedException(); |
146 |
< |
} |
147 |
< |
} |
142 |
> |
assertFalse(lock.hasQueuedThreads()); |
143 |
> |
lock.lock(); |
144 |
> |
t1.start(); |
145 |
> |
delay(SHORT_DELAY_MS); |
146 |
> |
assertTrue(lock.hasQueuedThreads()); |
147 |
> |
t2.start(); |
148 |
> |
delay(SHORT_DELAY_MS); |
149 |
> |
assertTrue(lock.hasQueuedThreads()); |
150 |
> |
t1.interrupt(); |
151 |
> |
delay(SHORT_DELAY_MS); |
152 |
> |
assertTrue(lock.hasQueuedThreads()); |
153 |
> |
lock.unlock(); |
154 |
> |
delay(SHORT_DELAY_MS); |
155 |
> |
assertFalse(lock.hasQueuedThreads()); |
156 |
> |
awaitTermination(t1); |
157 |
> |
awaitTermination(t2); |
158 |
> |
} |
159 |
|
|
160 |
|
/** |
161 |
|
* getQueueLength reports number of waiting threads |
162 |
|
*/ |
163 |
< |
public void testGetQueueLength() { |
164 |
< |
final ReentrantLock lock = new ReentrantLock(); |
163 |
> |
public void testGetQueueLength() throws InterruptedException { |
164 |
> |
final ReentrantLock lock = new ReentrantLock(); |
165 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
166 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
167 |
< |
try { |
168 |
< |
assertEquals(0, lock.getQueueLength()); |
169 |
< |
lock.lock(); |
170 |
< |
t1.start(); |
171 |
< |
Thread.sleep(SHORT_DELAY_MS); |
172 |
< |
assertEquals(1, lock.getQueueLength()); |
173 |
< |
t2.start(); |
174 |
< |
Thread.sleep(SHORT_DELAY_MS); |
175 |
< |
assertEquals(2, lock.getQueueLength()); |
176 |
< |
t1.interrupt(); |
177 |
< |
Thread.sleep(SHORT_DELAY_MS); |
178 |
< |
assertEquals(1, lock.getQueueLength()); |
179 |
< |
lock.unlock(); |
180 |
< |
Thread.sleep(SHORT_DELAY_MS); |
181 |
< |
assertEquals(0, lock.getQueueLength()); |
182 |
< |
t1.join(); |
183 |
< |
t2.join(); |
173 |
< |
} catch(Exception e){ |
174 |
< |
unexpectedException(); |
175 |
< |
} |
176 |
< |
} |
167 |
> |
assertEquals(0, lock.getQueueLength()); |
168 |
> |
lock.lock(); |
169 |
> |
t1.start(); |
170 |
> |
delay(SHORT_DELAY_MS); |
171 |
> |
assertEquals(1, lock.getQueueLength()); |
172 |
> |
t2.start(); |
173 |
> |
delay(SHORT_DELAY_MS); |
174 |
> |
assertEquals(2, lock.getQueueLength()); |
175 |
> |
t1.interrupt(); |
176 |
> |
delay(SHORT_DELAY_MS); |
177 |
> |
assertEquals(1, lock.getQueueLength()); |
178 |
> |
lock.unlock(); |
179 |
> |
delay(SHORT_DELAY_MS); |
180 |
> |
assertEquals(0, lock.getQueueLength()); |
181 |
> |
awaitTermination(t1); |
182 |
> |
awaitTermination(t2); |
183 |
> |
} |
184 |
|
|
185 |
|
/** |
186 |
|
* getQueueLength reports number of waiting threads |
187 |
|
*/ |
188 |
< |
public void testGetQueueLength_fair() { |
189 |
< |
final ReentrantLock lock = new ReentrantLock(true); |
188 |
> |
public void testGetQueueLength_fair() throws InterruptedException { |
189 |
> |
final ReentrantLock lock = new ReentrantLock(true); |
190 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
191 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
192 |
< |
try { |
193 |
< |
assertEquals(0, lock.getQueueLength()); |
194 |
< |
lock.lock(); |
195 |
< |
t1.start(); |
196 |
< |
Thread.sleep(SHORT_DELAY_MS); |
197 |
< |
assertEquals(1, lock.getQueueLength()); |
198 |
< |
t2.start(); |
199 |
< |
Thread.sleep(SHORT_DELAY_MS); |
200 |
< |
assertEquals(2, lock.getQueueLength()); |
201 |
< |
t1.interrupt(); |
202 |
< |
Thread.sleep(SHORT_DELAY_MS); |
203 |
< |
assertEquals(1, lock.getQueueLength()); |
204 |
< |
lock.unlock(); |
205 |
< |
Thread.sleep(SHORT_DELAY_MS); |
206 |
< |
assertEquals(0, lock.getQueueLength()); |
207 |
< |
t1.join(); |
208 |
< |
t2.join(); |
209 |
< |
} catch(Exception e){ |
210 |
< |
unexpectedException(); |
211 |
< |
} |
212 |
< |
} |
192 |
> |
assertEquals(0, lock.getQueueLength()); |
193 |
> |
lock.lock(); |
194 |
> |
t1.start(); |
195 |
> |
delay(SHORT_DELAY_MS); |
196 |
> |
assertEquals(1, lock.getQueueLength()); |
197 |
> |
t2.start(); |
198 |
> |
delay(SHORT_DELAY_MS); |
199 |
> |
assertEquals(2, lock.getQueueLength()); |
200 |
> |
t1.interrupt(); |
201 |
> |
delay(SHORT_DELAY_MS); |
202 |
> |
assertEquals(1, lock.getQueueLength()); |
203 |
> |
lock.unlock(); |
204 |
> |
delay(SHORT_DELAY_MS); |
205 |
> |
assertEquals(0, lock.getQueueLength()); |
206 |
> |
awaitTermination(t1); |
207 |
> |
awaitTermination(t2); |
208 |
> |
} |
209 |
> |
|
210 |
> |
/** |
211 |
> |
* hasQueuedThread(null) throws NPE |
212 |
> |
*/ |
213 |
> |
public void testHasQueuedThreadNPE() { |
214 |
> |
final ReentrantLock sync = new ReentrantLock(); |
215 |
> |
try { |
216 |
> |
sync.hasQueuedThread(null); |
217 |
> |
shouldThrow(); |
218 |
> |
} catch (NullPointerException success) {} |
219 |
> |
} |
220 |
> |
|
221 |
> |
/** |
222 |
> |
* hasQueuedThread reports whether a thread is queued. |
223 |
> |
*/ |
224 |
> |
public void testHasQueuedThread() throws InterruptedException { |
225 |
> |
final ReentrantLock sync = new ReentrantLock(); |
226 |
> |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
227 |
> |
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
228 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
229 |
> |
assertFalse(sync.hasQueuedThread(t2)); |
230 |
> |
sync.lock(); |
231 |
> |
t1.start(); |
232 |
> |
delay(SHORT_DELAY_MS); |
233 |
> |
assertTrue(sync.hasQueuedThread(t1)); |
234 |
> |
t2.start(); |
235 |
> |
delay(SHORT_DELAY_MS); |
236 |
> |
assertTrue(sync.hasQueuedThread(t1)); |
237 |
> |
assertTrue(sync.hasQueuedThread(t2)); |
238 |
> |
t1.interrupt(); |
239 |
> |
delay(SHORT_DELAY_MS); |
240 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
241 |
> |
assertTrue(sync.hasQueuedThread(t2)); |
242 |
> |
sync.unlock(); |
243 |
> |
delay(SHORT_DELAY_MS); |
244 |
> |
assertFalse(sync.hasQueuedThread(t1)); |
245 |
> |
delay(SHORT_DELAY_MS); |
246 |
> |
assertFalse(sync.hasQueuedThread(t2)); |
247 |
> |
awaitTermination(t1); |
248 |
> |
awaitTermination(t2); |
249 |
> |
} |
250 |
> |
|
251 |
|
|
252 |
|
/** |
253 |
|
* getQueuedThreads includes waiting threads |
254 |
|
*/ |
255 |
< |
public void testGetQueuedThreads() { |
256 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
255 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
256 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
257 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
258 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
259 |
< |
try { |
260 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
261 |
< |
lock.lock(); |
262 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
263 |
< |
t1.start(); |
264 |
< |
Thread.sleep(SHORT_DELAY_MS); |
265 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
266 |
< |
t2.start(); |
267 |
< |
Thread.sleep(SHORT_DELAY_MS); |
268 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
269 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
270 |
< |
t1.interrupt(); |
271 |
< |
Thread.sleep(SHORT_DELAY_MS); |
272 |
< |
assertFalse(lock.getQueuedThreads().contains(t1)); |
273 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
274 |
< |
lock.unlock(); |
275 |
< |
Thread.sleep(SHORT_DELAY_MS); |
276 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
277 |
< |
t1.join(); |
278 |
< |
t2.join(); |
234 |
< |
} catch(Exception e){ |
235 |
< |
unexpectedException(); |
236 |
< |
} |
237 |
< |
} |
259 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
260 |
> |
lock.lock(); |
261 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
262 |
> |
t1.start(); |
263 |
> |
delay(SHORT_DELAY_MS); |
264 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
265 |
> |
t2.start(); |
266 |
> |
delay(SHORT_DELAY_MS); |
267 |
> |
assertTrue(lock.getQueuedThreads().contains(t1)); |
268 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
269 |
> |
t1.interrupt(); |
270 |
> |
delay(SHORT_DELAY_MS); |
271 |
> |
assertFalse(lock.getQueuedThreads().contains(t1)); |
272 |
> |
assertTrue(lock.getQueuedThreads().contains(t2)); |
273 |
> |
lock.unlock(); |
274 |
> |
delay(SHORT_DELAY_MS); |
275 |
> |
assertTrue(lock.getQueuedThreads().isEmpty()); |
276 |
> |
awaitTermination(t1); |
277 |
> |
awaitTermination(t2); |
278 |
> |
} |
279 |
|
|
280 |
|
|
281 |
|
/** |
282 |
|
* timed tryLock is interruptible. |
283 |
|
*/ |
284 |
< |
public void testInterruptedException2() { |
285 |
< |
final ReentrantLock lock = new ReentrantLock(); |
286 |
< |
lock.lock(); |
287 |
< |
Thread t = new Thread(new Runnable() { |
288 |
< |
public void run() { |
289 |
< |
try { |
290 |
< |
lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); |
291 |
< |
threadShouldThrow(); |
292 |
< |
} catch(InterruptedException success){} |
293 |
< |
} |
294 |
< |
}); |
254 |
< |
try { |
255 |
< |
t.start(); |
256 |
< |
t.interrupt(); |
257 |
< |
} catch(Exception e){ |
258 |
< |
unexpectedException(); |
259 |
< |
} |
284 |
> |
public void testInterruptedException2() throws InterruptedException { |
285 |
> |
final ReentrantLock lock = new ReentrantLock(); |
286 |
> |
lock.lock(); |
287 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
288 |
> |
public void realRun() throws InterruptedException { |
289 |
> |
lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS); |
290 |
> |
}}); |
291 |
> |
|
292 |
> |
delay(SHORT_DELAY_MS); |
293 |
> |
t.interrupt(); |
294 |
> |
awaitTermination(t); |
295 |
|
} |
296 |
|
|
297 |
|
|
298 |
|
/** |
299 |
|
* TryLock on a locked lock fails |
300 |
|
*/ |
301 |
< |
public void testTryLockWhenLocked() { |
302 |
< |
final ReentrantLock lock = new ReentrantLock(); |
303 |
< |
lock.lock(); |
304 |
< |
Thread t = new Thread(new Runnable() { |
305 |
< |
public void run() { |
306 |
< |
threadAssertFalse(lock.tryLock()); |
307 |
< |
} |
308 |
< |
}); |
309 |
< |
try { |
310 |
< |
t.start(); |
311 |
< |
t.join(); |
277 |
< |
lock.unlock(); |
278 |
< |
} catch(Exception e){ |
279 |
< |
unexpectedException(); |
280 |
< |
} |
281 |
< |
} |
301 |
> |
public void testTryLockWhenLocked() throws InterruptedException { |
302 |
> |
final ReentrantLock lock = new ReentrantLock(); |
303 |
> |
lock.lock(); |
304 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
305 |
> |
public void realRun() { |
306 |
> |
assertFalse(lock.tryLock()); |
307 |
> |
}}); |
308 |
> |
|
309 |
> |
awaitTermination(t); |
310 |
> |
lock.unlock(); |
311 |
> |
} |
312 |
|
|
313 |
|
/** |
314 |
|
* Timed tryLock on a locked lock times out |
315 |
|
*/ |
316 |
< |
public void testTryLock_Timeout() { |
317 |
< |
final ReentrantLock lock = new ReentrantLock(); |
318 |
< |
lock.lock(); |
319 |
< |
Thread t = new Thread(new Runnable() { |
320 |
< |
public void run() { |
321 |
< |
try { |
322 |
< |
threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
323 |
< |
} catch (Exception ex) { |
324 |
< |
threadUnexpectedException(); |
325 |
< |
} |
326 |
< |
} |
327 |
< |
}); |
298 |
< |
try { |
299 |
< |
t.start(); |
300 |
< |
t.join(); |
301 |
< |
lock.unlock(); |
302 |
< |
} catch(Exception e){ |
303 |
< |
unexpectedException(); |
304 |
< |
} |
305 |
< |
} |
306 |
< |
|
316 |
> |
public void testTryLock_Timeout() throws InterruptedException { |
317 |
> |
final ReentrantLock lock = new ReentrantLock(); |
318 |
> |
lock.lock(); |
319 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
320 |
> |
public void realRun() throws InterruptedException { |
321 |
> |
assertFalse(lock.tryLock(1, MILLISECONDS)); |
322 |
> |
}}); |
323 |
> |
|
324 |
> |
awaitTermination(t); |
325 |
> |
lock.unlock(); |
326 |
> |
} |
327 |
> |
|
328 |
|
/** |
329 |
|
* getHoldCount returns number of recursive holds |
330 |
|
*/ |
331 |
|
public void testGetHoldCount() { |
332 |
< |
ReentrantLock lock = new ReentrantLock(); |
333 |
< |
for(int i = 1; i <= SIZE; i++) { |
334 |
< |
lock.lock(); |
335 |
< |
assertEquals(i,lock.getHoldCount()); |
336 |
< |
} |
337 |
< |
for(int i = SIZE; i > 0; i--) { |
338 |
< |
lock.unlock(); |
339 |
< |
assertEquals(i-1,lock.getHoldCount()); |
340 |
< |
} |
332 |
> |
ReentrantLock lock = new ReentrantLock(); |
333 |
> |
for (int i = 1; i <= SIZE; i++) { |
334 |
> |
lock.lock(); |
335 |
> |
assertEquals(i, lock.getHoldCount()); |
336 |
> |
} |
337 |
> |
for (int i = SIZE; i > 0; i--) { |
338 |
> |
lock.unlock(); |
339 |
> |
assertEquals(i-1, lock.getHoldCount()); |
340 |
> |
} |
341 |
|
} |
342 |
< |
|
343 |
< |
|
342 |
> |
|
343 |
> |
|
344 |
|
/** |
345 |
|
* isLocked is true when locked and false when not |
346 |
|
*/ |
347 |
< |
public void testIsLocked() { |
348 |
< |
final ReentrantLock lock = new ReentrantLock(); |
349 |
< |
lock.lock(); |
350 |
< |
assertTrue(lock.isLocked()); |
351 |
< |
lock.unlock(); |
352 |
< |
assertFalse(lock.isLocked()); |
353 |
< |
Thread t = new Thread(new Runnable() { |
354 |
< |
public void run() { |
355 |
< |
lock.lock(); |
356 |
< |
try { |
357 |
< |
Thread.sleep(SMALL_DELAY_MS); |
358 |
< |
} |
359 |
< |
catch(Exception e) { |
360 |
< |
threadUnexpectedException(); |
361 |
< |
} |
362 |
< |
lock.unlock(); |
363 |
< |
} |
343 |
< |
}); |
344 |
< |
try { |
345 |
< |
t.start(); |
346 |
< |
Thread.sleep(SHORT_DELAY_MS); |
347 |
< |
assertTrue(lock.isLocked()); |
348 |
< |
t.join(); |
349 |
< |
assertFalse(lock.isLocked()); |
350 |
< |
} catch(Exception e){ |
351 |
< |
unexpectedException(); |
352 |
< |
} |
347 |
> |
public void testIsLocked() throws InterruptedException { |
348 |
> |
final ReentrantLock lock = new ReentrantLock(); |
349 |
> |
lock.lock(); |
350 |
> |
assertTrue(lock.isLocked()); |
351 |
> |
lock.unlock(); |
352 |
> |
assertFalse(lock.isLocked()); |
353 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
354 |
> |
public void realRun() throws InterruptedException { |
355 |
> |
lock.lock(); |
356 |
> |
delay(SMALL_DELAY_MS); |
357 |
> |
lock.unlock(); |
358 |
> |
}}); |
359 |
> |
|
360 |
> |
delay(SHORT_DELAY_MS); |
361 |
> |
assertTrue(lock.isLocked()); |
362 |
> |
awaitTermination(t); |
363 |
> |
assertFalse(lock.isLocked()); |
364 |
|
} |
365 |
|
|
366 |
|
|
367 |
|
/** |
368 |
|
* lockInterruptibly is interruptible. |
369 |
|
*/ |
370 |
< |
public void testLockInterruptibly1() { |
371 |
< |
final ReentrantLock lock = new ReentrantLock(); |
372 |
< |
lock.lock(); |
373 |
< |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
374 |
< |
try { |
375 |
< |
t.start(); |
376 |
< |
t.interrupt(); |
377 |
< |
lock.unlock(); |
378 |
< |
t.join(); |
379 |
< |
} catch(Exception e){ |
369 |
< |
unexpectedException(); |
370 |
< |
} |
371 |
< |
} |
370 |
> |
public void testLockInterruptibly1() throws InterruptedException { |
371 |
> |
final ReentrantLock lock = new ReentrantLock(); |
372 |
> |
lock.lock(); |
373 |
> |
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
374 |
> |
delay(SHORT_DELAY_MS); |
375 |
> |
t.interrupt(); |
376 |
> |
delay(SHORT_DELAY_MS); |
377 |
> |
lock.unlock(); |
378 |
> |
awaitTermination(t); |
379 |
> |
} |
380 |
|
|
381 |
|
/** |
382 |
|
* lockInterruptibly succeeds when unlocked, else is interruptible |
383 |
|
*/ |
384 |
< |
public void testLockInterruptibly2() { |
385 |
< |
final ReentrantLock lock = new ReentrantLock(); |
386 |
< |
try { |
387 |
< |
lock.lockInterruptibly(); |
388 |
< |
} catch(Exception e) { |
389 |
< |
unexpectedException(); |
390 |
< |
} |
391 |
< |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
392 |
< |
try { |
385 |
< |
t.start(); |
386 |
< |
t.interrupt(); |
387 |
< |
assertTrue(lock.isLocked()); |
388 |
< |
assertTrue(lock.isHeldByCurrentThread()); |
389 |
< |
t.join(); |
390 |
< |
} catch(Exception e){ |
391 |
< |
unexpectedException(); |
392 |
< |
} |
384 |
> |
public void testLockInterruptibly2() throws InterruptedException { |
385 |
> |
final ReentrantLock lock = new ReentrantLock(); |
386 |
> |
lock.lockInterruptibly(); |
387 |
> |
Thread t = newStartedThread(new InterruptedLockRunnable(lock)); |
388 |
> |
delay(SHORT_DELAY_MS); |
389 |
> |
t.interrupt(); |
390 |
> |
assertTrue(lock.isLocked()); |
391 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
392 |
> |
awaitTermination(t); |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
|
* Calling await without holding lock throws IllegalMonitorStateException |
397 |
|
*/ |
398 |
< |
public void testAwait_IllegalMonitor() { |
399 |
< |
final ReentrantLock lock = new ReentrantLock(); |
398 |
> |
public void testAwait_IllegalMonitor() throws InterruptedException { |
399 |
> |
final ReentrantLock lock = new ReentrantLock(); |
400 |
|
final Condition c = lock.newCondition(); |
401 |
|
try { |
402 |
|
c.await(); |
403 |
|
shouldThrow(); |
404 |
< |
} |
405 |
< |
catch (IllegalMonitorStateException success) { |
406 |
< |
} |
407 |
< |
catch (Exception ex) { |
408 |
< |
unexpectedException(); |
409 |
< |
} |
404 |
> |
} catch (IllegalMonitorStateException success) {} |
405 |
|
} |
406 |
|
|
407 |
|
/** |
408 |
|
* Calling signal without holding lock throws IllegalMonitorStateException |
409 |
|
*/ |
410 |
|
public void testSignal_IllegalMonitor() { |
411 |
< |
final ReentrantLock lock = new ReentrantLock(); |
411 |
> |
final ReentrantLock lock = new ReentrantLock(); |
412 |
|
final Condition c = lock.newCondition(); |
413 |
|
try { |
414 |
|
c.signal(); |
415 |
|
shouldThrow(); |
416 |
< |
} |
422 |
< |
catch (IllegalMonitorStateException success) { |
423 |
< |
} |
424 |
< |
catch (Exception ex) { |
425 |
< |
unexpectedException(); |
426 |
< |
} |
416 |
> |
} catch (IllegalMonitorStateException success) {} |
417 |
|
} |
418 |
|
|
419 |
|
/** |
420 |
|
* awaitNanos without a signal times out |
421 |
|
*/ |
422 |
< |
public void testAwaitNanos_Timeout() { |
423 |
< |
final ReentrantLock lock = new ReentrantLock(); |
422 |
> |
public void testAwaitNanos_Timeout() throws InterruptedException { |
423 |
> |
final ReentrantLock lock = new ReentrantLock(); |
424 |
|
final Condition c = lock.newCondition(); |
425 |
< |
try { |
426 |
< |
lock.lock(); |
427 |
< |
long t = c.awaitNanos(100); |
428 |
< |
assertTrue(t <= 0); |
439 |
< |
lock.unlock(); |
440 |
< |
} |
441 |
< |
catch (Exception ex) { |
442 |
< |
unexpectedException(); |
443 |
< |
} |
425 |
> |
lock.lock(); |
426 |
> |
long t = c.awaitNanos(100); |
427 |
> |
assertTrue(t <= 0); |
428 |
> |
lock.unlock(); |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
< |
* timed await without a signal times out |
432 |
> |
* timed await without a signal times out |
433 |
|
*/ |
434 |
< |
public void testAwait_Timeout() { |
435 |
< |
final ReentrantLock lock = new ReentrantLock(); |
434 |
> |
public void testAwait_Timeout() throws InterruptedException { |
435 |
> |
final ReentrantLock lock = new ReentrantLock(); |
436 |
|
final Condition c = lock.newCondition(); |
437 |
< |
try { |
438 |
< |
lock.lock(); |
439 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
455 |
< |
lock.unlock(); |
456 |
< |
} |
457 |
< |
catch (Exception ex) { |
458 |
< |
unexpectedException(); |
459 |
< |
} |
437 |
> |
lock.lock(); |
438 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
439 |
> |
lock.unlock(); |
440 |
|
} |
441 |
|
|
442 |
|
/** |
443 |
|
* awaitUntil without a signal times out |
444 |
|
*/ |
445 |
< |
public void testAwaitUntil_Timeout() { |
446 |
< |
final ReentrantLock lock = new ReentrantLock(); |
445 |
> |
public void testAwaitUntil_Timeout() throws InterruptedException { |
446 |
> |
final ReentrantLock lock = new ReentrantLock(); |
447 |
|
final Condition c = lock.newCondition(); |
448 |
< |
try { |
449 |
< |
lock.lock(); |
450 |
< |
java.util.Date d = new java.util.Date(); |
451 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
472 |
< |
lock.unlock(); |
473 |
< |
} |
474 |
< |
catch (Exception ex) { |
475 |
< |
unexpectedException(); |
476 |
< |
} |
448 |
> |
lock.lock(); |
449 |
> |
java.util.Date d = new java.util.Date(); |
450 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
451 |
> |
lock.unlock(); |
452 |
|
} |
453 |
|
|
454 |
|
/** |
455 |
|
* await returns when signalled |
456 |
|
*/ |
457 |
< |
public void testAwait() { |
458 |
< |
final ReentrantLock lock = new ReentrantLock(); |
457 |
> |
public void testAwait() throws InterruptedException { |
458 |
> |
final ReentrantLock lock = new ReentrantLock(); |
459 |
|
final Condition c = lock.newCondition(); |
460 |
< |
Thread t = new Thread(new Runnable() { |
461 |
< |
public void run() { |
462 |
< |
try { |
463 |
< |
lock.lock(); |
464 |
< |
c.await(); |
465 |
< |
lock.unlock(); |
466 |
< |
} |
467 |
< |
catch(InterruptedException e) { |
468 |
< |
threadUnexpectedException(); |
469 |
< |
} |
470 |
< |
} |
471 |
< |
}); |
497 |
< |
|
498 |
< |
try { |
499 |
< |
t.start(); |
500 |
< |
Thread.sleep(SHORT_DELAY_MS); |
501 |
< |
lock.lock(); |
502 |
< |
c.signal(); |
503 |
< |
lock.unlock(); |
504 |
< |
t.join(SHORT_DELAY_MS); |
505 |
< |
assertFalse(t.isAlive()); |
506 |
< |
} |
507 |
< |
catch (Exception ex) { |
508 |
< |
unexpectedException(); |
509 |
< |
} |
460 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
461 |
> |
public void realRun() throws InterruptedException { |
462 |
> |
lock.lock(); |
463 |
> |
c.await(); |
464 |
> |
lock.unlock(); |
465 |
> |
}}); |
466 |
> |
|
467 |
> |
delay(SHORT_DELAY_MS); |
468 |
> |
lock.lock(); |
469 |
> |
c.signal(); |
470 |
> |
lock.unlock(); |
471 |
> |
awaitTermination(t); |
472 |
|
} |
473 |
|
|
474 |
|
/** |
475 |
|
* hasWaiters throws NPE if null |
476 |
|
*/ |
477 |
|
public void testHasWaitersNPE() { |
478 |
< |
final ReentrantLock lock = new ReentrantLock(); |
478 |
> |
final ReentrantLock lock = new ReentrantLock(); |
479 |
|
try { |
480 |
|
lock.hasWaiters(null); |
481 |
|
shouldThrow(); |
482 |
< |
} catch (NullPointerException success) { |
521 |
< |
} catch (Exception ex) { |
522 |
< |
unexpectedException(); |
523 |
< |
} |
482 |
> |
} catch (NullPointerException success) {} |
483 |
|
} |
484 |
|
|
485 |
|
/** |
486 |
|
* getWaitQueueLength throws NPE if null |
487 |
|
*/ |
488 |
|
public void testGetWaitQueueLengthNPE() { |
489 |
< |
final ReentrantLock lock = new ReentrantLock(); |
489 |
> |
final ReentrantLock lock = new ReentrantLock(); |
490 |
|
try { |
491 |
|
lock.getWaitQueueLength(null); |
492 |
|
shouldThrow(); |
493 |
< |
} catch (NullPointerException success) { |
535 |
< |
} catch (Exception ex) { |
536 |
< |
unexpectedException(); |
537 |
< |
} |
493 |
> |
} catch (NullPointerException success) {} |
494 |
|
} |
495 |
|
|
496 |
|
|
498 |
|
* getWaitingThreads throws NPE if null |
499 |
|
*/ |
500 |
|
public void testGetWaitingThreadsNPE() { |
501 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
501 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
502 |
|
try { |
503 |
|
lock.getWaitingThreads(null); |
504 |
|
shouldThrow(); |
505 |
< |
} catch (NullPointerException success) { |
550 |
< |
} catch (Exception ex) { |
551 |
< |
unexpectedException(); |
552 |
< |
} |
505 |
> |
} catch (NullPointerException success) {} |
506 |
|
} |
507 |
|
|
508 |
|
|
510 |
|
* hasWaiters throws IAE if not owned |
511 |
|
*/ |
512 |
|
public void testHasWaitersIAE() { |
513 |
< |
final ReentrantLock lock = new ReentrantLock(); |
514 |
< |
final Condition c = (lock.newCondition()); |
515 |
< |
final ReentrantLock lock2 = new ReentrantLock(); |
513 |
> |
final ReentrantLock lock = new ReentrantLock(); |
514 |
> |
final Condition c = lock.newCondition(); |
515 |
> |
final ReentrantLock lock2 = new ReentrantLock(); |
516 |
|
try { |
517 |
|
lock2.hasWaiters(c); |
518 |
|
shouldThrow(); |
519 |
< |
} catch (IllegalArgumentException success) { |
567 |
< |
} catch (Exception ex) { |
568 |
< |
unexpectedException(); |
569 |
< |
} |
519 |
> |
} catch (IllegalArgumentException success) {} |
520 |
|
} |
521 |
|
|
522 |
|
/** |
523 |
|
* hasWaiters throws IMSE if not locked |
524 |
|
*/ |
525 |
|
public void testHasWaitersIMSE() { |
526 |
< |
final ReentrantLock lock = new ReentrantLock(); |
527 |
< |
final Condition c = (lock.newCondition()); |
526 |
> |
final ReentrantLock lock = new ReentrantLock(); |
527 |
> |
final Condition c = lock.newCondition(); |
528 |
|
try { |
529 |
|
lock.hasWaiters(c); |
530 |
|
shouldThrow(); |
531 |
< |
} catch (IllegalMonitorStateException success) { |
582 |
< |
} catch (Exception ex) { |
583 |
< |
unexpectedException(); |
584 |
< |
} |
531 |
> |
} catch (IllegalMonitorStateException success) {} |
532 |
|
} |
533 |
|
|
534 |
|
|
536 |
|
* getWaitQueueLength throws IAE if not owned |
537 |
|
*/ |
538 |
|
public void testGetWaitQueueLengthIAE() { |
539 |
< |
final ReentrantLock lock = new ReentrantLock(); |
540 |
< |
final Condition c = (lock.newCondition()); |
541 |
< |
final ReentrantLock lock2 = new ReentrantLock(); |
539 |
> |
final ReentrantLock lock = new ReentrantLock(); |
540 |
> |
final Condition c = lock.newCondition(); |
541 |
> |
final ReentrantLock lock2 = new ReentrantLock(); |
542 |
|
try { |
543 |
|
lock2.getWaitQueueLength(c); |
544 |
|
shouldThrow(); |
545 |
< |
} catch (IllegalArgumentException success) { |
599 |
< |
} catch (Exception ex) { |
600 |
< |
unexpectedException(); |
601 |
< |
} |
545 |
> |
} catch (IllegalArgumentException success) {} |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
|
* getWaitQueueLength throws IMSE if not locked |
550 |
|
*/ |
551 |
|
public void testGetWaitQueueLengthIMSE() { |
552 |
< |
final ReentrantLock lock = new ReentrantLock(); |
553 |
< |
final Condition c = (lock.newCondition()); |
552 |
> |
final ReentrantLock lock = new ReentrantLock(); |
553 |
> |
final Condition c = lock.newCondition(); |
554 |
|
try { |
555 |
|
lock.getWaitQueueLength(c); |
556 |
|
shouldThrow(); |
557 |
< |
} catch (IllegalMonitorStateException success) { |
614 |
< |
} catch (Exception ex) { |
615 |
< |
unexpectedException(); |
616 |
< |
} |
557 |
> |
} catch (IllegalMonitorStateException success) {} |
558 |
|
} |
559 |
|
|
560 |
|
|
562 |
|
* getWaitingThreads throws IAE if not owned |
563 |
|
*/ |
564 |
|
public void testGetWaitingThreadsIAE() { |
565 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
566 |
< |
final Condition c = (lock.newCondition()); |
567 |
< |
final PublicReentrantLock lock2 = new PublicReentrantLock(); |
565 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
566 |
> |
final Condition c = lock.newCondition(); |
567 |
> |
final PublicReentrantLock lock2 = new PublicReentrantLock(); |
568 |
|
try { |
569 |
|
lock2.getWaitingThreads(c); |
570 |
|
shouldThrow(); |
571 |
< |
} catch (IllegalArgumentException success) { |
631 |
< |
} catch (Exception ex) { |
632 |
< |
unexpectedException(); |
633 |
< |
} |
571 |
> |
} catch (IllegalArgumentException success) {} |
572 |
|
} |
573 |
|
|
574 |
|
/** |
575 |
|
* getWaitingThreads throws IMSE if not locked |
576 |
|
*/ |
577 |
|
public void testGetWaitingThreadsIMSE() { |
578 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
579 |
< |
final Condition c = (lock.newCondition()); |
578 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
579 |
> |
final Condition c = lock.newCondition(); |
580 |
|
try { |
581 |
|
lock.getWaitingThreads(c); |
582 |
|
shouldThrow(); |
583 |
< |
} catch (IllegalMonitorStateException success) { |
646 |
< |
} catch (Exception ex) { |
647 |
< |
unexpectedException(); |
648 |
< |
} |
583 |
> |
} catch (IllegalMonitorStateException success) {} |
584 |
|
} |
585 |
|
|
586 |
|
|
652 |
– |
|
587 |
|
/** |
588 |
|
* hasWaiters returns true when a thread is waiting, else false |
589 |
|
*/ |
590 |
< |
public void testHasWaiters() { |
591 |
< |
final ReentrantLock lock = new ReentrantLock(); |
590 |
> |
public void testHasWaiters() throws InterruptedException { |
591 |
> |
final ReentrantLock lock = new ReentrantLock(); |
592 |
|
final Condition c = lock.newCondition(); |
593 |
< |
Thread t = new Thread(new Runnable() { |
594 |
< |
public void run() { |
595 |
< |
try { |
596 |
< |
lock.lock(); |
597 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
598 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
599 |
< |
c.await(); |
600 |
< |
lock.unlock(); |
601 |
< |
} |
602 |
< |
catch(InterruptedException e) { |
603 |
< |
threadUnexpectedException(); |
604 |
< |
} |
605 |
< |
} |
606 |
< |
}); |
607 |
< |
|
608 |
< |
try { |
609 |
< |
t.start(); |
610 |
< |
Thread.sleep(SHORT_DELAY_MS); |
611 |
< |
lock.lock(); |
612 |
< |
assertTrue(lock.hasWaiters(c)); |
613 |
< |
assertEquals(1, lock.getWaitQueueLength(c)); |
680 |
< |
c.signal(); |
681 |
< |
lock.unlock(); |
682 |
< |
Thread.sleep(SHORT_DELAY_MS); |
683 |
< |
lock.lock(); |
684 |
< |
assertFalse(lock.hasWaiters(c)); |
685 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
686 |
< |
lock.unlock(); |
687 |
< |
t.join(SHORT_DELAY_MS); |
688 |
< |
assertFalse(t.isAlive()); |
689 |
< |
} |
690 |
< |
catch (Exception ex) { |
691 |
< |
unexpectedException(); |
692 |
< |
} |
593 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
594 |
> |
public void realRun() throws InterruptedException { |
595 |
> |
lock.lock(); |
596 |
> |
assertFalse(lock.hasWaiters(c)); |
597 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
598 |
> |
c.await(); |
599 |
> |
lock.unlock(); |
600 |
> |
}}); |
601 |
> |
|
602 |
> |
delay(SHORT_DELAY_MS); |
603 |
> |
lock.lock(); |
604 |
> |
assertTrue(lock.hasWaiters(c)); |
605 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
606 |
> |
c.signal(); |
607 |
> |
lock.unlock(); |
608 |
> |
delay(SHORT_DELAY_MS); |
609 |
> |
lock.lock(); |
610 |
> |
assertFalse(lock.hasWaiters(c)); |
611 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
612 |
> |
lock.unlock(); |
613 |
> |
awaitTermination(t); |
614 |
|
} |
615 |
|
|
616 |
|
/** |
617 |
|
* getWaitQueueLength returns number of waiting threads |
618 |
|
*/ |
619 |
< |
public void testGetWaitQueueLength() { |
620 |
< |
final ReentrantLock lock = new ReentrantLock(); |
619 |
> |
public void testGetWaitQueueLength() throws InterruptedException { |
620 |
> |
final ReentrantLock lock = new ReentrantLock(); |
621 |
|
final Condition c = lock.newCondition(); |
622 |
< |
Thread t1 = new Thread(new Runnable() { |
623 |
< |
public void run() { |
624 |
< |
try { |
625 |
< |
lock.lock(); |
626 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
627 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
628 |
< |
c.await(); |
629 |
< |
lock.unlock(); |
630 |
< |
} |
631 |
< |
catch(InterruptedException e) { |
632 |
< |
threadUnexpectedException(); |
633 |
< |
} |
634 |
< |
} |
635 |
< |
}); |
636 |
< |
|
637 |
< |
Thread t2 = new Thread(new Runnable() { |
638 |
< |
public void run() { |
639 |
< |
try { |
640 |
< |
lock.lock(); |
641 |
< |
threadAssertTrue(lock.hasWaiters(c)); |
642 |
< |
threadAssertEquals(1, lock.getWaitQueueLength(c)); |
643 |
< |
c.await(); |
644 |
< |
lock.unlock(); |
645 |
< |
} |
646 |
< |
catch(InterruptedException e) { |
647 |
< |
threadUnexpectedException(); |
648 |
< |
} |
649 |
< |
} |
650 |
< |
}); |
651 |
< |
|
652 |
< |
try { |
653 |
< |
t1.start(); |
654 |
< |
Thread.sleep(SHORT_DELAY_MS); |
734 |
< |
t2.start(); |
735 |
< |
Thread.sleep(SHORT_DELAY_MS); |
736 |
< |
lock.lock(); |
737 |
< |
assertTrue(lock.hasWaiters(c)); |
738 |
< |
assertEquals(2, lock.getWaitQueueLength(c)); |
739 |
< |
c.signalAll(); |
740 |
< |
lock.unlock(); |
741 |
< |
Thread.sleep(SHORT_DELAY_MS); |
742 |
< |
lock.lock(); |
743 |
< |
assertFalse(lock.hasWaiters(c)); |
744 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
745 |
< |
lock.unlock(); |
746 |
< |
t1.join(SHORT_DELAY_MS); |
747 |
< |
t2.join(SHORT_DELAY_MS); |
748 |
< |
assertFalse(t1.isAlive()); |
749 |
< |
assertFalse(t2.isAlive()); |
750 |
< |
} |
751 |
< |
catch (Exception ex) { |
752 |
< |
unexpectedException(); |
753 |
< |
} |
622 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
623 |
> |
public void realRun() throws InterruptedException { |
624 |
> |
lock.lock(); |
625 |
> |
assertFalse(lock.hasWaiters(c)); |
626 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
627 |
> |
c.await(); |
628 |
> |
lock.unlock(); |
629 |
> |
}}); |
630 |
> |
|
631 |
> |
delay(SHORT_DELAY_MS); |
632 |
> |
|
633 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
634 |
> |
public void realRun() throws InterruptedException { |
635 |
> |
lock.lock(); |
636 |
> |
assertTrue(lock.hasWaiters(c)); |
637 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
638 |
> |
c.await(); |
639 |
> |
lock.unlock(); |
640 |
> |
}}); |
641 |
> |
|
642 |
> |
delay(SHORT_DELAY_MS); |
643 |
> |
lock.lock(); |
644 |
> |
assertTrue(lock.hasWaiters(c)); |
645 |
> |
assertEquals(2, lock.getWaitQueueLength(c)); |
646 |
> |
c.signalAll(); |
647 |
> |
lock.unlock(); |
648 |
> |
delay(SHORT_DELAY_MS); |
649 |
> |
lock.lock(); |
650 |
> |
assertFalse(lock.hasWaiters(c)); |
651 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
652 |
> |
lock.unlock(); |
653 |
> |
awaitTermination(t1); |
654 |
> |
awaitTermination(t2); |
655 |
|
} |
656 |
|
|
657 |
|
/** |
658 |
|
* getWaitingThreads returns only and all waiting threads |
659 |
|
*/ |
660 |
< |
public void testGetWaitingThreads() { |
661 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
660 |
> |
public void testGetWaitingThreads() throws InterruptedException { |
661 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
662 |
|
final Condition c = lock.newCondition(); |
663 |
< |
Thread t1 = new Thread(new Runnable() { |
664 |
< |
public void run() { |
665 |
< |
try { |
666 |
< |
lock.lock(); |
667 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
668 |
< |
c.await(); |
669 |
< |
lock.unlock(); |
670 |
< |
} |
671 |
< |
catch(InterruptedException e) { |
672 |
< |
threadUnexpectedException(); |
673 |
< |
} |
674 |
< |
} |
675 |
< |
}); |
676 |
< |
|
677 |
< |
Thread t2 = new Thread(new Runnable() { |
678 |
< |
public void run() { |
679 |
< |
try { |
680 |
< |
lock.lock(); |
681 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
682 |
< |
c.await(); |
683 |
< |
lock.unlock(); |
684 |
< |
} |
685 |
< |
catch(InterruptedException e) { |
686 |
< |
threadUnexpectedException(); |
687 |
< |
} |
688 |
< |
} |
689 |
< |
}); |
663 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
664 |
> |
public void realRun() throws InterruptedException { |
665 |
> |
lock.lock(); |
666 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
667 |
> |
c.await(); |
668 |
> |
lock.unlock(); |
669 |
> |
}}); |
670 |
> |
|
671 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
672 |
> |
public void realRun() throws InterruptedException { |
673 |
> |
lock.lock(); |
674 |
> |
assertFalse(lock.getWaitingThreads(c).isEmpty()); |
675 |
> |
c.await(); |
676 |
> |
lock.unlock(); |
677 |
> |
}}); |
678 |
> |
|
679 |
> |
lock.lock(); |
680 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
681 |
> |
lock.unlock(); |
682 |
> |
t1.start(); |
683 |
> |
delay(SHORT_DELAY_MS); |
684 |
> |
t2.start(); |
685 |
> |
delay(SHORT_DELAY_MS); |
686 |
> |
lock.lock(); |
687 |
> |
assertTrue(lock.hasWaiters(c)); |
688 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
689 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
690 |
> |
c.signalAll(); |
691 |
> |
lock.unlock(); |
692 |
> |
delay(SHORT_DELAY_MS); |
693 |
> |
lock.lock(); |
694 |
> |
assertFalse(lock.hasWaiters(c)); |
695 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
696 |
> |
lock.unlock(); |
697 |
> |
awaitTermination(t1); |
698 |
> |
awaitTermination(t2); |
699 |
> |
} |
700 |
> |
|
701 |
> |
/** A helper class for uninterruptible wait tests */ |
702 |
> |
class UninterruptibleThread extends Thread { |
703 |
> |
private ReentrantLock lock; |
704 |
> |
private Condition c; |
705 |
> |
|
706 |
> |
public volatile boolean canAwake = false; |
707 |
> |
public volatile boolean interrupted = false; |
708 |
> |
public volatile boolean lockStarted = false; |
709 |
> |
|
710 |
> |
public UninterruptibleThread(ReentrantLock lock, Condition c) { |
711 |
> |
this.lock = lock; |
712 |
> |
this.c = c; |
713 |
> |
} |
714 |
> |
|
715 |
> |
public synchronized void run() { |
716 |
> |
lock.lock(); |
717 |
> |
lockStarted = true; |
718 |
> |
|
719 |
> |
while (!canAwake) { |
720 |
> |
c.awaitUninterruptibly(); |
721 |
> |
} |
722 |
|
|
723 |
< |
try { |
791 |
< |
lock.lock(); |
792 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
723 |
> |
interrupted = isInterrupted(); |
724 |
|
lock.unlock(); |
794 |
– |
t1.start(); |
795 |
– |
Thread.sleep(SHORT_DELAY_MS); |
796 |
– |
t2.start(); |
797 |
– |
Thread.sleep(SHORT_DELAY_MS); |
798 |
– |
lock.lock(); |
799 |
– |
assertTrue(lock.hasWaiters(c)); |
800 |
– |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
801 |
– |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
802 |
– |
c.signalAll(); |
803 |
– |
lock.unlock(); |
804 |
– |
Thread.sleep(SHORT_DELAY_MS); |
805 |
– |
lock.lock(); |
806 |
– |
assertFalse(lock.hasWaiters(c)); |
807 |
– |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
808 |
– |
lock.unlock(); |
809 |
– |
t1.join(SHORT_DELAY_MS); |
810 |
– |
t2.join(SHORT_DELAY_MS); |
811 |
– |
assertFalse(t1.isAlive()); |
812 |
– |
assertFalse(t2.isAlive()); |
813 |
– |
} |
814 |
– |
catch (Exception ex) { |
815 |
– |
unexpectedException(); |
725 |
|
} |
726 |
|
} |
727 |
|
|
819 |
– |
|
820 |
– |
|
728 |
|
/** |
729 |
|
* awaitUninterruptibly doesn't abort on interrupt |
730 |
|
*/ |
731 |
< |
public void testAwaitUninterruptibly() { |
732 |
< |
final ReentrantLock lock = new ReentrantLock(); |
731 |
> |
public void testAwaitUninterruptibly() throws InterruptedException { |
732 |
> |
final ReentrantLock lock = new ReentrantLock(); |
733 |
|
final Condition c = lock.newCondition(); |
734 |
< |
Thread t = new Thread(new Runnable() { |
735 |
< |
public void run() { |
736 |
< |
lock.lock(); |
737 |
< |
c.awaitUninterruptibly(); |
738 |
< |
lock.unlock(); |
739 |
< |
} |
740 |
< |
}); |
734 |
> |
UninterruptibleThread thread = new UninterruptibleThread(lock, c); |
735 |
> |
|
736 |
> |
thread.start(); |
737 |
> |
|
738 |
> |
while (!thread.lockStarted) { |
739 |
> |
delay(100); |
740 |
> |
} |
741 |
|
|
742 |
+ |
lock.lock(); |
743 |
|
try { |
744 |
< |
t.start(); |
745 |
< |
Thread.sleep(SHORT_DELAY_MS); |
838 |
< |
t.interrupt(); |
839 |
< |
lock.lock(); |
744 |
> |
thread.interrupt(); |
745 |
> |
thread.canAwake = true; |
746 |
|
c.signal(); |
747 |
+ |
} finally { |
748 |
|
lock.unlock(); |
842 |
– |
assert(t.isInterrupted()); |
843 |
– |
t.join(SHORT_DELAY_MS); |
844 |
– |
assertFalse(t.isAlive()); |
845 |
– |
} |
846 |
– |
catch (Exception ex) { |
847 |
– |
unexpectedException(); |
749 |
|
} |
750 |
+ |
|
751 |
+ |
thread.join(); |
752 |
+ |
assertTrue(thread.interrupted); |
753 |
+ |
assertFalse(thread.isAlive()); |
754 |
|
} |
755 |
|
|
756 |
|
/** |
757 |
|
* await is interruptible |
758 |
|
*/ |
759 |
< |
public void testAwait_Interrupt() { |
760 |
< |
final ReentrantLock lock = new ReentrantLock(); |
759 |
> |
public void testAwait_Interrupt() throws InterruptedException { |
760 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
761 |
|
final Condition c = lock.newCondition(); |
762 |
< |
Thread t = new Thread(new Runnable() { |
763 |
< |
public void run() { |
764 |
< |
try { |
765 |
< |
lock.lock(); |
766 |
< |
c.await(); |
767 |
< |
lock.unlock(); |
768 |
< |
threadShouldThrow(); |
769 |
< |
} |
770 |
< |
catch(InterruptedException success) { |
771 |
< |
} |
772 |
< |
} |
773 |
< |
}); |
774 |
< |
|
775 |
< |
try { |
776 |
< |
t.start(); |
777 |
< |
Thread.sleep(SHORT_DELAY_MS); |
778 |
< |
t.interrupt(); |
779 |
< |
t.join(SHORT_DELAY_MS); |
780 |
< |
assertFalse(t.isAlive()); |
781 |
< |
} |
782 |
< |
catch (Exception ex) { |
783 |
< |
unexpectedException(); |
784 |
< |
} |
762 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
763 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
764 |
> |
public void realRun() throws InterruptedException { |
765 |
> |
lock.lock(); |
766 |
> |
assertTrue(lock.isLocked()); |
767 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
768 |
> |
assertHasNoWaiters(lock, c); |
769 |
> |
locked.countDown(); |
770 |
> |
try { |
771 |
> |
c.await(); |
772 |
> |
} finally { |
773 |
> |
assertTrue(lock.isLocked()); |
774 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
775 |
> |
assertHasNoWaiters(lock, c); |
776 |
> |
lock.unlock(); |
777 |
> |
assertFalse(Thread.interrupted()); |
778 |
> |
} |
779 |
> |
}}); |
780 |
> |
|
781 |
> |
locked.await(); |
782 |
> |
assertHasWaiters(lock, c, t); |
783 |
> |
t.interrupt(); |
784 |
> |
awaitTermination(t); |
785 |
> |
assertFalse(lock.isLocked()); |
786 |
|
} |
787 |
|
|
788 |
|
/** |
789 |
|
* awaitNanos is interruptible |
790 |
|
*/ |
791 |
< |
public void testAwaitNanos_Interrupt() { |
792 |
< |
final ReentrantLock lock = new ReentrantLock(); |
791 |
> |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
792 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
793 |
|
final Condition c = lock.newCondition(); |
794 |
< |
Thread t = new Thread(new Runnable() { |
795 |
< |
public void run() { |
796 |
< |
try { |
797 |
< |
lock.lock(); |
798 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
799 |
< |
lock.unlock(); |
800 |
< |
threadShouldThrow(); |
801 |
< |
} |
802 |
< |
catch(InterruptedException success) { |
803 |
< |
} |
804 |
< |
} |
805 |
< |
}); |
806 |
< |
|
807 |
< |
try { |
808 |
< |
t.start(); |
809 |
< |
Thread.sleep(SHORT_DELAY_MS); |
810 |
< |
t.interrupt(); |
811 |
< |
t.join(SHORT_DELAY_MS); |
812 |
< |
assertFalse(t.isAlive()); |
813 |
< |
} |
814 |
< |
catch (Exception ex) { |
815 |
< |
unexpectedException(); |
816 |
< |
} |
794 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
795 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
796 |
> |
public void realRun() throws InterruptedException { |
797 |
> |
lock.lock(); |
798 |
> |
assertTrue(lock.isLocked()); |
799 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
800 |
> |
assertHasNoWaiters(lock, c); |
801 |
> |
locked.countDown(); |
802 |
> |
try { |
803 |
> |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
804 |
> |
} finally { |
805 |
> |
assertTrue(lock.isLocked()); |
806 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
807 |
> |
assertHasNoWaiters(lock, c); |
808 |
> |
lock.unlock(); |
809 |
> |
assertFalse(Thread.interrupted()); |
810 |
> |
} |
811 |
> |
}}); |
812 |
> |
|
813 |
> |
locked.await(); |
814 |
> |
assertHasWaiters(lock, c, t); |
815 |
> |
t.interrupt(); |
816 |
> |
awaitTermination(t); |
817 |
> |
assertFalse(lock.isLocked()); |
818 |
|
} |
819 |
|
|
820 |
|
/** |
821 |
|
* awaitUntil is interruptible |
822 |
|
*/ |
823 |
< |
public void testAwaitUntil_Interrupt() { |
824 |
< |
final ReentrantLock lock = new ReentrantLock(); |
823 |
> |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
824 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
825 |
|
final Condition c = lock.newCondition(); |
826 |
< |
Thread t = new Thread(new Runnable() { |
827 |
< |
public void run() { |
828 |
< |
try { |
829 |
< |
lock.lock(); |
830 |
< |
java.util.Date d = new java.util.Date(); |
831 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
832 |
< |
lock.unlock(); |
833 |
< |
threadShouldThrow(); |
834 |
< |
} |
835 |
< |
catch(InterruptedException success) { |
836 |
< |
} |
837 |
< |
} |
838 |
< |
}); |
839 |
< |
|
840 |
< |
try { |
841 |
< |
t.start(); |
842 |
< |
Thread.sleep(SHORT_DELAY_MS); |
843 |
< |
t.interrupt(); |
844 |
< |
t.join(SHORT_DELAY_MS); |
845 |
< |
assertFalse(t.isAlive()); |
846 |
< |
} |
847 |
< |
catch (Exception ex) { |
848 |
< |
unexpectedException(); |
849 |
< |
} |
826 |
> |
final CountDownLatch locked = new CountDownLatch(1); |
827 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
828 |
> |
public void realRun() throws InterruptedException { |
829 |
> |
lock.lock(); |
830 |
> |
assertTrue(lock.isLocked()); |
831 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
832 |
> |
assertHasNoWaiters(lock, c); |
833 |
> |
locked.countDown(); |
834 |
> |
java.util.Date d = new java.util.Date(); |
835 |
> |
try { |
836 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
837 |
> |
} finally { |
838 |
> |
assertTrue(lock.isLocked()); |
839 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
840 |
> |
assertHasNoWaiters(lock, c); |
841 |
> |
lock.unlock(); |
842 |
> |
assertFalse(Thread.interrupted()); |
843 |
> |
} |
844 |
> |
}}); |
845 |
> |
|
846 |
> |
locked.await(); |
847 |
> |
assertHasWaiters(lock, c, t); |
848 |
> |
t.interrupt(); |
849 |
> |
awaitTermination(t); |
850 |
> |
assertFalse(lock.isLocked()); |
851 |
|
} |
852 |
|
|
853 |
|
/** |
854 |
|
* signalAll wakes up all threads |
855 |
|
*/ |
856 |
< |
public void testSignalAll() { |
857 |
< |
final ReentrantLock lock = new ReentrantLock(); |
856 |
> |
public void testSignalAll() throws InterruptedException { |
857 |
> |
final ReentrantLock lock = new ReentrantLock(); |
858 |
|
final Condition c = lock.newCondition(); |
859 |
< |
Thread t1 = new Thread(new Runnable() { |
860 |
< |
public void run() { |
861 |
< |
try { |
862 |
< |
lock.lock(); |
863 |
< |
c.await(); |
864 |
< |
lock.unlock(); |
865 |
< |
} |
866 |
< |
catch(InterruptedException e) { |
867 |
< |
threadUnexpectedException(); |
868 |
< |
} |
869 |
< |
} |
870 |
< |
}); |
871 |
< |
|
872 |
< |
Thread t2 = new Thread(new Runnable() { |
873 |
< |
public void run() { |
874 |
< |
try { |
875 |
< |
lock.lock(); |
876 |
< |
c.await(); |
877 |
< |
lock.unlock(); |
878 |
< |
} |
879 |
< |
catch(InterruptedException e) { |
880 |
< |
threadUnexpectedException(); |
881 |
< |
} |
882 |
< |
} |
883 |
< |
}); |
884 |
< |
|
885 |
< |
try { |
886 |
< |
t1.start(); |
887 |
< |
t2.start(); |
888 |
< |
Thread.sleep(SHORT_DELAY_MS); |
889 |
< |
lock.lock(); |
890 |
< |
c.signalAll(); |
891 |
< |
lock.unlock(); |
892 |
< |
t1.join(SHORT_DELAY_MS); |
893 |
< |
t2.join(SHORT_DELAY_MS); |
894 |
< |
assertFalse(t1.isAlive()); |
895 |
< |
assertFalse(t2.isAlive()); |
896 |
< |
} |
897 |
< |
catch (Exception ex) { |
898 |
< |
unexpectedException(); |
899 |
< |
} |
859 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
860 |
> |
public void realRun() throws InterruptedException { |
861 |
> |
lock.lock(); |
862 |
> |
c.await(); |
863 |
> |
lock.unlock(); |
864 |
> |
}}); |
865 |
> |
|
866 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
867 |
> |
public void realRun() throws InterruptedException { |
868 |
> |
lock.lock(); |
869 |
> |
c.await(); |
870 |
> |
lock.unlock(); |
871 |
> |
}}); |
872 |
> |
|
873 |
> |
delay(SHORT_DELAY_MS); |
874 |
> |
lock.lock(); |
875 |
> |
c.signalAll(); |
876 |
> |
lock.unlock(); |
877 |
> |
awaitTermination(t1); |
878 |
> |
awaitTermination(t2); |
879 |
> |
} |
880 |
> |
|
881 |
> |
/** |
882 |
> |
* await after multiple reentrant locking preserves lock count |
883 |
> |
*/ |
884 |
> |
public void testAwaitLockCount() throws InterruptedException { |
885 |
> |
final ReentrantLock lock = new ReentrantLock(); |
886 |
> |
final Condition c = lock.newCondition(); |
887 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
888 |
> |
public void realRun() throws InterruptedException { |
889 |
> |
lock.lock(); |
890 |
> |
assertEquals(1, lock.getHoldCount()); |
891 |
> |
c.await(); |
892 |
> |
assertEquals(1, lock.getHoldCount()); |
893 |
> |
lock.unlock(); |
894 |
> |
}}); |
895 |
> |
|
896 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
897 |
> |
public void realRun() throws InterruptedException { |
898 |
> |
lock.lock(); |
899 |
> |
lock.lock(); |
900 |
> |
assertEquals(2, lock.getHoldCount()); |
901 |
> |
c.await(); |
902 |
> |
assertEquals(2, lock.getHoldCount()); |
903 |
> |
lock.unlock(); |
904 |
> |
lock.unlock(); |
905 |
> |
}}); |
906 |
> |
|
907 |
> |
delay(SHORT_DELAY_MS); |
908 |
> |
lock.lock(); |
909 |
> |
c.signalAll(); |
910 |
> |
lock.unlock(); |
911 |
> |
awaitTermination(t1); |
912 |
> |
awaitTermination(t2); |
913 |
|
} |
914 |
|
|
915 |
|
/** |
916 |
|
* A serialized lock deserializes as unlocked |
917 |
|
*/ |
918 |
< |
public void testSerialization() { |
918 |
> |
public void testSerialization() throws Exception { |
919 |
|
ReentrantLock l = new ReentrantLock(); |
920 |
|
l.lock(); |
921 |
|
l.unlock(); |
922 |
|
|
923 |
< |
try { |
924 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
925 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
926 |
< |
out.writeObject(l); |
927 |
< |
out.close(); |
928 |
< |
|
929 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
930 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
931 |
< |
ReentrantLock r = (ReentrantLock) in.readObject(); |
932 |
< |
r.lock(); |
933 |
< |
r.unlock(); |
934 |
< |
} catch(Exception e){ |
935 |
< |
e.printStackTrace(); |
936 |
< |
unexpectedException(); |
937 |
< |
} |
923 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
924 |
> |
ObjectOutputStream out = |
925 |
> |
new ObjectOutputStream(new BufferedOutputStream(bout)); |
926 |
> |
out.writeObject(l); |
927 |
> |
out.close(); |
928 |
> |
|
929 |
> |
ByteArrayInputStream bin = |
930 |
> |
new ByteArrayInputStream(bout.toByteArray()); |
931 |
> |
ObjectInputStream in = |
932 |
> |
new ObjectInputStream(new BufferedInputStream(bin)); |
933 |
> |
ReentrantLock r = (ReentrantLock) in.readObject(); |
934 |
> |
r.lock(); |
935 |
> |
r.unlock(); |
936 |
> |
} |
937 |
> |
|
938 |
> |
/** |
939 |
> |
* toString indicates current lock state |
940 |
> |
*/ |
941 |
> |
public void testToString() { |
942 |
> |
ReentrantLock lock = new ReentrantLock(); |
943 |
> |
String us = lock.toString(); |
944 |
> |
assertTrue(us.indexOf("Unlocked") >= 0); |
945 |
> |
lock.lock(); |
946 |
> |
String ls = lock.toString(); |
947 |
> |
assertTrue(ls.indexOf("Locked") >= 0); |
948 |
|
} |
949 |
|
|
950 |
|
} |