65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
< |
* A runnable calling acquireInterruptibly |
68 |
> |
* A runnable calling acquireInterruptibly that does not expect to |
69 |
> |
* be interrupted. |
70 |
|
*/ |
71 |
< |
class InterruptibleSyncRunnable implements Runnable { |
71 |
> |
class InterruptibleSyncRunnable extends CheckedRunnable { |
72 |
|
final Mutex sync; |
73 |
|
InterruptibleSyncRunnable(Mutex l) { sync = l; } |
74 |
< |
public void run() { |
75 |
< |
try { |
75 |
< |
sync.acquireInterruptibly(1); |
76 |
< |
} catch (InterruptedException success) {} |
74 |
> |
public void realRun() throws InterruptedException { |
75 |
> |
sync.acquireInterruptibly(1); |
76 |
|
} |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
/** |
81 |
|
* A runnable calling acquireInterruptibly that expects to be |
82 |
< |
* interrupted |
82 |
> |
* interrupted. |
83 |
|
*/ |
84 |
< |
class InterruptedSyncRunnable implements Runnable { |
84 |
> |
class InterruptedSyncRunnable extends CheckedInterruptedRunnable { |
85 |
|
final Mutex sync; |
86 |
|
InterruptedSyncRunnable(Mutex l) { sync = l; } |
87 |
< |
public void run() { |
88 |
< |
try { |
90 |
< |
sync.acquireInterruptibly(1); |
91 |
< |
threadShouldThrow(); |
92 |
< |
} catch (InterruptedException success) {} |
87 |
> |
public void realRun() throws InterruptedException { |
88 |
> |
sync.acquireInterruptibly(1); |
89 |
|
} |
90 |
|
} |
91 |
|
|
93 |
|
* isHeldExclusively is false upon construction |
94 |
|
*/ |
95 |
|
public void testIsHeldExclusively() { |
96 |
< |
Mutex rl = new Mutex(); |
96 |
> |
Mutex rl = new Mutex(); |
97 |
|
assertFalse(rl.isHeldExclusively()); |
98 |
|
} |
99 |
|
|
101 |
|
* acquiring released sync succeeds |
102 |
|
*/ |
103 |
|
public void testAcquire() { |
104 |
< |
Mutex rl = new Mutex(); |
104 |
> |
Mutex rl = new Mutex(); |
105 |
|
rl.acquire(1); |
106 |
|
assertTrue(rl.isHeldExclusively()); |
107 |
|
rl.release(1); |
112 |
|
* tryAcquire on an released sync succeeds |
113 |
|
*/ |
114 |
|
public void testTryAcquire() { |
115 |
< |
Mutex rl = new Mutex(); |
115 |
> |
Mutex rl = new Mutex(); |
116 |
|
assertTrue(rl.tryAcquire(1)); |
117 |
|
assertTrue(rl.isHeldExclusively()); |
118 |
|
rl.release(1); |
121 |
|
/** |
122 |
|
* hasQueuedThreads reports whether there are waiting threads |
123 |
|
*/ |
124 |
< |
public void testhasQueuedThreads() { |
125 |
< |
final Mutex sync = new Mutex(); |
124 |
> |
public void testhasQueuedThreads() throws InterruptedException { |
125 |
> |
final Mutex sync = new Mutex(); |
126 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
127 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
128 |
< |
try { |
129 |
< |
assertFalse(sync.hasQueuedThreads()); |
130 |
< |
sync.acquire(1); |
131 |
< |
t1.start(); |
132 |
< |
Thread.sleep(SHORT_DELAY_MS); |
133 |
< |
assertTrue(sync.hasQueuedThreads()); |
134 |
< |
t2.start(); |
135 |
< |
Thread.sleep(SHORT_DELAY_MS); |
136 |
< |
assertTrue(sync.hasQueuedThreads()); |
137 |
< |
t1.interrupt(); |
138 |
< |
Thread.sleep(SHORT_DELAY_MS); |
139 |
< |
assertTrue(sync.hasQueuedThreads()); |
140 |
< |
sync.release(1); |
141 |
< |
Thread.sleep(SHORT_DELAY_MS); |
142 |
< |
assertFalse(sync.hasQueuedThreads()); |
143 |
< |
t1.join(); |
148 |
< |
t2.join(); |
149 |
< |
} catch (Exception e) { |
150 |
< |
unexpectedException(); |
151 |
< |
} |
128 |
> |
assertFalse(sync.hasQueuedThreads()); |
129 |
> |
sync.acquire(1); |
130 |
> |
t1.start(); |
131 |
> |
Thread.sleep(SHORT_DELAY_MS); |
132 |
> |
assertTrue(sync.hasQueuedThreads()); |
133 |
> |
t2.start(); |
134 |
> |
Thread.sleep(SHORT_DELAY_MS); |
135 |
> |
assertTrue(sync.hasQueuedThreads()); |
136 |
> |
t1.interrupt(); |
137 |
> |
Thread.sleep(SHORT_DELAY_MS); |
138 |
> |
assertTrue(sync.hasQueuedThreads()); |
139 |
> |
sync.release(1); |
140 |
> |
Thread.sleep(SHORT_DELAY_MS); |
141 |
> |
assertFalse(sync.hasQueuedThreads()); |
142 |
> |
t1.join(); |
143 |
> |
t2.join(); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
147 |
|
* isQueued(null) throws NPE |
148 |
|
*/ |
149 |
|
public void testIsQueuedNPE() { |
150 |
< |
final Mutex sync = new Mutex(); |
150 |
> |
final Mutex sync = new Mutex(); |
151 |
|
try { |
152 |
|
sync.isQueued(null); |
153 |
|
shouldThrow(); |
154 |
< |
} catch (NullPointerException success) { |
163 |
< |
} |
154 |
> |
} catch (NullPointerException success) {} |
155 |
|
} |
156 |
|
|
157 |
|
/** |
158 |
|
* isQueued reports whether a thread is queued. |
159 |
|
*/ |
160 |
< |
public void testIsQueued() { |
161 |
< |
final Mutex sync = new Mutex(); |
160 |
> |
public void testIsQueued() throws InterruptedException { |
161 |
> |
final Mutex sync = new Mutex(); |
162 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
163 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
164 |
< |
try { |
165 |
< |
assertFalse(sync.isQueued(t1)); |
166 |
< |
assertFalse(sync.isQueued(t2)); |
167 |
< |
sync.acquire(1); |
168 |
< |
t1.start(); |
169 |
< |
Thread.sleep(SHORT_DELAY_MS); |
170 |
< |
assertTrue(sync.isQueued(t1)); |
171 |
< |
t2.start(); |
172 |
< |
Thread.sleep(SHORT_DELAY_MS); |
173 |
< |
assertTrue(sync.isQueued(t1)); |
174 |
< |
assertTrue(sync.isQueued(t2)); |
175 |
< |
t1.interrupt(); |
176 |
< |
Thread.sleep(SHORT_DELAY_MS); |
177 |
< |
assertFalse(sync.isQueued(t1)); |
178 |
< |
assertTrue(sync.isQueued(t2)); |
179 |
< |
sync.release(1); |
180 |
< |
Thread.sleep(SHORT_DELAY_MS); |
181 |
< |
assertFalse(sync.isQueued(t1)); |
182 |
< |
Thread.sleep(SHORT_DELAY_MS); |
183 |
< |
assertFalse(sync.isQueued(t2)); |
184 |
< |
t1.join(); |
194 |
< |
t2.join(); |
195 |
< |
} catch (Exception e) { |
196 |
< |
unexpectedException(); |
197 |
< |
} |
164 |
> |
assertFalse(sync.isQueued(t1)); |
165 |
> |
assertFalse(sync.isQueued(t2)); |
166 |
> |
sync.acquire(1); |
167 |
> |
t1.start(); |
168 |
> |
Thread.sleep(SHORT_DELAY_MS); |
169 |
> |
assertTrue(sync.isQueued(t1)); |
170 |
> |
t2.start(); |
171 |
> |
Thread.sleep(SHORT_DELAY_MS); |
172 |
> |
assertTrue(sync.isQueued(t1)); |
173 |
> |
assertTrue(sync.isQueued(t2)); |
174 |
> |
t1.interrupt(); |
175 |
> |
Thread.sleep(SHORT_DELAY_MS); |
176 |
> |
assertFalse(sync.isQueued(t1)); |
177 |
> |
assertTrue(sync.isQueued(t2)); |
178 |
> |
sync.release(1); |
179 |
> |
Thread.sleep(SHORT_DELAY_MS); |
180 |
> |
assertFalse(sync.isQueued(t1)); |
181 |
> |
Thread.sleep(SHORT_DELAY_MS); |
182 |
> |
assertFalse(sync.isQueued(t2)); |
183 |
> |
t1.join(); |
184 |
> |
t2.join(); |
185 |
|
} |
186 |
|
|
187 |
|
/** |
188 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
189 |
|
*/ |
190 |
< |
public void testGetFirstQueuedThread() { |
191 |
< |
final Mutex sync = new Mutex(); |
190 |
> |
public void testGetFirstQueuedThread() throws InterruptedException { |
191 |
> |
final Mutex sync = new Mutex(); |
192 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
193 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
194 |
< |
try { |
195 |
< |
assertNull(sync.getFirstQueuedThread()); |
196 |
< |
sync.acquire(1); |
197 |
< |
t1.start(); |
198 |
< |
Thread.sleep(SHORT_DELAY_MS); |
199 |
< |
assertEquals(t1, sync.getFirstQueuedThread()); |
200 |
< |
t2.start(); |
201 |
< |
Thread.sleep(SHORT_DELAY_MS); |
202 |
< |
assertEquals(t1, sync.getFirstQueuedThread()); |
203 |
< |
t1.interrupt(); |
204 |
< |
Thread.sleep(SHORT_DELAY_MS); |
205 |
< |
Thread.sleep(SHORT_DELAY_MS); |
206 |
< |
assertEquals(t2, sync.getFirstQueuedThread()); |
207 |
< |
sync.release(1); |
208 |
< |
Thread.sleep(SHORT_DELAY_MS); |
209 |
< |
assertNull(sync.getFirstQueuedThread()); |
210 |
< |
t1.join(); |
224 |
< |
t2.join(); |
225 |
< |
} catch (Exception e) { |
226 |
< |
unexpectedException(); |
227 |
< |
} |
194 |
> |
assertNull(sync.getFirstQueuedThread()); |
195 |
> |
sync.acquire(1); |
196 |
> |
t1.start(); |
197 |
> |
Thread.sleep(SHORT_DELAY_MS); |
198 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
199 |
> |
t2.start(); |
200 |
> |
Thread.sleep(SHORT_DELAY_MS); |
201 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
202 |
> |
t1.interrupt(); |
203 |
> |
Thread.sleep(SHORT_DELAY_MS); |
204 |
> |
Thread.sleep(SHORT_DELAY_MS); |
205 |
> |
assertEquals(t2, sync.getFirstQueuedThread()); |
206 |
> |
sync.release(1); |
207 |
> |
Thread.sleep(SHORT_DELAY_MS); |
208 |
> |
assertNull(sync.getFirstQueuedThread()); |
209 |
> |
t1.join(); |
210 |
> |
t2.join(); |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
|
/** |
215 |
|
* hasContended reports false if no thread has ever blocked, else true |
216 |
|
*/ |
217 |
< |
public void testHasContended() { |
218 |
< |
final Mutex sync = new Mutex(); |
217 |
> |
public void testHasContended() throws InterruptedException { |
218 |
> |
final Mutex sync = new Mutex(); |
219 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
220 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
221 |
< |
try { |
222 |
< |
assertFalse(sync.hasContended()); |
223 |
< |
sync.acquire(1); |
224 |
< |
t1.start(); |
225 |
< |
Thread.sleep(SHORT_DELAY_MS); |
226 |
< |
assertTrue(sync.hasContended()); |
227 |
< |
t2.start(); |
228 |
< |
Thread.sleep(SHORT_DELAY_MS); |
229 |
< |
assertTrue(sync.hasContended()); |
230 |
< |
t1.interrupt(); |
231 |
< |
Thread.sleep(SHORT_DELAY_MS); |
232 |
< |
assertTrue(sync.hasContended()); |
233 |
< |
sync.release(1); |
234 |
< |
Thread.sleep(SHORT_DELAY_MS); |
235 |
< |
assertTrue(sync.hasContended()); |
236 |
< |
t1.join(); |
254 |
< |
t2.join(); |
255 |
< |
} catch (Exception e) { |
256 |
< |
unexpectedException(); |
257 |
< |
} |
221 |
> |
assertFalse(sync.hasContended()); |
222 |
> |
sync.acquire(1); |
223 |
> |
t1.start(); |
224 |
> |
Thread.sleep(SHORT_DELAY_MS); |
225 |
> |
assertTrue(sync.hasContended()); |
226 |
> |
t2.start(); |
227 |
> |
Thread.sleep(SHORT_DELAY_MS); |
228 |
> |
assertTrue(sync.hasContended()); |
229 |
> |
t1.interrupt(); |
230 |
> |
Thread.sleep(SHORT_DELAY_MS); |
231 |
> |
assertTrue(sync.hasContended()); |
232 |
> |
sync.release(1); |
233 |
> |
Thread.sleep(SHORT_DELAY_MS); |
234 |
> |
assertTrue(sync.hasContended()); |
235 |
> |
t1.join(); |
236 |
> |
t2.join(); |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
|
* getQueuedThreads includes waiting threads |
241 |
|
*/ |
242 |
< |
public void testGetQueuedThreads() { |
243 |
< |
final Mutex sync = new Mutex(); |
242 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
243 |
> |
final Mutex sync = new Mutex(); |
244 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
245 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
246 |
< |
try { |
247 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
248 |
< |
sync.acquire(1); |
249 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
250 |
< |
t1.start(); |
251 |
< |
Thread.sleep(SHORT_DELAY_MS); |
252 |
< |
assertTrue(sync.getQueuedThreads().contains(t1)); |
253 |
< |
t2.start(); |
254 |
< |
Thread.sleep(SHORT_DELAY_MS); |
255 |
< |
assertTrue(sync.getQueuedThreads().contains(t1)); |
256 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
257 |
< |
t1.interrupt(); |
258 |
< |
Thread.sleep(SHORT_DELAY_MS); |
259 |
< |
assertFalse(sync.getQueuedThreads().contains(t1)); |
260 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
261 |
< |
sync.release(1); |
262 |
< |
Thread.sleep(SHORT_DELAY_MS); |
263 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
264 |
< |
t1.join(); |
286 |
< |
t2.join(); |
287 |
< |
} catch (Exception e) { |
288 |
< |
unexpectedException(); |
289 |
< |
} |
246 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
247 |
> |
sync.acquire(1); |
248 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
249 |
> |
t1.start(); |
250 |
> |
Thread.sleep(SHORT_DELAY_MS); |
251 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
252 |
> |
t2.start(); |
253 |
> |
Thread.sleep(SHORT_DELAY_MS); |
254 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
255 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
256 |
> |
t1.interrupt(); |
257 |
> |
Thread.sleep(SHORT_DELAY_MS); |
258 |
> |
assertFalse(sync.getQueuedThreads().contains(t1)); |
259 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
260 |
> |
sync.release(1); |
261 |
> |
Thread.sleep(SHORT_DELAY_MS); |
262 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
263 |
> |
t1.join(); |
264 |
> |
t2.join(); |
265 |
|
} |
266 |
|
|
267 |
|
/** |
268 |
|
* getExclusiveQueuedThreads includes waiting threads |
269 |
|
*/ |
270 |
< |
public void testGetExclusiveQueuedThreads() { |
271 |
< |
final Mutex sync = new Mutex(); |
270 |
> |
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
271 |
> |
final Mutex sync = new Mutex(); |
272 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
273 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
274 |
< |
try { |
275 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
276 |
< |
sync.acquire(1); |
277 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
278 |
< |
t1.start(); |
279 |
< |
Thread.sleep(SHORT_DELAY_MS); |
280 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
281 |
< |
t2.start(); |
282 |
< |
Thread.sleep(SHORT_DELAY_MS); |
283 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
284 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
285 |
< |
t1.interrupt(); |
286 |
< |
Thread.sleep(SHORT_DELAY_MS); |
287 |
< |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
288 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
289 |
< |
sync.release(1); |
290 |
< |
Thread.sleep(SHORT_DELAY_MS); |
291 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
292 |
< |
t1.join(); |
318 |
< |
t2.join(); |
319 |
< |
} catch (Exception e) { |
320 |
< |
unexpectedException(); |
321 |
< |
} |
274 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
275 |
> |
sync.acquire(1); |
276 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
277 |
> |
t1.start(); |
278 |
> |
Thread.sleep(SHORT_DELAY_MS); |
279 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
280 |
> |
t2.start(); |
281 |
> |
Thread.sleep(SHORT_DELAY_MS); |
282 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
283 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
284 |
> |
t1.interrupt(); |
285 |
> |
Thread.sleep(SHORT_DELAY_MS); |
286 |
> |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
287 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
288 |
> |
sync.release(1); |
289 |
> |
Thread.sleep(SHORT_DELAY_MS); |
290 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
291 |
> |
t1.join(); |
292 |
> |
t2.join(); |
293 |
|
} |
294 |
|
|
295 |
|
/** |
296 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
297 |
|
*/ |
298 |
< |
public void testGetSharedQueuedThreads() { |
299 |
< |
final Mutex sync = new Mutex(); |
298 |
> |
public void testGetSharedQueuedThreads() throws InterruptedException { |
299 |
> |
final Mutex sync = new Mutex(); |
300 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
301 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
302 |
< |
try { |
303 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
304 |
< |
sync.acquire(1); |
305 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
306 |
< |
t1.start(); |
307 |
< |
Thread.sleep(SHORT_DELAY_MS); |
308 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
309 |
< |
t2.start(); |
310 |
< |
Thread.sleep(SHORT_DELAY_MS); |
311 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
312 |
< |
t1.interrupt(); |
313 |
< |
Thread.sleep(SHORT_DELAY_MS); |
314 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
315 |
< |
sync.release(1); |
316 |
< |
Thread.sleep(SHORT_DELAY_MS); |
317 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
318 |
< |
t1.join(); |
348 |
< |
t2.join(); |
349 |
< |
} catch (Exception e) { |
350 |
< |
unexpectedException(); |
351 |
< |
} |
302 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
303 |
> |
sync.acquire(1); |
304 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
305 |
> |
t1.start(); |
306 |
> |
Thread.sleep(SHORT_DELAY_MS); |
307 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
308 |
> |
t2.start(); |
309 |
> |
Thread.sleep(SHORT_DELAY_MS); |
310 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
311 |
> |
t1.interrupt(); |
312 |
> |
Thread.sleep(SHORT_DELAY_MS); |
313 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
314 |
> |
sync.release(1); |
315 |
> |
Thread.sleep(SHORT_DELAY_MS); |
316 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
317 |
> |
t1.join(); |
318 |
> |
t2.join(); |
319 |
|
} |
320 |
|
|
321 |
|
/** |
322 |
|
* tryAcquireNanos is interruptible. |
323 |
|
*/ |
324 |
< |
public void testInterruptedException2() { |
325 |
< |
final Mutex sync = new Mutex(); |
326 |
< |
sync.acquire(1); |
327 |
< |
Thread t = new Thread(new Runnable() { |
328 |
< |
public void run() { |
329 |
< |
try { |
330 |
< |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
331 |
< |
threadShouldThrow(); |
332 |
< |
} catch (InterruptedException success) {} |
333 |
< |
} |
334 |
< |
}); |
368 |
< |
try { |
369 |
< |
t.start(); |
370 |
< |
t.interrupt(); |
371 |
< |
} catch (Exception e) { |
372 |
< |
unexpectedException(); |
373 |
< |
} |
324 |
> |
public void testInterruptedException2() throws InterruptedException { |
325 |
> |
final Mutex sync = new Mutex(); |
326 |
> |
sync.acquire(1); |
327 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
328 |
> |
public void realRun() throws InterruptedException { |
329 |
> |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
330 |
> |
}}); |
331 |
> |
|
332 |
> |
t.start(); |
333 |
> |
t.interrupt(); |
334 |
> |
t.join(); |
335 |
|
} |
336 |
|
|
337 |
|
|
338 |
|
/** |
339 |
|
* TryAcquire on exclusively held sync fails |
340 |
|
*/ |
341 |
< |
public void testTryAcquireWhenSynced() { |
342 |
< |
final Mutex sync = new Mutex(); |
343 |
< |
sync.acquire(1); |
344 |
< |
Thread t = new Thread(new Runnable() { |
345 |
< |
public void run() { |
346 |
< |
threadAssertFalse(sync.tryAcquire(1)); |
347 |
< |
} |
348 |
< |
}); |
349 |
< |
try { |
350 |
< |
t.start(); |
351 |
< |
t.join(); |
391 |
< |
sync.release(1); |
392 |
< |
} catch (Exception e) { |
393 |
< |
unexpectedException(); |
394 |
< |
} |
341 |
> |
public void testTryAcquireWhenSynced() throws InterruptedException { |
342 |
> |
final Mutex sync = new Mutex(); |
343 |
> |
sync.acquire(1); |
344 |
> |
Thread t = new Thread(new CheckedRunnable() { |
345 |
> |
public void realRun() { |
346 |
> |
threadAssertFalse(sync.tryAcquire(1)); |
347 |
> |
}}); |
348 |
> |
|
349 |
> |
t.start(); |
350 |
> |
t.join(); |
351 |
> |
sync.release(1); |
352 |
|
} |
353 |
|
|
354 |
|
/** |
355 |
|
* tryAcquireNanos on an exclusively held sync times out |
356 |
|
*/ |
357 |
< |
public void testAcquireNanos_Timeout() { |
358 |
< |
final Mutex sync = new Mutex(); |
359 |
< |
sync.acquire(1); |
360 |
< |
Thread t = new Thread(new Runnable() { |
361 |
< |
public void run() { |
362 |
< |
try { |
363 |
< |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
364 |
< |
} catch (Exception ex) { |
365 |
< |
threadUnexpectedException(); |
366 |
< |
} |
367 |
< |
} |
411 |
< |
}); |
412 |
< |
try { |
413 |
< |
t.start(); |
414 |
< |
t.join(); |
415 |
< |
sync.release(1); |
416 |
< |
} catch (Exception e) { |
417 |
< |
unexpectedException(); |
418 |
< |
} |
357 |
> |
public void testAcquireNanos_Timeout() throws InterruptedException { |
358 |
> |
final Mutex sync = new Mutex(); |
359 |
> |
sync.acquire(1); |
360 |
> |
Thread t = new Thread(new CheckedRunnable() { |
361 |
> |
public void realRun() throws InterruptedException { |
362 |
> |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
363 |
> |
}}); |
364 |
> |
|
365 |
> |
t.start(); |
366 |
> |
t.join(); |
367 |
> |
sync.release(1); |
368 |
|
} |
369 |
|
|
370 |
|
|
371 |
|
/** |
372 |
|
* getState is true when acquired and false when not |
373 |
|
*/ |
374 |
< |
public void testGetState() { |
375 |
< |
final Mutex sync = new Mutex(); |
376 |
< |
sync.acquire(1); |
377 |
< |
assertTrue(sync.isHeldExclusively()); |
378 |
< |
sync.release(1); |
379 |
< |
assertFalse(sync.isHeldExclusively()); |
380 |
< |
Thread t = new Thread(new Runnable() { |
381 |
< |
public void run() { |
382 |
< |
sync.acquire(1); |
383 |
< |
try { |
384 |
< |
Thread.sleep(SMALL_DELAY_MS); |
385 |
< |
} |
386 |
< |
catch (Exception e) { |
387 |
< |
threadUnexpectedException(); |
388 |
< |
} |
389 |
< |
sync.release(1); |
390 |
< |
} |
391 |
< |
}); |
443 |
< |
try { |
444 |
< |
t.start(); |
445 |
< |
Thread.sleep(SHORT_DELAY_MS); |
446 |
< |
assertTrue(sync.isHeldExclusively()); |
447 |
< |
t.join(); |
448 |
< |
assertFalse(sync.isHeldExclusively()); |
449 |
< |
} catch (Exception e) { |
450 |
< |
unexpectedException(); |
451 |
< |
} |
374 |
> |
public void testGetState() throws InterruptedException { |
375 |
> |
final Mutex sync = new Mutex(); |
376 |
> |
sync.acquire(1); |
377 |
> |
assertTrue(sync.isHeldExclusively()); |
378 |
> |
sync.release(1); |
379 |
> |
assertFalse(sync.isHeldExclusively()); |
380 |
> |
Thread t = new Thread(new CheckedRunnable() { |
381 |
> |
public void realRun() throws InterruptedException { |
382 |
> |
sync.acquire(1); |
383 |
> |
Thread.sleep(SMALL_DELAY_MS); |
384 |
> |
sync.release(1); |
385 |
> |
}}); |
386 |
> |
|
387 |
> |
t.start(); |
388 |
> |
Thread.sleep(SHORT_DELAY_MS); |
389 |
> |
assertTrue(sync.isHeldExclusively()); |
390 |
> |
t.join(); |
391 |
> |
assertFalse(sync.isHeldExclusively()); |
392 |
|
} |
393 |
|
|
394 |
|
|
395 |
|
/** |
396 |
|
* acquireInterruptibly is interruptible. |
397 |
|
*/ |
398 |
< |
public void testAcquireInterruptibly1() { |
399 |
< |
final Mutex sync = new Mutex(); |
400 |
< |
sync.acquire(1); |
401 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
402 |
< |
try { |
403 |
< |
t.start(); |
404 |
< |
Thread.sleep(SHORT_DELAY_MS); |
405 |
< |
t.interrupt(); |
406 |
< |
Thread.sleep(SHORT_DELAY_MS); |
407 |
< |
sync.release(1); |
408 |
< |
t.join(); |
469 |
< |
} catch (Exception e) { |
470 |
< |
unexpectedException(); |
471 |
< |
} |
398 |
> |
public void testAcquireInterruptibly1() throws InterruptedException { |
399 |
> |
final Mutex sync = new Mutex(); |
400 |
> |
sync.acquire(1); |
401 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
402 |
> |
|
403 |
> |
t.start(); |
404 |
> |
Thread.sleep(SHORT_DELAY_MS); |
405 |
> |
t.interrupt(); |
406 |
> |
Thread.sleep(SHORT_DELAY_MS); |
407 |
> |
sync.release(1); |
408 |
> |
t.join(); |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
413 |
|
*/ |
414 |
< |
public void testAcquireInterruptibly2() { |
415 |
< |
final Mutex sync = new Mutex(); |
416 |
< |
try { |
417 |
< |
sync.acquireInterruptibly(1); |
418 |
< |
} catch (Exception e) { |
419 |
< |
unexpectedException(); |
420 |
< |
} |
421 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
485 |
< |
try { |
486 |
< |
t.start(); |
487 |
< |
t.interrupt(); |
488 |
< |
assertTrue(sync.isHeldExclusively()); |
489 |
< |
t.join(); |
490 |
< |
} catch (Exception e) { |
491 |
< |
unexpectedException(); |
492 |
< |
} |
414 |
> |
public void testAcquireInterruptibly2() throws InterruptedException { |
415 |
> |
final Mutex sync = new Mutex(); |
416 |
> |
sync.acquireInterruptibly(1); |
417 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
418 |
> |
t.start(); |
419 |
> |
t.interrupt(); |
420 |
> |
assertTrue(sync.isHeldExclusively()); |
421 |
> |
t.join(); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
|
* owns is true for a condition created by sync else false |
426 |
|
*/ |
427 |
|
public void testOwns() { |
428 |
< |
final Mutex sync = new Mutex(); |
428 |
> |
final Mutex sync = new Mutex(); |
429 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
430 |
|
final Mutex sync2 = new Mutex(); |
431 |
|
assertTrue(sync.owns(c)); |
435 |
|
/** |
436 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
437 |
|
*/ |
438 |
< |
public void testAwait_IllegalMonitor() { |
439 |
< |
final Mutex sync = new Mutex(); |
438 |
> |
public void testAwait_IllegalMonitor() throws InterruptedException { |
439 |
> |
final Mutex sync = new Mutex(); |
440 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
441 |
|
try { |
442 |
|
c.await(); |
443 |
|
shouldThrow(); |
444 |
< |
} |
516 |
< |
catch (IllegalMonitorStateException success) { |
517 |
< |
} |
518 |
< |
catch (Exception ex) { |
519 |
< |
unexpectedException(); |
520 |
< |
} |
444 |
> |
} catch (IllegalMonitorStateException success) {} |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
449 |
|
*/ |
450 |
|
public void testSignal_IllegalMonitor() { |
451 |
< |
final Mutex sync = new Mutex(); |
451 |
> |
final Mutex sync = new Mutex(); |
452 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
453 |
|
try { |
454 |
|
c.signal(); |
455 |
|
shouldThrow(); |
456 |
< |
} |
533 |
< |
catch (IllegalMonitorStateException success) { |
534 |
< |
} |
535 |
< |
catch (Exception ex) { |
536 |
< |
unexpectedException(); |
537 |
< |
} |
456 |
> |
} catch (IllegalMonitorStateException success) {} |
457 |
|
} |
458 |
|
|
459 |
|
/** |
460 |
|
* awaitNanos without a signal times out |
461 |
|
*/ |
462 |
< |
public void testAwaitNanos_Timeout() { |
463 |
< |
final Mutex sync = new Mutex(); |
462 |
> |
public void testAwaitNanos_Timeout() throws InterruptedException { |
463 |
> |
final Mutex sync = new Mutex(); |
464 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
465 |
< |
try { |
466 |
< |
sync.acquire(1); |
467 |
< |
long t = c.awaitNanos(100); |
468 |
< |
assertTrue(t <= 0); |
550 |
< |
sync.release(1); |
551 |
< |
} |
552 |
< |
catch (Exception ex) { |
553 |
< |
unexpectedException(); |
554 |
< |
} |
465 |
> |
sync.acquire(1); |
466 |
> |
long t = c.awaitNanos(100); |
467 |
> |
assertTrue(t <= 0); |
468 |
> |
sync.release(1); |
469 |
|
} |
470 |
|
|
471 |
|
/** |
472 |
|
* Timed await without a signal times out |
473 |
|
*/ |
474 |
< |
public void testAwait_Timeout() { |
475 |
< |
final Mutex sync = new Mutex(); |
474 |
> |
public void testAwait_Timeout() throws InterruptedException { |
475 |
> |
final Mutex sync = new Mutex(); |
476 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
477 |
< |
try { |
478 |
< |
sync.acquire(1); |
479 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
566 |
< |
sync.release(1); |
567 |
< |
} |
568 |
< |
catch (Exception ex) { |
569 |
< |
unexpectedException(); |
570 |
< |
} |
477 |
> |
sync.acquire(1); |
478 |
> |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
479 |
> |
sync.release(1); |
480 |
|
} |
481 |
|
|
482 |
|
/** |
483 |
|
* awaitUntil without a signal times out |
484 |
|
*/ |
485 |
< |
public void testAwaitUntil_Timeout() { |
486 |
< |
final Mutex sync = new Mutex(); |
485 |
> |
public void testAwaitUntil_Timeout() throws InterruptedException { |
486 |
> |
final Mutex sync = new Mutex(); |
487 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
488 |
< |
try { |
489 |
< |
sync.acquire(1); |
490 |
< |
java.util.Date d = new java.util.Date(); |
491 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
583 |
< |
sync.release(1); |
584 |
< |
} |
585 |
< |
catch (Exception ex) { |
586 |
< |
unexpectedException(); |
587 |
< |
} |
488 |
> |
sync.acquire(1); |
489 |
> |
java.util.Date d = new java.util.Date(); |
490 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
491 |
> |
sync.release(1); |
492 |
|
} |
493 |
|
|
494 |
|
/** |
495 |
|
* await returns when signalled |
496 |
|
*/ |
497 |
< |
public void testAwait() { |
498 |
< |
final Mutex sync = new Mutex(); |
497 |
> |
public void testAwait() throws InterruptedException { |
498 |
> |
final Mutex sync = new Mutex(); |
499 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
500 |
< |
Thread t = new Thread(new Runnable() { |
501 |
< |
public void run() { |
502 |
< |
try { |
503 |
< |
sync.acquire(1); |
504 |
< |
c.await(); |
505 |
< |
sync.release(1); |
602 |
< |
} |
603 |
< |
catch (InterruptedException e) { |
604 |
< |
threadUnexpectedException(); |
605 |
< |
} |
606 |
< |
} |
607 |
< |
}); |
500 |
> |
Thread t = new Thread(new CheckedRunnable() { |
501 |
> |
public void realRun() throws InterruptedException { |
502 |
> |
sync.acquire(1); |
503 |
> |
c.await(); |
504 |
> |
sync.release(1); |
505 |
> |
}}); |
506 |
|
|
507 |
< |
try { |
508 |
< |
t.start(); |
509 |
< |
Thread.sleep(SHORT_DELAY_MS); |
510 |
< |
sync.acquire(1); |
511 |
< |
c.signal(); |
512 |
< |
sync.release(1); |
513 |
< |
t.join(SHORT_DELAY_MS); |
616 |
< |
assertFalse(t.isAlive()); |
617 |
< |
} |
618 |
< |
catch (Exception ex) { |
619 |
< |
unexpectedException(); |
620 |
< |
} |
507 |
> |
t.start(); |
508 |
> |
Thread.sleep(SHORT_DELAY_MS); |
509 |
> |
sync.acquire(1); |
510 |
> |
c.signal(); |
511 |
> |
sync.release(1); |
512 |
> |
t.join(SHORT_DELAY_MS); |
513 |
> |
assertFalse(t.isAlive()); |
514 |
|
} |
515 |
|
|
516 |
|
|
519 |
|
* hasWaiters throws NPE if null |
520 |
|
*/ |
521 |
|
public void testHasWaitersNPE() { |
522 |
< |
final Mutex sync = new Mutex(); |
522 |
> |
final Mutex sync = new Mutex(); |
523 |
|
try { |
524 |
|
sync.hasWaiters(null); |
525 |
|
shouldThrow(); |
526 |
< |
} catch (NullPointerException success) { |
634 |
< |
} catch (Exception ex) { |
635 |
< |
unexpectedException(); |
636 |
< |
} |
526 |
> |
} catch (NullPointerException success) {} |
527 |
|
} |
528 |
|
|
529 |
|
/** |
530 |
|
* getWaitQueueLength throws NPE if null |
531 |
|
*/ |
532 |
|
public void testGetWaitQueueLengthNPE() { |
533 |
< |
final Mutex sync = new Mutex(); |
533 |
> |
final Mutex sync = new Mutex(); |
534 |
|
try { |
535 |
|
sync.getWaitQueueLength(null); |
536 |
|
shouldThrow(); |
537 |
< |
} catch (NullPointerException success) { |
648 |
< |
} catch (Exception ex) { |
649 |
< |
unexpectedException(); |
650 |
< |
} |
537 |
> |
} catch (NullPointerException success) {} |
538 |
|
} |
539 |
|
|
540 |
|
|
542 |
|
* getWaitingThreads throws NPE if null |
543 |
|
*/ |
544 |
|
public void testGetWaitingThreadsNPE() { |
545 |
< |
final Mutex sync = new Mutex(); |
545 |
> |
final Mutex sync = new Mutex(); |
546 |
|
try { |
547 |
|
sync.getWaitingThreads(null); |
548 |
|
shouldThrow(); |
549 |
< |
} catch (NullPointerException success) { |
663 |
< |
} catch (Exception ex) { |
664 |
< |
unexpectedException(); |
665 |
< |
} |
549 |
> |
} catch (NullPointerException success) {} |
550 |
|
} |
551 |
|
|
552 |
|
|
554 |
|
* hasWaiters throws IAE if not owned |
555 |
|
*/ |
556 |
|
public void testHasWaitersIAE() { |
557 |
< |
final Mutex sync = new Mutex(); |
557 |
> |
final Mutex sync = new Mutex(); |
558 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
559 |
< |
final Mutex sync2 = new Mutex(); |
559 |
> |
final Mutex sync2 = new Mutex(); |
560 |
|
try { |
561 |
|
sync2.hasWaiters(c); |
562 |
|
shouldThrow(); |
563 |
< |
} catch (IllegalArgumentException success) { |
680 |
< |
} catch (Exception ex) { |
681 |
< |
unexpectedException(); |
682 |
< |
} |
563 |
> |
} catch (IllegalArgumentException success) {} |
564 |
|
} |
565 |
|
|
566 |
|
/** |
567 |
|
* hasWaiters throws IMSE if not synced |
568 |
|
*/ |
569 |
|
public void testHasWaitersIMSE() { |
570 |
< |
final Mutex sync = new Mutex(); |
570 |
> |
final Mutex sync = new Mutex(); |
571 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
572 |
|
try { |
573 |
|
sync.hasWaiters(c); |
574 |
|
shouldThrow(); |
575 |
< |
} catch (IllegalMonitorStateException success) { |
695 |
< |
} catch (Exception ex) { |
696 |
< |
unexpectedException(); |
697 |
< |
} |
575 |
> |
} catch (IllegalMonitorStateException success) {} |
576 |
|
} |
577 |
|
|
578 |
|
|
580 |
|
* getWaitQueueLength throws IAE if not owned |
581 |
|
*/ |
582 |
|
public void testGetWaitQueueLengthIAE() { |
583 |
< |
final Mutex sync = new Mutex(); |
583 |
> |
final Mutex sync = new Mutex(); |
584 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
585 |
< |
final Mutex sync2 = new Mutex(); |
585 |
> |
final Mutex sync2 = new Mutex(); |
586 |
|
try { |
587 |
|
sync2.getWaitQueueLength(c); |
588 |
|
shouldThrow(); |
589 |
< |
} catch (IllegalArgumentException success) { |
712 |
< |
} catch (Exception ex) { |
713 |
< |
unexpectedException(); |
714 |
< |
} |
589 |
> |
} catch (IllegalArgumentException success) {} |
590 |
|
} |
591 |
|
|
592 |
|
/** |
593 |
|
* getWaitQueueLength throws IMSE if not synced |
594 |
|
*/ |
595 |
|
public void testGetWaitQueueLengthIMSE() { |
596 |
< |
final Mutex sync = new Mutex(); |
596 |
> |
final Mutex sync = new Mutex(); |
597 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
598 |
|
try { |
599 |
|
sync.getWaitQueueLength(c); |
600 |
|
shouldThrow(); |
601 |
< |
} catch (IllegalMonitorStateException success) { |
727 |
< |
} catch (Exception ex) { |
728 |
< |
unexpectedException(); |
729 |
< |
} |
601 |
> |
} catch (IllegalMonitorStateException success) {} |
602 |
|
} |
603 |
|
|
604 |
|
|
606 |
|
* getWaitingThreads throws IAE if not owned |
607 |
|
*/ |
608 |
|
public void testGetWaitingThreadsIAE() { |
609 |
< |
final Mutex sync = new Mutex(); |
609 |
> |
final Mutex sync = new Mutex(); |
610 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
611 |
< |
final Mutex sync2 = new Mutex(); |
611 |
> |
final Mutex sync2 = new Mutex(); |
612 |
|
try { |
613 |
|
sync2.getWaitingThreads(c); |
614 |
|
shouldThrow(); |
615 |
< |
} catch (IllegalArgumentException success) { |
744 |
< |
} catch (Exception ex) { |
745 |
< |
unexpectedException(); |
746 |
< |
} |
615 |
> |
} catch (IllegalArgumentException success) {} |
616 |
|
} |
617 |
|
|
618 |
|
/** |
619 |
|
* getWaitingThreads throws IMSE if not synced |
620 |
|
*/ |
621 |
|
public void testGetWaitingThreadsIMSE() { |
622 |
< |
final Mutex sync = new Mutex(); |
622 |
> |
final Mutex sync = new Mutex(); |
623 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
624 |
|
try { |
625 |
|
sync.getWaitingThreads(c); |
626 |
|
shouldThrow(); |
627 |
< |
} catch (IllegalMonitorStateException success) { |
759 |
< |
} catch (Exception ex) { |
760 |
< |
unexpectedException(); |
761 |
< |
} |
627 |
> |
} catch (IllegalMonitorStateException success) {} |
628 |
|
} |
629 |
|
|
630 |
|
|
632 |
|
/** |
633 |
|
* hasWaiters returns true when a thread is waiting, else false |
634 |
|
*/ |
635 |
< |
public void testHasWaiters() { |
636 |
< |
final Mutex sync = new Mutex(); |
635 |
> |
public void testHasWaiters() throws InterruptedException { |
636 |
> |
final Mutex sync = new Mutex(); |
637 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
638 |
< |
Thread t = new Thread(new Runnable() { |
639 |
< |
public void run() { |
640 |
< |
try { |
641 |
< |
sync.acquire(1); |
642 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
643 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
644 |
< |
c.await(); |
645 |
< |
sync.release(1); |
780 |
< |
} |
781 |
< |
catch (InterruptedException e) { |
782 |
< |
threadUnexpectedException(); |
783 |
< |
} |
784 |
< |
} |
785 |
< |
}); |
638 |
> |
Thread t = new Thread(new CheckedRunnable() { |
639 |
> |
public void realRun() throws InterruptedException { |
640 |
> |
sync.acquire(1); |
641 |
> |
threadAssertFalse(sync.hasWaiters(c)); |
642 |
> |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
643 |
> |
c.await(); |
644 |
> |
sync.release(1); |
645 |
> |
}}); |
646 |
|
|
647 |
< |
try { |
648 |
< |
t.start(); |
649 |
< |
Thread.sleep(SHORT_DELAY_MS); |
650 |
< |
sync.acquire(1); |
651 |
< |
assertTrue(sync.hasWaiters(c)); |
652 |
< |
assertEquals(1, sync.getWaitQueueLength(c)); |
653 |
< |
c.signal(); |
654 |
< |
sync.release(1); |
655 |
< |
Thread.sleep(SHORT_DELAY_MS); |
656 |
< |
sync.acquire(1); |
657 |
< |
assertFalse(sync.hasWaiters(c)); |
658 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
659 |
< |
sync.release(1); |
660 |
< |
t.join(SHORT_DELAY_MS); |
801 |
< |
assertFalse(t.isAlive()); |
802 |
< |
} |
803 |
< |
catch (Exception ex) { |
804 |
< |
unexpectedException(); |
805 |
< |
} |
647 |
> |
t.start(); |
648 |
> |
Thread.sleep(SHORT_DELAY_MS); |
649 |
> |
sync.acquire(1); |
650 |
> |
assertTrue(sync.hasWaiters(c)); |
651 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
652 |
> |
c.signal(); |
653 |
> |
sync.release(1); |
654 |
> |
Thread.sleep(SHORT_DELAY_MS); |
655 |
> |
sync.acquire(1); |
656 |
> |
assertFalse(sync.hasWaiters(c)); |
657 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
658 |
> |
sync.release(1); |
659 |
> |
t.join(SHORT_DELAY_MS); |
660 |
> |
assertFalse(t.isAlive()); |
661 |
|
} |
662 |
|
|
663 |
|
/** |
664 |
|
* getWaitQueueLength returns number of waiting threads |
665 |
|
*/ |
666 |
< |
public void testGetWaitQueueLength() { |
667 |
< |
final Mutex sync = new Mutex(); |
666 |
> |
public void testGetWaitQueueLength() throws InterruptedException { |
667 |
> |
final Mutex sync = new Mutex(); |
668 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
669 |
< |
Thread t1 = new Thread(new Runnable() { |
670 |
< |
public void run() { |
671 |
< |
try { |
672 |
< |
sync.acquire(1); |
673 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
674 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
675 |
< |
c.await(); |
676 |
< |
sync.release(1); |
677 |
< |
} |
678 |
< |
catch (InterruptedException e) { |
679 |
< |
threadUnexpectedException(); |
680 |
< |
} |
681 |
< |
} |
682 |
< |
}); |
683 |
< |
|
684 |
< |
Thread t2 = new Thread(new Runnable() { |
685 |
< |
public void run() { |
686 |
< |
try { |
687 |
< |
sync.acquire(1); |
688 |
< |
threadAssertTrue(sync.hasWaiters(c)); |
689 |
< |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
690 |
< |
c.await(); |
691 |
< |
sync.release(1); |
692 |
< |
} |
693 |
< |
catch (InterruptedException e) { |
694 |
< |
threadUnexpectedException(); |
695 |
< |
} |
696 |
< |
} |
697 |
< |
}); |
698 |
< |
|
699 |
< |
try { |
700 |
< |
t1.start(); |
701 |
< |
Thread.sleep(SHORT_DELAY_MS); |
702 |
< |
t2.start(); |
703 |
< |
Thread.sleep(SHORT_DELAY_MS); |
704 |
< |
sync.acquire(1); |
850 |
< |
assertTrue(sync.hasWaiters(c)); |
851 |
< |
assertEquals(2, sync.getWaitQueueLength(c)); |
852 |
< |
c.signalAll(); |
853 |
< |
sync.release(1); |
854 |
< |
Thread.sleep(SHORT_DELAY_MS); |
855 |
< |
sync.acquire(1); |
856 |
< |
assertFalse(sync.hasWaiters(c)); |
857 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
858 |
< |
sync.release(1); |
859 |
< |
t1.join(SHORT_DELAY_MS); |
860 |
< |
t2.join(SHORT_DELAY_MS); |
861 |
< |
assertFalse(t1.isAlive()); |
862 |
< |
assertFalse(t2.isAlive()); |
863 |
< |
} |
864 |
< |
catch (Exception ex) { |
865 |
< |
unexpectedException(); |
866 |
< |
} |
669 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
670 |
> |
public void realRun() throws InterruptedException { |
671 |
> |
sync.acquire(1); |
672 |
> |
threadAssertFalse(sync.hasWaiters(c)); |
673 |
> |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
674 |
> |
c.await(); |
675 |
> |
sync.release(1); |
676 |
> |
}}); |
677 |
> |
|
678 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
679 |
> |
public void realRun() throws InterruptedException { |
680 |
> |
sync.acquire(1); |
681 |
> |
threadAssertTrue(sync.hasWaiters(c)); |
682 |
> |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
683 |
> |
c.await(); |
684 |
> |
sync.release(1); |
685 |
> |
}}); |
686 |
> |
|
687 |
> |
t1.start(); |
688 |
> |
Thread.sleep(SHORT_DELAY_MS); |
689 |
> |
t2.start(); |
690 |
> |
Thread.sleep(SHORT_DELAY_MS); |
691 |
> |
sync.acquire(1); |
692 |
> |
assertTrue(sync.hasWaiters(c)); |
693 |
> |
assertEquals(2, sync.getWaitQueueLength(c)); |
694 |
> |
c.signalAll(); |
695 |
> |
sync.release(1); |
696 |
> |
Thread.sleep(SHORT_DELAY_MS); |
697 |
> |
sync.acquire(1); |
698 |
> |
assertFalse(sync.hasWaiters(c)); |
699 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
700 |
> |
sync.release(1); |
701 |
> |
t1.join(SHORT_DELAY_MS); |
702 |
> |
t2.join(SHORT_DELAY_MS); |
703 |
> |
assertFalse(t1.isAlive()); |
704 |
> |
assertFalse(t2.isAlive()); |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
|
* getWaitingThreads returns only and all waiting threads |
709 |
|
*/ |
710 |
< |
public void testGetWaitingThreads() { |
711 |
< |
final Mutex sync = new Mutex(); |
710 |
> |
public void testGetWaitingThreads() throws InterruptedException { |
711 |
> |
final Mutex sync = new Mutex(); |
712 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
713 |
< |
Thread t1 = new Thread(new Runnable() { |
714 |
< |
public void run() { |
715 |
< |
try { |
716 |
< |
sync.acquire(1); |
717 |
< |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
718 |
< |
c.await(); |
719 |
< |
sync.release(1); |
720 |
< |
} |
721 |
< |
catch (InterruptedException e) { |
722 |
< |
threadUnexpectedException(); |
723 |
< |
} |
724 |
< |
} |
725 |
< |
}); |
726 |
< |
|
727 |
< |
Thread t2 = new Thread(new Runnable() { |
890 |
< |
public void run() { |
891 |
< |
try { |
892 |
< |
sync.acquire(1); |
893 |
< |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
894 |
< |
c.await(); |
895 |
< |
sync.release(1); |
896 |
< |
} |
897 |
< |
catch (InterruptedException e) { |
898 |
< |
threadUnexpectedException(); |
899 |
< |
} |
900 |
< |
} |
901 |
< |
}); |
713 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
714 |
> |
public void realRun() throws InterruptedException { |
715 |
> |
sync.acquire(1); |
716 |
> |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
717 |
> |
c.await(); |
718 |
> |
sync.release(1); |
719 |
> |
}}); |
720 |
> |
|
721 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
722 |
> |
public void realRun() throws InterruptedException { |
723 |
> |
sync.acquire(1); |
724 |
> |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
725 |
> |
c.await(); |
726 |
> |
sync.release(1); |
727 |
> |
}}); |
728 |
|
|
729 |
< |
try { |
730 |
< |
sync.acquire(1); |
731 |
< |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
732 |
< |
sync.release(1); |
733 |
< |
t1.start(); |
734 |
< |
Thread.sleep(SHORT_DELAY_MS); |
735 |
< |
t2.start(); |
736 |
< |
Thread.sleep(SHORT_DELAY_MS); |
737 |
< |
sync.acquire(1); |
738 |
< |
assertTrue(sync.hasWaiters(c)); |
739 |
< |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
740 |
< |
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
741 |
< |
c.signalAll(); |
742 |
< |
sync.release(1); |
743 |
< |
Thread.sleep(SHORT_DELAY_MS); |
744 |
< |
sync.acquire(1); |
745 |
< |
assertFalse(sync.hasWaiters(c)); |
746 |
< |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
747 |
< |
sync.release(1); |
748 |
< |
t1.join(SHORT_DELAY_MS); |
749 |
< |
t2.join(SHORT_DELAY_MS); |
750 |
< |
assertFalse(t1.isAlive()); |
925 |
< |
assertFalse(t2.isAlive()); |
926 |
< |
} |
927 |
< |
catch (Exception ex) { |
928 |
< |
unexpectedException(); |
929 |
< |
} |
729 |
> |
sync.acquire(1); |
730 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
731 |
> |
sync.release(1); |
732 |
> |
t1.start(); |
733 |
> |
Thread.sleep(SHORT_DELAY_MS); |
734 |
> |
t2.start(); |
735 |
> |
Thread.sleep(SHORT_DELAY_MS); |
736 |
> |
sync.acquire(1); |
737 |
> |
assertTrue(sync.hasWaiters(c)); |
738 |
> |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
739 |
> |
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
740 |
> |
c.signalAll(); |
741 |
> |
sync.release(1); |
742 |
> |
Thread.sleep(SHORT_DELAY_MS); |
743 |
> |
sync.acquire(1); |
744 |
> |
assertFalse(sync.hasWaiters(c)); |
745 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
746 |
> |
sync.release(1); |
747 |
> |
t1.join(SHORT_DELAY_MS); |
748 |
> |
t2.join(SHORT_DELAY_MS); |
749 |
> |
assertFalse(t1.isAlive()); |
750 |
> |
assertFalse(t2.isAlive()); |
751 |
|
} |
752 |
|
|
753 |
|
|
755 |
|
/** |
756 |
|
* awaitUninterruptibly doesn't abort on interrupt |
757 |
|
*/ |
758 |
< |
public void testAwaitUninterruptibly() { |
759 |
< |
final Mutex sync = new Mutex(); |
758 |
> |
public void testAwaitUninterruptibly() throws InterruptedException { |
759 |
> |
final Mutex sync = new Mutex(); |
760 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
761 |
< |
Thread t = new Thread(new Runnable() { |
762 |
< |
public void run() { |
763 |
< |
sync.acquire(1); |
764 |
< |
c.awaitUninterruptibly(); |
765 |
< |
sync.release(1); |
766 |
< |
} |
946 |
< |
}); |
761 |
> |
Thread t = new Thread(new CheckedRunnable() { |
762 |
> |
public void realRun() { |
763 |
> |
sync.acquire(1); |
764 |
> |
c.awaitUninterruptibly(); |
765 |
> |
sync.release(1); |
766 |
> |
}}); |
767 |
|
|
768 |
< |
try { |
769 |
< |
t.start(); |
770 |
< |
Thread.sleep(SHORT_DELAY_MS); |
771 |
< |
t.interrupt(); |
772 |
< |
sync.acquire(1); |
773 |
< |
c.signal(); |
774 |
< |
sync.release(1); |
775 |
< |
t.join(SHORT_DELAY_MS); |
956 |
< |
assertFalse(t.isAlive()); |
957 |
< |
} |
958 |
< |
catch (Exception ex) { |
959 |
< |
unexpectedException(); |
960 |
< |
} |
768 |
> |
t.start(); |
769 |
> |
Thread.sleep(SHORT_DELAY_MS); |
770 |
> |
t.interrupt(); |
771 |
> |
sync.acquire(1); |
772 |
> |
c.signal(); |
773 |
> |
sync.release(1); |
774 |
> |
t.join(SHORT_DELAY_MS); |
775 |
> |
assertFalse(t.isAlive()); |
776 |
|
} |
777 |
|
|
778 |
|
/** |
779 |
|
* await is interruptible |
780 |
|
*/ |
781 |
< |
public void testAwait_Interrupt() { |
782 |
< |
final Mutex sync = new Mutex(); |
781 |
> |
public void testAwait_Interrupt() throws InterruptedException { |
782 |
> |
final Mutex sync = new Mutex(); |
783 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
784 |
< |
Thread t = new Thread(new Runnable() { |
785 |
< |
public void run() { |
786 |
< |
try { |
787 |
< |
sync.acquire(1); |
788 |
< |
c.await(); |
789 |
< |
sync.release(1); |
975 |
< |
threadShouldThrow(); |
976 |
< |
} |
977 |
< |
catch (InterruptedException success) { |
978 |
< |
} |
979 |
< |
} |
980 |
< |
}); |
784 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
785 |
> |
public void realRun() throws InterruptedException { |
786 |
> |
sync.acquire(1); |
787 |
> |
c.await(); |
788 |
> |
sync.release(1); |
789 |
> |
}}); |
790 |
|
|
791 |
< |
try { |
792 |
< |
t.start(); |
793 |
< |
Thread.sleep(SHORT_DELAY_MS); |
794 |
< |
t.interrupt(); |
795 |
< |
t.join(SHORT_DELAY_MS); |
987 |
< |
assertFalse(t.isAlive()); |
988 |
< |
} |
989 |
< |
catch (Exception ex) { |
990 |
< |
unexpectedException(); |
991 |
< |
} |
791 |
> |
t.start(); |
792 |
> |
Thread.sleep(SHORT_DELAY_MS); |
793 |
> |
t.interrupt(); |
794 |
> |
t.join(SHORT_DELAY_MS); |
795 |
> |
assertFalse(t.isAlive()); |
796 |
|
} |
797 |
|
|
798 |
|
/** |
799 |
|
* awaitNanos is interruptible |
800 |
|
*/ |
801 |
< |
public void testAwaitNanos_Interrupt() { |
802 |
< |
final Mutex sync = new Mutex(); |
801 |
> |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
802 |
> |
final Mutex sync = new Mutex(); |
803 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
804 |
< |
Thread t = new Thread(new Runnable() { |
805 |
< |
public void run() { |
806 |
< |
try { |
807 |
< |
sync.acquire(1); |
808 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
809 |
< |
sync.release(1); |
1006 |
< |
threadShouldThrow(); |
1007 |
< |
} |
1008 |
< |
catch (InterruptedException success) { |
1009 |
< |
} |
1010 |
< |
} |
1011 |
< |
}); |
804 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
805 |
> |
public void realRun() throws InterruptedException { |
806 |
> |
sync.acquire(1); |
807 |
> |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
808 |
> |
sync.release(1); |
809 |
> |
}}); |
810 |
|
|
811 |
< |
try { |
812 |
< |
t.start(); |
813 |
< |
Thread.sleep(SHORT_DELAY_MS); |
814 |
< |
t.interrupt(); |
815 |
< |
t.join(SHORT_DELAY_MS); |
1018 |
< |
assertFalse(t.isAlive()); |
1019 |
< |
} |
1020 |
< |
catch (Exception ex) { |
1021 |
< |
unexpectedException(); |
1022 |
< |
} |
811 |
> |
t.start(); |
812 |
> |
Thread.sleep(SHORT_DELAY_MS); |
813 |
> |
t.interrupt(); |
814 |
> |
t.join(SHORT_DELAY_MS); |
815 |
> |
assertFalse(t.isAlive()); |
816 |
|
} |
817 |
|
|
818 |
|
/** |
819 |
|
* awaitUntil is interruptible |
820 |
|
*/ |
821 |
< |
public void testAwaitUntil_Interrupt() { |
822 |
< |
final Mutex sync = new Mutex(); |
821 |
> |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
822 |
> |
final Mutex sync = new Mutex(); |
823 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
824 |
< |
Thread t = new Thread(new Runnable() { |
825 |
< |
public void run() { |
826 |
< |
try { |
827 |
< |
sync.acquire(1); |
828 |
< |
java.util.Date d = new java.util.Date(); |
829 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
830 |
< |
sync.release(1); |
1038 |
< |
threadShouldThrow(); |
1039 |
< |
} |
1040 |
< |
catch (InterruptedException success) { |
1041 |
< |
} |
1042 |
< |
} |
1043 |
< |
}); |
824 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
825 |
> |
public void realRun() throws InterruptedException { |
826 |
> |
sync.acquire(1); |
827 |
> |
java.util.Date d = new java.util.Date(); |
828 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
829 |
> |
sync.release(1); |
830 |
> |
}}); |
831 |
|
|
832 |
< |
try { |
833 |
< |
t.start(); |
834 |
< |
Thread.sleep(SHORT_DELAY_MS); |
835 |
< |
t.interrupt(); |
836 |
< |
t.join(SHORT_DELAY_MS); |
1050 |
< |
assertFalse(t.isAlive()); |
1051 |
< |
} |
1052 |
< |
catch (Exception ex) { |
1053 |
< |
unexpectedException(); |
1054 |
< |
} |
832 |
> |
t.start(); |
833 |
> |
Thread.sleep(SHORT_DELAY_MS); |
834 |
> |
t.interrupt(); |
835 |
> |
t.join(SHORT_DELAY_MS); |
836 |
> |
assertFalse(t.isAlive()); |
837 |
|
} |
838 |
|
|
839 |
|
/** |
840 |
|
* signalAll wakes up all threads |
841 |
|
*/ |
842 |
< |
public void testSignalAll() { |
843 |
< |
final Mutex sync = new Mutex(); |
842 |
> |
public void testSignalAll() throws InterruptedException { |
843 |
> |
final Mutex sync = new Mutex(); |
844 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
845 |
< |
Thread t1 = new Thread(new Runnable() { |
846 |
< |
public void run() { |
847 |
< |
try { |
848 |
< |
sync.acquire(1); |
849 |
< |
c.await(); |
850 |
< |
sync.release(1); |
851 |
< |
} |
852 |
< |
catch (InterruptedException e) { |
853 |
< |
threadUnexpectedException(); |
854 |
< |
} |
855 |
< |
} |
856 |
< |
}); |
857 |
< |
|
858 |
< |
Thread t2 = new Thread(new Runnable() { |
859 |
< |
public void run() { |
860 |
< |
try { |
861 |
< |
sync.acquire(1); |
862 |
< |
c.await(); |
863 |
< |
sync.release(1); |
864 |
< |
} |
865 |
< |
catch (InterruptedException e) { |
866 |
< |
threadUnexpectedException(); |
867 |
< |
} |
868 |
< |
} |
1087 |
< |
}); |
1088 |
< |
|
1089 |
< |
try { |
1090 |
< |
t1.start(); |
1091 |
< |
t2.start(); |
1092 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1093 |
< |
sync.acquire(1); |
1094 |
< |
c.signalAll(); |
1095 |
< |
sync.release(1); |
1096 |
< |
t1.join(SHORT_DELAY_MS); |
1097 |
< |
t2.join(SHORT_DELAY_MS); |
1098 |
< |
assertFalse(t1.isAlive()); |
1099 |
< |
assertFalse(t2.isAlive()); |
1100 |
< |
} |
1101 |
< |
catch (Exception ex) { |
1102 |
< |
unexpectedException(); |
1103 |
< |
} |
845 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
846 |
> |
public void realRun() throws InterruptedException { |
847 |
> |
sync.acquire(1); |
848 |
> |
c.await(); |
849 |
> |
sync.release(1); |
850 |
> |
}}); |
851 |
> |
|
852 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
853 |
> |
public void realRun() throws InterruptedException { |
854 |
> |
sync.acquire(1); |
855 |
> |
c.await(); |
856 |
> |
sync.release(1); |
857 |
> |
}}); |
858 |
> |
|
859 |
> |
t1.start(); |
860 |
> |
t2.start(); |
861 |
> |
Thread.sleep(SHORT_DELAY_MS); |
862 |
> |
sync.acquire(1); |
863 |
> |
c.signalAll(); |
864 |
> |
sync.release(1); |
865 |
> |
t1.join(SHORT_DELAY_MS); |
866 |
> |
t2.join(SHORT_DELAY_MS); |
867 |
> |
assertFalse(t1.isAlive()); |
868 |
> |
assertFalse(t2.isAlive()); |
869 |
|
} |
870 |
|
|
871 |
|
|
884 |
|
/** |
885 |
|
* A serialized AQS deserializes with current state |
886 |
|
*/ |
887 |
< |
public void testSerialization() { |
887 |
> |
public void testSerialization() throws Exception { |
888 |
|
Mutex l = new Mutex(); |
889 |
|
l.acquire(1); |
890 |
|
assertTrue(l.isHeldExclusively()); |
891 |
|
|
892 |
< |
try { |
893 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
894 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
895 |
< |
out.writeObject(l); |
896 |
< |
out.close(); |
897 |
< |
|
898 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
899 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
900 |
< |
Mutex r = (Mutex) in.readObject(); |
1136 |
< |
assertTrue(r.isHeldExclusively()); |
1137 |
< |
} catch (Exception e) { |
1138 |
< |
e.printStackTrace(); |
1139 |
< |
unexpectedException(); |
1140 |
< |
} |
892 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
893 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
894 |
> |
out.writeObject(l); |
895 |
> |
out.close(); |
896 |
> |
|
897 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
898 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
899 |
> |
Mutex r = (Mutex) in.readObject(); |
900 |
> |
assertTrue(r.isHeldExclusively()); |
901 |
|
} |
902 |
|
|
903 |
|
|
905 |
|
* tryReleaseShared setting state changes getState |
906 |
|
*/ |
907 |
|
public void testGetStateWithReleaseShared() { |
908 |
< |
final BooleanLatch l = new BooleanLatch(); |
909 |
< |
assertFalse(l.isSignalled()); |
910 |
< |
l.releaseShared(0); |
911 |
< |
assertTrue(l.isSignalled()); |
908 |
> |
final BooleanLatch l = new BooleanLatch(); |
909 |
> |
assertFalse(l.isSignalled()); |
910 |
> |
l.releaseShared(0); |
911 |
> |
assertTrue(l.isSignalled()); |
912 |
|
} |
913 |
|
|
914 |
|
/** |
915 |
|
* releaseShared has no effect when already signalled |
916 |
|
*/ |
917 |
|
public void testReleaseShared() { |
918 |
< |
final BooleanLatch l = new BooleanLatch(); |
919 |
< |
assertFalse(l.isSignalled()); |
920 |
< |
l.releaseShared(0); |
921 |
< |
assertTrue(l.isSignalled()); |
922 |
< |
l.releaseShared(0); |
923 |
< |
assertTrue(l.isSignalled()); |
918 |
> |
final BooleanLatch l = new BooleanLatch(); |
919 |
> |
assertFalse(l.isSignalled()); |
920 |
> |
l.releaseShared(0); |
921 |
> |
assertTrue(l.isSignalled()); |
922 |
> |
l.releaseShared(0); |
923 |
> |
assertTrue(l.isSignalled()); |
924 |
|
} |
925 |
|
|
926 |
|
/** |
927 |
|
* acquireSharedInterruptibly returns after release, but not before |
928 |
|
*/ |
929 |
< |
public void testAcquireSharedInterruptibly() { |
930 |
< |
final BooleanLatch l = new BooleanLatch(); |
929 |
> |
public void testAcquireSharedInterruptibly() throws InterruptedException { |
930 |
> |
final BooleanLatch l = new BooleanLatch(); |
931 |
|
|
932 |
< |
Thread t = new Thread(new Runnable() { |
933 |
< |
public void run() { |
934 |
< |
try { |
935 |
< |
threadAssertFalse(l.isSignalled()); |
936 |
< |
l.acquireSharedInterruptibly(0); |
937 |
< |
threadAssertTrue(l.isSignalled()); |
938 |
< |
} catch (InterruptedException e) { |
939 |
< |
threadUnexpectedException(); |
940 |
< |
} |
941 |
< |
} |
942 |
< |
}); |
943 |
< |
try { |
944 |
< |
t.start(); |
1185 |
< |
assertFalse(l.isSignalled()); |
1186 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1187 |
< |
l.releaseShared(0); |
1188 |
< |
assertTrue(l.isSignalled()); |
1189 |
< |
t.join(); |
1190 |
< |
} catch (InterruptedException e) { |
1191 |
< |
unexpectedException(); |
1192 |
< |
} |
932 |
> |
Thread t = new Thread(new CheckedRunnable() { |
933 |
> |
public void realRun() throws InterruptedException { |
934 |
> |
threadAssertFalse(l.isSignalled()); |
935 |
> |
l.acquireSharedInterruptibly(0); |
936 |
> |
threadAssertTrue(l.isSignalled()); |
937 |
> |
}}); |
938 |
> |
|
939 |
> |
t.start(); |
940 |
> |
assertFalse(l.isSignalled()); |
941 |
> |
Thread.sleep(SHORT_DELAY_MS); |
942 |
> |
l.releaseShared(0); |
943 |
> |
assertTrue(l.isSignalled()); |
944 |
> |
t.join(); |
945 |
|
} |
946 |
|
|
947 |
|
|
948 |
|
/** |
949 |
|
* acquireSharedTimed returns after release |
950 |
|
*/ |
951 |
< |
public void testAsquireSharedTimed() { |
952 |
< |
final BooleanLatch l = new BooleanLatch(); |
951 |
> |
public void testAsquireSharedTimed() throws InterruptedException { |
952 |
> |
final BooleanLatch l = new BooleanLatch(); |
953 |
|
|
954 |
< |
Thread t = new Thread(new Runnable() { |
955 |
< |
public void run() { |
956 |
< |
try { |
957 |
< |
threadAssertFalse(l.isSignalled()); |
958 |
< |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
959 |
< |
threadAssertTrue(l.isSignalled()); |
960 |
< |
|
961 |
< |
} catch (InterruptedException e) { |
962 |
< |
threadUnexpectedException(); |
963 |
< |
} |
964 |
< |
} |
965 |
< |
}); |
966 |
< |
try { |
1215 |
< |
t.start(); |
1216 |
< |
assertFalse(l.isSignalled()); |
1217 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1218 |
< |
l.releaseShared(0); |
1219 |
< |
assertTrue(l.isSignalled()); |
1220 |
< |
t.join(); |
1221 |
< |
} catch (InterruptedException e) { |
1222 |
< |
unexpectedException(); |
1223 |
< |
} |
954 |
> |
Thread t = new Thread(new CheckedRunnable() { |
955 |
> |
public void realRun() throws InterruptedException { |
956 |
> |
threadAssertFalse(l.isSignalled()); |
957 |
> |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
958 |
> |
threadAssertTrue(l.isSignalled()); |
959 |
> |
}}); |
960 |
> |
|
961 |
> |
t.start(); |
962 |
> |
assertFalse(l.isSignalled()); |
963 |
> |
Thread.sleep(SHORT_DELAY_MS); |
964 |
> |
l.releaseShared(0); |
965 |
> |
assertTrue(l.isSignalled()); |
966 |
> |
t.join(); |
967 |
|
} |
968 |
|
|
969 |
|
/** |
970 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
971 |
|
*/ |
972 |
< |
public void testAcquireSharedInterruptibly_InterruptedException() { |
972 |
> |
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
973 |
|
final BooleanLatch l = new BooleanLatch(); |
974 |
< |
Thread t = new Thread(new Runnable() { |
975 |
< |
public void run() { |
976 |
< |
try { |
977 |
< |
threadAssertFalse(l.isSignalled()); |
978 |
< |
l.acquireSharedInterruptibly(0); |
979 |
< |
threadShouldThrow(); |
980 |
< |
} catch (InterruptedException success) {} |
981 |
< |
} |
982 |
< |
}); |
983 |
< |
t.start(); |
1241 |
< |
try { |
1242 |
< |
assertFalse(l.isSignalled()); |
1243 |
< |
t.interrupt(); |
1244 |
< |
t.join(); |
1245 |
< |
} catch (InterruptedException e) { |
1246 |
< |
unexpectedException(); |
1247 |
< |
} |
974 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
975 |
> |
public void realRun() throws InterruptedException { |
976 |
> |
threadAssertFalse(l.isSignalled()); |
977 |
> |
l.acquireSharedInterruptibly(0); |
978 |
> |
}}); |
979 |
> |
|
980 |
> |
t.start(); |
981 |
> |
assertFalse(l.isSignalled()); |
982 |
> |
t.interrupt(); |
983 |
> |
t.join(); |
984 |
|
} |
985 |
|
|
986 |
|
/** |
987 |
|
* acquireSharedTimed throws IE if interrupted before released |
988 |
|
*/ |
989 |
< |
public void testAcquireSharedNanos_InterruptedException() { |
989 |
> |
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
990 |
|
final BooleanLatch l = new BooleanLatch(); |
991 |
< |
Thread t = new Thread(new Runnable() { |
992 |
< |
public void run() { |
993 |
< |
try { |
994 |
< |
threadAssertFalse(l.isSignalled()); |
995 |
< |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
996 |
< |
threadShouldThrow(); |
1261 |
< |
} catch (InterruptedException success) {} |
1262 |
< |
} |
1263 |
< |
}); |
991 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
992 |
> |
public void realRun() throws InterruptedException { |
993 |
> |
threadAssertFalse(l.isSignalled()); |
994 |
> |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
995 |
> |
}}); |
996 |
> |
|
997 |
|
t.start(); |
998 |
< |
try { |
999 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1000 |
< |
assertFalse(l.isSignalled()); |
1001 |
< |
t.interrupt(); |
1269 |
< |
t.join(); |
1270 |
< |
} catch (InterruptedException e) { |
1271 |
< |
unexpectedException(); |
1272 |
< |
} |
998 |
> |
Thread.sleep(SHORT_DELAY_MS); |
999 |
> |
assertFalse(l.isSignalled()); |
1000 |
> |
t.interrupt(); |
1001 |
> |
t.join(); |
1002 |
|
} |
1003 |
|
|
1004 |
|
/** |
1005 |
|
* acquireSharedTimed times out if not released before timeout |
1006 |
|
*/ |
1007 |
< |
public void testAcquireSharedNanos_Timeout() { |
1007 |
> |
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1008 |
|
final BooleanLatch l = new BooleanLatch(); |
1009 |
< |
Thread t = new Thread(new Runnable() { |
1010 |
< |
public void run() { |
1011 |
< |
try { |
1012 |
< |
threadAssertFalse(l.isSignalled()); |
1013 |
< |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1014 |
< |
} catch (InterruptedException ie) { |
1286 |
< |
threadUnexpectedException(); |
1287 |
< |
} |
1288 |
< |
} |
1289 |
< |
}); |
1009 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1010 |
> |
public void realRun() throws InterruptedException { |
1011 |
> |
threadAssertFalse(l.isSignalled()); |
1012 |
> |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1013 |
> |
}}); |
1014 |
> |
|
1015 |
|
t.start(); |
1016 |
< |
try { |
1017 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1018 |
< |
assertFalse(l.isSignalled()); |
1294 |
< |
t.join(); |
1295 |
< |
} catch (InterruptedException e) { |
1296 |
< |
unexpectedException(); |
1297 |
< |
} |
1016 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1017 |
> |
assertFalse(l.isSignalled()); |
1018 |
> |
t.join(); |
1019 |
|
} |
1020 |
|
|
1300 |
– |
|
1021 |
|
} |