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