1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
jsr166 |
1.5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
|
10 |
|
|
import junit.framework.*; |
11 |
|
|
import java.util.*; |
12 |
|
|
import java.util.concurrent.*; |
13 |
jsr166 |
1.12 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
dl |
1.1 |
import java.util.concurrent.locks.*; |
15 |
|
|
import java.io.*; |
16 |
|
|
|
17 |
|
|
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
18 |
|
|
public static void main(String[] args) { |
19 |
jsr166 |
1.16 |
junit.textui.TestRunner.run(suite()); |
20 |
dl |
1.1 |
} |
21 |
|
|
public static Test suite() { |
22 |
|
|
return new TestSuite(AbstractQueuedLongSynchronizerTest.class); |
23 |
|
|
} |
24 |
|
|
|
25 |
|
|
/** |
26 |
|
|
* A simple mutex class, adapted from the |
27 |
|
|
* AbstractQueuedLongSynchronizer javadoc. Exclusive acquire tests |
28 |
|
|
* exercise this as a sample user extension. Other |
29 |
|
|
* methods/features of AbstractQueuedLongSynchronizerTest are tested |
30 |
|
|
* via other test classes, including those for ReentrantLock, |
31 |
|
|
* ReentrantReadWriteLock, and Semaphore |
32 |
|
|
*/ |
33 |
|
|
static class Mutex extends AbstractQueuedLongSynchronizer { |
34 |
|
|
// Use value > 32 bits for locked state |
35 |
|
|
static final long LOCKED = 1 << 48; |
36 |
jsr166 |
1.5 |
public boolean isHeldExclusively() { |
37 |
|
|
return getState() == LOCKED; |
38 |
dl |
1.1 |
} |
39 |
jsr166 |
1.5 |
|
40 |
dl |
1.1 |
public boolean tryAcquire(long acquires) { |
41 |
|
|
return compareAndSetState(0, LOCKED); |
42 |
|
|
} |
43 |
jsr166 |
1.5 |
|
44 |
dl |
1.1 |
public boolean tryRelease(long releases) { |
45 |
|
|
if (getState() == 0) throw new IllegalMonitorStateException(); |
46 |
|
|
setState(0); |
47 |
|
|
return true; |
48 |
|
|
} |
49 |
jsr166 |
1.5 |
|
50 |
jsr166 |
1.17 |
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { |
51 |
|
|
return new AbstractQueuedLongSynchronizer.ConditionObject(); |
52 |
|
|
} |
53 |
dl |
1.1 |
|
54 |
|
|
} |
55 |
|
|
|
56 |
jsr166 |
1.5 |
|
57 |
dl |
1.1 |
/** |
58 |
|
|
* A simple latch class, to test shared mode. |
59 |
|
|
*/ |
60 |
jsr166 |
1.5 |
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
61 |
dl |
1.1 |
public boolean isSignalled() { return getState() != 0; } |
62 |
|
|
|
63 |
|
|
public long tryAcquireShared(long ignore) { |
64 |
|
|
return isSignalled()? 1 : -1; |
65 |
|
|
} |
66 |
jsr166 |
1.5 |
|
67 |
dl |
1.1 |
public boolean tryReleaseShared(long ignore) { |
68 |
|
|
setState(1 << 62); |
69 |
|
|
return true; |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
/** |
74 |
jsr166 |
1.10 |
* A runnable calling acquireInterruptibly that does not expect to |
75 |
|
|
* be interrupted. |
76 |
dl |
1.1 |
*/ |
77 |
jsr166 |
1.10 |
class InterruptibleSyncRunnable extends CheckedRunnable { |
78 |
dl |
1.1 |
final Mutex sync; |
79 |
|
|
InterruptibleSyncRunnable(Mutex l) { sync = l; } |
80 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
81 |
|
|
sync.acquireInterruptibly(1); |
82 |
dl |
1.1 |
} |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
/** |
87 |
|
|
* A runnable calling acquireInterruptibly that expects to be |
88 |
jsr166 |
1.10 |
* interrupted. |
89 |
dl |
1.1 |
*/ |
90 |
jsr166 |
1.10 |
class InterruptedSyncRunnable extends CheckedInterruptedRunnable { |
91 |
dl |
1.1 |
final Mutex sync; |
92 |
|
|
InterruptedSyncRunnable(Mutex l) { sync = l; } |
93 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
94 |
|
|
sync.acquireInterruptibly(1); |
95 |
dl |
1.1 |
} |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
/** |
99 |
|
|
* isHeldExclusively is false upon construction |
100 |
|
|
*/ |
101 |
jsr166 |
1.5 |
public void testIsHeldExclusively() { |
102 |
jsr166 |
1.11 |
Mutex rl = new Mutex(); |
103 |
dl |
1.1 |
assertFalse(rl.isHeldExclusively()); |
104 |
|
|
} |
105 |
jsr166 |
1.5 |
|
106 |
dl |
1.1 |
/** |
107 |
|
|
* acquiring released sync succeeds |
108 |
|
|
*/ |
109 |
jsr166 |
1.5 |
public void testAcquire() { |
110 |
jsr166 |
1.11 |
Mutex rl = new Mutex(); |
111 |
dl |
1.1 |
rl.acquire(1); |
112 |
|
|
assertTrue(rl.isHeldExclusively()); |
113 |
|
|
rl.release(1); |
114 |
|
|
assertFalse(rl.isHeldExclusively()); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
/** |
118 |
|
|
* tryAcquire on an released sync succeeds |
119 |
|
|
*/ |
120 |
jsr166 |
1.5 |
public void testTryAcquire() { |
121 |
jsr166 |
1.11 |
Mutex rl = new Mutex(); |
122 |
dl |
1.1 |
assertTrue(rl.tryAcquire(1)); |
123 |
|
|
assertTrue(rl.isHeldExclusively()); |
124 |
|
|
rl.release(1); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
/** |
128 |
|
|
* hasQueuedThreads reports whether there are waiting threads |
129 |
|
|
*/ |
130 |
jsr166 |
1.9 |
public void testhasQueuedThreads() throws InterruptedException { |
131 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
132 |
dl |
1.1 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
133 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
134 |
jsr166 |
1.9 |
assertFalse(sync.hasQueuedThreads()); |
135 |
|
|
sync.acquire(1); |
136 |
|
|
t1.start(); |
137 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
138 |
|
|
assertTrue(sync.hasQueuedThreads()); |
139 |
|
|
t2.start(); |
140 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
141 |
|
|
assertTrue(sync.hasQueuedThreads()); |
142 |
|
|
t1.interrupt(); |
143 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
144 |
|
|
assertTrue(sync.hasQueuedThreads()); |
145 |
|
|
sync.release(1); |
146 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
147 |
|
|
assertFalse(sync.hasQueuedThreads()); |
148 |
|
|
t1.join(); |
149 |
|
|
t2.join(); |
150 |
jsr166 |
1.5 |
} |
151 |
dl |
1.1 |
|
152 |
|
|
/** |
153 |
|
|
* isQueued(null) throws NPE |
154 |
|
|
*/ |
155 |
jsr166 |
1.5 |
public void testIsQueuedNPE() { |
156 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
157 |
dl |
1.1 |
try { |
158 |
|
|
sync.isQueued(null); |
159 |
|
|
shouldThrow(); |
160 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
161 |
dl |
1.1 |
} |
162 |
|
|
|
163 |
|
|
/** |
164 |
|
|
* isQueued reports whether a thread is queued. |
165 |
|
|
*/ |
166 |
jsr166 |
1.9 |
public void testIsQueued() throws InterruptedException { |
167 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
168 |
dl |
1.1 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
169 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
170 |
jsr166 |
1.9 |
assertFalse(sync.isQueued(t1)); |
171 |
|
|
assertFalse(sync.isQueued(t2)); |
172 |
|
|
sync.acquire(1); |
173 |
|
|
t1.start(); |
174 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
175 |
|
|
assertTrue(sync.isQueued(t1)); |
176 |
|
|
t2.start(); |
177 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
178 |
|
|
assertTrue(sync.isQueued(t1)); |
179 |
|
|
assertTrue(sync.isQueued(t2)); |
180 |
|
|
t1.interrupt(); |
181 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
182 |
|
|
assertFalse(sync.isQueued(t1)); |
183 |
|
|
assertTrue(sync.isQueued(t2)); |
184 |
|
|
sync.release(1); |
185 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
186 |
|
|
assertFalse(sync.isQueued(t1)); |
187 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
188 |
|
|
assertFalse(sync.isQueued(t2)); |
189 |
|
|
t1.join(); |
190 |
|
|
t2.join(); |
191 |
jsr166 |
1.5 |
} |
192 |
dl |
1.1 |
|
193 |
|
|
/** |
194 |
|
|
* getFirstQueuedThread returns first waiting thread or null if none |
195 |
|
|
*/ |
196 |
jsr166 |
1.9 |
public void testGetFirstQueuedThread() throws InterruptedException { |
197 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
198 |
dl |
1.1 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
199 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
200 |
jsr166 |
1.9 |
assertNull(sync.getFirstQueuedThread()); |
201 |
|
|
sync.acquire(1); |
202 |
|
|
t1.start(); |
203 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
204 |
|
|
assertEquals(t1, sync.getFirstQueuedThread()); |
205 |
|
|
t2.start(); |
206 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
207 |
|
|
assertEquals(t1, sync.getFirstQueuedThread()); |
208 |
|
|
t1.interrupt(); |
209 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
210 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
211 |
|
|
assertEquals(t2, sync.getFirstQueuedThread()); |
212 |
|
|
sync.release(1); |
213 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
214 |
|
|
assertNull(sync.getFirstQueuedThread()); |
215 |
|
|
t1.join(); |
216 |
|
|
t2.join(); |
217 |
jsr166 |
1.5 |
} |
218 |
dl |
1.1 |
|
219 |
|
|
|
220 |
|
|
/** |
221 |
|
|
* hasContended reports false if no thread has ever blocked, else true |
222 |
|
|
*/ |
223 |
jsr166 |
1.9 |
public void testHasContended() throws InterruptedException { |
224 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
225 |
dl |
1.1 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
226 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
227 |
jsr166 |
1.9 |
assertFalse(sync.hasContended()); |
228 |
|
|
sync.acquire(1); |
229 |
|
|
t1.start(); |
230 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
231 |
|
|
assertTrue(sync.hasContended()); |
232 |
|
|
t2.start(); |
233 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
234 |
|
|
assertTrue(sync.hasContended()); |
235 |
|
|
t1.interrupt(); |
236 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
237 |
|
|
assertTrue(sync.hasContended()); |
238 |
|
|
sync.release(1); |
239 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
240 |
|
|
assertTrue(sync.hasContended()); |
241 |
|
|
t1.join(); |
242 |
|
|
t2.join(); |
243 |
jsr166 |
1.5 |
} |
244 |
dl |
1.1 |
|
245 |
|
|
/** |
246 |
|
|
* getQueuedThreads includes waiting threads |
247 |
|
|
*/ |
248 |
jsr166 |
1.9 |
public void testGetQueuedThreads() throws InterruptedException { |
249 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
250 |
dl |
1.1 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
251 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
252 |
jsr166 |
1.9 |
assertTrue(sync.getQueuedThreads().isEmpty()); |
253 |
|
|
sync.acquire(1); |
254 |
|
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
255 |
|
|
t1.start(); |
256 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
257 |
|
|
assertTrue(sync.getQueuedThreads().contains(t1)); |
258 |
|
|
t2.start(); |
259 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
260 |
|
|
assertTrue(sync.getQueuedThreads().contains(t1)); |
261 |
|
|
assertTrue(sync.getQueuedThreads().contains(t2)); |
262 |
|
|
t1.interrupt(); |
263 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
264 |
|
|
assertFalse(sync.getQueuedThreads().contains(t1)); |
265 |
|
|
assertTrue(sync.getQueuedThreads().contains(t2)); |
266 |
|
|
sync.release(1); |
267 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
268 |
|
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
269 |
|
|
t1.join(); |
270 |
|
|
t2.join(); |
271 |
jsr166 |
1.5 |
} |
272 |
dl |
1.1 |
|
273 |
|
|
/** |
274 |
|
|
* getExclusiveQueuedThreads includes waiting threads |
275 |
|
|
*/ |
276 |
jsr166 |
1.9 |
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
277 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
278 |
dl |
1.1 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
279 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
280 |
jsr166 |
1.9 |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
281 |
|
|
sync.acquire(1); |
282 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
283 |
|
|
t1.start(); |
284 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
285 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
286 |
|
|
t2.start(); |
287 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
288 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
289 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
290 |
|
|
t1.interrupt(); |
291 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
292 |
|
|
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
293 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
294 |
|
|
sync.release(1); |
295 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
296 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
297 |
|
|
t1.join(); |
298 |
|
|
t2.join(); |
299 |
jsr166 |
1.5 |
} |
300 |
dl |
1.1 |
|
301 |
|
|
/** |
302 |
|
|
* getSharedQueuedThreads does not include exclusively waiting threads |
303 |
|
|
*/ |
304 |
jsr166 |
1.9 |
public void testGetSharedQueuedThreads() throws InterruptedException { |
305 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
306 |
dl |
1.1 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
307 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
308 |
jsr166 |
1.9 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
309 |
|
|
sync.acquire(1); |
310 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
311 |
|
|
t1.start(); |
312 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
313 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
314 |
|
|
t2.start(); |
315 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
316 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
317 |
|
|
t1.interrupt(); |
318 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
319 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
320 |
|
|
sync.release(1); |
321 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
322 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
323 |
|
|
t1.join(); |
324 |
|
|
t2.join(); |
325 |
jsr166 |
1.5 |
} |
326 |
dl |
1.1 |
|
327 |
|
|
/** |
328 |
|
|
* tryAcquireNanos is interruptible. |
329 |
|
|
*/ |
330 |
jsr166 |
1.9 |
public void testInterruptedException2() throws InterruptedException { |
331 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
332 |
|
|
sync.acquire(1); |
333 |
|
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
334 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
335 |
jsr166 |
1.15 |
sync.tryAcquireNanos(1, MILLISECONDS.toNanos(MEDIUM_DELAY_MS)); |
336 |
jsr166 |
1.10 |
}}); |
337 |
jsr166 |
1.9 |
|
338 |
|
|
t.start(); |
339 |
jsr166 |
1.14 |
Thread.sleep(SHORT_DELAY_MS); |
340 |
jsr166 |
1.9 |
t.interrupt(); |
341 |
|
|
t.join(); |
342 |
dl |
1.1 |
} |
343 |
|
|
|
344 |
|
|
|
345 |
|
|
/** |
346 |
|
|
* TryAcquire on exclusively held sync fails |
347 |
|
|
*/ |
348 |
jsr166 |
1.9 |
public void testTryAcquireWhenSynced() throws InterruptedException { |
349 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
350 |
|
|
sync.acquire(1); |
351 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
352 |
jsr166 |
1.10 |
public void realRun() { |
353 |
jsr166 |
1.17 |
assertFalse(sync.tryAcquire(1)); |
354 |
jsr166 |
1.10 |
}}); |
355 |
jsr166 |
1.9 |
|
356 |
|
|
t.start(); |
357 |
|
|
t.join(); |
358 |
|
|
sync.release(1); |
359 |
jsr166 |
1.5 |
} |
360 |
dl |
1.1 |
|
361 |
|
|
/** |
362 |
|
|
* tryAcquireNanos on an exclusively held sync times out |
363 |
|
|
*/ |
364 |
jsr166 |
1.9 |
public void testAcquireNanos_Timeout() throws InterruptedException { |
365 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
366 |
|
|
sync.acquire(1); |
367 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
368 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
369 |
jsr166 |
1.15 |
long nanos = MILLISECONDS.toNanos(SHORT_DELAY_MS); |
370 |
|
|
assertFalse(sync.tryAcquireNanos(1, nanos)); |
371 |
jsr166 |
1.10 |
}}); |
372 |
jsr166 |
1.9 |
|
373 |
|
|
t.start(); |
374 |
|
|
t.join(); |
375 |
|
|
sync.release(1); |
376 |
jsr166 |
1.5 |
} |
377 |
|
|
|
378 |
|
|
|
379 |
dl |
1.1 |
/** |
380 |
|
|
* getState is true when acquired and false when not |
381 |
|
|
*/ |
382 |
jsr166 |
1.9 |
public void testGetState() throws InterruptedException { |
383 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
384 |
|
|
sync.acquire(1); |
385 |
|
|
assertTrue(sync.isHeldExclusively()); |
386 |
|
|
sync.release(1); |
387 |
|
|
assertFalse(sync.isHeldExclusively()); |
388 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
389 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
390 |
|
|
sync.acquire(1); |
391 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
392 |
|
|
sync.release(1); |
393 |
|
|
}}); |
394 |
jsr166 |
1.9 |
|
395 |
|
|
t.start(); |
396 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
397 |
|
|
assertTrue(sync.isHeldExclusively()); |
398 |
|
|
t.join(); |
399 |
|
|
assertFalse(sync.isHeldExclusively()); |
400 |
dl |
1.1 |
} |
401 |
|
|
|
402 |
|
|
|
403 |
|
|
/** |
404 |
|
|
* acquireInterruptibly is interruptible. |
405 |
|
|
*/ |
406 |
jsr166 |
1.9 |
public void testAcquireInterruptibly1() throws InterruptedException { |
407 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
408 |
|
|
sync.acquire(1); |
409 |
|
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
410 |
jsr166 |
1.9 |
t.start(); |
411 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
412 |
|
|
t.interrupt(); |
413 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
414 |
|
|
sync.release(1); |
415 |
|
|
t.join(); |
416 |
jsr166 |
1.5 |
} |
417 |
dl |
1.1 |
|
418 |
|
|
/** |
419 |
|
|
* acquireInterruptibly succeeds when released, else is interruptible |
420 |
|
|
*/ |
421 |
jsr166 |
1.9 |
public void testAcquireInterruptibly2() throws InterruptedException { |
422 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
423 |
jsr166 |
1.9 |
sync.acquireInterruptibly(1); |
424 |
jsr166 |
1.11 |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
425 |
jsr166 |
1.9 |
t.start(); |
426 |
jsr166 |
1.14 |
Thread.sleep(SHORT_DELAY_MS); |
427 |
jsr166 |
1.9 |
t.interrupt(); |
428 |
|
|
assertTrue(sync.isHeldExclusively()); |
429 |
|
|
t.join(); |
430 |
dl |
1.1 |
} |
431 |
|
|
|
432 |
|
|
/** |
433 |
|
|
* owns is true for a condition created by sync else false |
434 |
|
|
*/ |
435 |
|
|
public void testOwns() { |
436 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
437 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
438 |
|
|
final Mutex sync2 = new Mutex(); |
439 |
|
|
assertTrue(sync.owns(c)); |
440 |
|
|
assertFalse(sync2.owns(c)); |
441 |
|
|
} |
442 |
|
|
|
443 |
|
|
/** |
444 |
|
|
* Calling await without holding sync throws IllegalMonitorStateException |
445 |
|
|
*/ |
446 |
jsr166 |
1.9 |
public void testAwait_IllegalMonitor() throws InterruptedException { |
447 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
448 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
449 |
|
|
try { |
450 |
|
|
c.await(); |
451 |
|
|
shouldThrow(); |
452 |
jsr166 |
1.10 |
} catch (IllegalMonitorStateException success) {} |
453 |
dl |
1.1 |
} |
454 |
|
|
|
455 |
|
|
/** |
456 |
|
|
* Calling signal without holding sync throws IllegalMonitorStateException |
457 |
|
|
*/ |
458 |
jsr166 |
1.9 |
public void testSignal_IllegalMonitor() throws InterruptedException { |
459 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
460 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
461 |
|
|
try { |
462 |
|
|
c.signal(); |
463 |
|
|
shouldThrow(); |
464 |
jsr166 |
1.10 |
} catch (IllegalMonitorStateException success) {} |
465 |
dl |
1.1 |
} |
466 |
|
|
|
467 |
|
|
/** |
468 |
|
|
* awaitNanos without a signal times out |
469 |
|
|
*/ |
470 |
jsr166 |
1.9 |
public void testAwaitNanos_Timeout() throws InterruptedException { |
471 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
472 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
473 |
jsr166 |
1.9 |
sync.acquire(1); |
474 |
|
|
long t = c.awaitNanos(100); |
475 |
|
|
assertTrue(t <= 0); |
476 |
|
|
sync.release(1); |
477 |
dl |
1.1 |
} |
478 |
|
|
|
479 |
|
|
/** |
480 |
jsr166 |
1.18 |
* Timed await without a signal times out |
481 |
dl |
1.1 |
*/ |
482 |
jsr166 |
1.9 |
public void testAwait_Timeout() throws InterruptedException { |
483 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
484 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
485 |
jsr166 |
1.9 |
sync.acquire(1); |
486 |
jsr166 |
1.12 |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
487 |
jsr166 |
1.9 |
sync.release(1); |
488 |
dl |
1.1 |
} |
489 |
|
|
|
490 |
|
|
/** |
491 |
|
|
* awaitUntil without a signal times out |
492 |
|
|
*/ |
493 |
jsr166 |
1.9 |
public void testAwaitUntil_Timeout() throws InterruptedException { |
494 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
495 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
496 |
jsr166 |
1.9 |
sync.acquire(1); |
497 |
|
|
java.util.Date d = new java.util.Date(); |
498 |
|
|
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
499 |
|
|
sync.release(1); |
500 |
dl |
1.1 |
} |
501 |
|
|
|
502 |
|
|
/** |
503 |
|
|
* await returns when signalled |
504 |
|
|
*/ |
505 |
jsr166 |
1.9 |
public void testAwait() throws InterruptedException { |
506 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
507 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
508 |
jsr166 |
1.11 |
Thread t = new Thread(new CheckedRunnable() { |
509 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
510 |
|
|
sync.acquire(1); |
511 |
|
|
c.await(); |
512 |
|
|
sync.release(1); |
513 |
|
|
}}); |
514 |
dl |
1.1 |
|
515 |
jsr166 |
1.9 |
t.start(); |
516 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
517 |
|
|
sync.acquire(1); |
518 |
|
|
c.signal(); |
519 |
|
|
sync.release(1); |
520 |
|
|
t.join(SHORT_DELAY_MS); |
521 |
|
|
assertFalse(t.isAlive()); |
522 |
dl |
1.1 |
} |
523 |
|
|
|
524 |
|
|
|
525 |
|
|
|
526 |
|
|
/** |
527 |
|
|
* hasWaiters throws NPE if null |
528 |
|
|
*/ |
529 |
|
|
public void testHasWaitersNPE() { |
530 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
531 |
dl |
1.1 |
try { |
532 |
|
|
sync.hasWaiters(null); |
533 |
|
|
shouldThrow(); |
534 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
535 |
dl |
1.1 |
} |
536 |
|
|
|
537 |
|
|
/** |
538 |
|
|
* getWaitQueueLength throws NPE if null |
539 |
|
|
*/ |
540 |
|
|
public void testGetWaitQueueLengthNPE() { |
541 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
542 |
dl |
1.1 |
try { |
543 |
|
|
sync.getWaitQueueLength(null); |
544 |
|
|
shouldThrow(); |
545 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
546 |
dl |
1.1 |
} |
547 |
|
|
|
548 |
|
|
|
549 |
|
|
/** |
550 |
|
|
* getWaitingThreads throws NPE if null |
551 |
|
|
*/ |
552 |
|
|
public void testGetWaitingThreadsNPE() { |
553 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
554 |
dl |
1.1 |
try { |
555 |
|
|
sync.getWaitingThreads(null); |
556 |
|
|
shouldThrow(); |
557 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
558 |
dl |
1.1 |
} |
559 |
|
|
|
560 |
|
|
|
561 |
|
|
/** |
562 |
|
|
* hasWaiters throws IAE if not owned |
563 |
|
|
*/ |
564 |
|
|
public void testHasWaitersIAE() { |
565 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
566 |
jsr166 |
1.8 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
567 |
jsr166 |
1.11 |
final Mutex sync2 = new Mutex(); |
568 |
dl |
1.1 |
try { |
569 |
|
|
sync2.hasWaiters(c); |
570 |
|
|
shouldThrow(); |
571 |
jsr166 |
1.9 |
} catch (IllegalArgumentException success) {} |
572 |
dl |
1.1 |
} |
573 |
|
|
|
574 |
|
|
/** |
575 |
|
|
* hasWaiters throws IMSE if not synced |
576 |
|
|
*/ |
577 |
|
|
public void testHasWaitersIMSE() { |
578 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
579 |
jsr166 |
1.8 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
580 |
dl |
1.1 |
try { |
581 |
|
|
sync.hasWaiters(c); |
582 |
|
|
shouldThrow(); |
583 |
jsr166 |
1.9 |
} catch (IllegalMonitorStateException success) {} |
584 |
dl |
1.1 |
} |
585 |
|
|
|
586 |
|
|
|
587 |
|
|
/** |
588 |
|
|
* getWaitQueueLength throws IAE if not owned |
589 |
|
|
*/ |
590 |
|
|
public void testGetWaitQueueLengthIAE() { |
591 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
592 |
jsr166 |
1.8 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
593 |
jsr166 |
1.11 |
final Mutex sync2 = new Mutex(); |
594 |
dl |
1.1 |
try { |
595 |
|
|
sync2.getWaitQueueLength(c); |
596 |
|
|
shouldThrow(); |
597 |
jsr166 |
1.9 |
} catch (IllegalArgumentException success) {} |
598 |
dl |
1.1 |
} |
599 |
|
|
|
600 |
|
|
/** |
601 |
|
|
* getWaitQueueLength throws IMSE if not synced |
602 |
|
|
*/ |
603 |
|
|
public void testGetWaitQueueLengthIMSE() { |
604 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
605 |
jsr166 |
1.8 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
606 |
dl |
1.1 |
try { |
607 |
|
|
sync.getWaitQueueLength(c); |
608 |
|
|
shouldThrow(); |
609 |
jsr166 |
1.9 |
} catch (IllegalMonitorStateException success) {} |
610 |
dl |
1.1 |
} |
611 |
|
|
|
612 |
|
|
|
613 |
|
|
/** |
614 |
|
|
* getWaitingThreads throws IAE if not owned |
615 |
|
|
*/ |
616 |
|
|
public void testGetWaitingThreadsIAE() { |
617 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
618 |
jsr166 |
1.8 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
619 |
jsr166 |
1.11 |
final Mutex sync2 = new Mutex(); |
620 |
dl |
1.1 |
try { |
621 |
|
|
sync2.getWaitingThreads(c); |
622 |
|
|
shouldThrow(); |
623 |
jsr166 |
1.9 |
} catch (IllegalArgumentException success) {} |
624 |
dl |
1.1 |
} |
625 |
|
|
|
626 |
|
|
/** |
627 |
|
|
* getWaitingThreads throws IMSE if not synced |
628 |
|
|
*/ |
629 |
|
|
public void testGetWaitingThreadsIMSE() { |
630 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
631 |
jsr166 |
1.8 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
632 |
dl |
1.1 |
try { |
633 |
|
|
sync.getWaitingThreads(c); |
634 |
|
|
shouldThrow(); |
635 |
jsr166 |
1.9 |
} catch (IllegalMonitorStateException success) {} |
636 |
dl |
1.1 |
} |
637 |
|
|
|
638 |
|
|
|
639 |
|
|
|
640 |
|
|
/** |
641 |
|
|
* hasWaiters returns true when a thread is waiting, else false |
642 |
|
|
*/ |
643 |
jsr166 |
1.9 |
public void testHasWaiters() throws InterruptedException { |
644 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
645 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
646 |
jsr166 |
1.11 |
Thread t = new Thread(new CheckedRunnable() { |
647 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
648 |
|
|
sync.acquire(1); |
649 |
jsr166 |
1.17 |
assertFalse(sync.hasWaiters(c)); |
650 |
|
|
assertEquals(0, sync.getWaitQueueLength(c)); |
651 |
jsr166 |
1.10 |
c.await(); |
652 |
|
|
sync.release(1); |
653 |
|
|
}}); |
654 |
dl |
1.1 |
|
655 |
jsr166 |
1.9 |
t.start(); |
656 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
657 |
|
|
sync.acquire(1); |
658 |
|
|
assertTrue(sync.hasWaiters(c)); |
659 |
|
|
assertEquals(1, sync.getWaitQueueLength(c)); |
660 |
|
|
c.signal(); |
661 |
|
|
sync.release(1); |
662 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
663 |
|
|
sync.acquire(1); |
664 |
|
|
assertFalse(sync.hasWaiters(c)); |
665 |
|
|
assertEquals(0, sync.getWaitQueueLength(c)); |
666 |
|
|
sync.release(1); |
667 |
|
|
t.join(SHORT_DELAY_MS); |
668 |
|
|
assertFalse(t.isAlive()); |
669 |
dl |
1.1 |
} |
670 |
|
|
|
671 |
|
|
/** |
672 |
|
|
* getWaitQueueLength returns number of waiting threads |
673 |
|
|
*/ |
674 |
jsr166 |
1.9 |
public void testGetWaitQueueLength() throws InterruptedException { |
675 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
676 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
677 |
jsr166 |
1.11 |
Thread t1 = new Thread(new CheckedRunnable() { |
678 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
679 |
|
|
sync.acquire(1); |
680 |
jsr166 |
1.17 |
assertFalse(sync.hasWaiters(c)); |
681 |
|
|
assertEquals(0, sync.getWaitQueueLength(c)); |
682 |
jsr166 |
1.10 |
c.await(); |
683 |
|
|
sync.release(1); |
684 |
|
|
}}); |
685 |
|
|
|
686 |
jsr166 |
1.11 |
Thread t2 = new Thread(new CheckedRunnable() { |
687 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
688 |
|
|
sync.acquire(1); |
689 |
jsr166 |
1.17 |
assertTrue(sync.hasWaiters(c)); |
690 |
|
|
assertEquals(1, sync.getWaitQueueLength(c)); |
691 |
jsr166 |
1.10 |
c.await(); |
692 |
|
|
sync.release(1); |
693 |
|
|
}}); |
694 |
dl |
1.1 |
|
695 |
jsr166 |
1.9 |
t1.start(); |
696 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
697 |
|
|
t2.start(); |
698 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
699 |
|
|
sync.acquire(1); |
700 |
|
|
assertTrue(sync.hasWaiters(c)); |
701 |
|
|
assertEquals(2, sync.getWaitQueueLength(c)); |
702 |
|
|
c.signalAll(); |
703 |
|
|
sync.release(1); |
704 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
705 |
|
|
sync.acquire(1); |
706 |
|
|
assertFalse(sync.hasWaiters(c)); |
707 |
|
|
assertEquals(0, sync.getWaitQueueLength(c)); |
708 |
|
|
sync.release(1); |
709 |
|
|
t1.join(SHORT_DELAY_MS); |
710 |
|
|
t2.join(SHORT_DELAY_MS); |
711 |
|
|
assertFalse(t1.isAlive()); |
712 |
|
|
assertFalse(t2.isAlive()); |
713 |
dl |
1.1 |
} |
714 |
|
|
|
715 |
|
|
/** |
716 |
|
|
* getWaitingThreads returns only and all waiting threads |
717 |
|
|
*/ |
718 |
jsr166 |
1.9 |
public void testGetWaitingThreads() throws InterruptedException { |
719 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
720 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
721 |
jsr166 |
1.11 |
Thread t1 = new Thread(new CheckedRunnable() { |
722 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
723 |
|
|
sync.acquire(1); |
724 |
jsr166 |
1.17 |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
725 |
jsr166 |
1.10 |
c.await(); |
726 |
|
|
sync.release(1); |
727 |
|
|
}}); |
728 |
|
|
|
729 |
jsr166 |
1.11 |
Thread t2 = new Thread(new CheckedRunnable() { |
730 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
731 |
|
|
sync.acquire(1); |
732 |
jsr166 |
1.17 |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
733 |
jsr166 |
1.10 |
c.await(); |
734 |
|
|
sync.release(1); |
735 |
|
|
}}); |
736 |
dl |
1.1 |
|
737 |
|
|
sync.acquire(1); |
738 |
|
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
739 |
|
|
sync.release(1); |
740 |
|
|
t1.start(); |
741 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
742 |
|
|
t2.start(); |
743 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
744 |
|
|
sync.acquire(1); |
745 |
|
|
assertTrue(sync.hasWaiters(c)); |
746 |
|
|
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
747 |
|
|
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
748 |
|
|
c.signalAll(); |
749 |
|
|
sync.release(1); |
750 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
751 |
|
|
sync.acquire(1); |
752 |
|
|
assertFalse(sync.hasWaiters(c)); |
753 |
|
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
754 |
|
|
sync.release(1); |
755 |
|
|
t1.join(SHORT_DELAY_MS); |
756 |
|
|
t2.join(SHORT_DELAY_MS); |
757 |
|
|
assertFalse(t1.isAlive()); |
758 |
|
|
assertFalse(t2.isAlive()); |
759 |
|
|
} |
760 |
|
|
|
761 |
|
|
|
762 |
|
|
|
763 |
|
|
/** |
764 |
|
|
* awaitUninterruptibly doesn't abort on interrupt |
765 |
|
|
*/ |
766 |
jsr166 |
1.9 |
public void testAwaitUninterruptibly() throws InterruptedException { |
767 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
768 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
769 |
jsr166 |
1.11 |
Thread t = new Thread(new CheckedRunnable() { |
770 |
jsr166 |
1.10 |
public void realRun() { |
771 |
|
|
sync.acquire(1); |
772 |
|
|
c.awaitUninterruptibly(); |
773 |
|
|
sync.release(1); |
774 |
|
|
}}); |
775 |
dl |
1.1 |
|
776 |
jsr166 |
1.9 |
t.start(); |
777 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
778 |
|
|
t.interrupt(); |
779 |
|
|
sync.acquire(1); |
780 |
|
|
c.signal(); |
781 |
|
|
sync.release(1); |
782 |
|
|
t.join(SHORT_DELAY_MS); |
783 |
|
|
assertFalse(t.isAlive()); |
784 |
dl |
1.1 |
} |
785 |
|
|
|
786 |
|
|
/** |
787 |
|
|
* await is interruptible |
788 |
|
|
*/ |
789 |
jsr166 |
1.9 |
public void testAwait_Interrupt() throws InterruptedException { |
790 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
791 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
792 |
jsr166 |
1.11 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
793 |
|
|
public void realRun() throws InterruptedException { |
794 |
jsr166 |
1.10 |
sync.acquire(1); |
795 |
|
|
c.await(); |
796 |
|
|
}}); |
797 |
dl |
1.1 |
|
798 |
jsr166 |
1.9 |
t.start(); |
799 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
800 |
|
|
t.interrupt(); |
801 |
|
|
t.join(SHORT_DELAY_MS); |
802 |
|
|
assertFalse(t.isAlive()); |
803 |
dl |
1.1 |
} |
804 |
|
|
|
805 |
|
|
/** |
806 |
|
|
* awaitNanos is interruptible |
807 |
|
|
*/ |
808 |
jsr166 |
1.9 |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
809 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
810 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
811 |
jsr166 |
1.11 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
812 |
|
|
public void realRun() throws InterruptedException { |
813 |
jsr166 |
1.10 |
sync.acquire(1); |
814 |
jsr166 |
1.15 |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
815 |
jsr166 |
1.10 |
}}); |
816 |
dl |
1.1 |
|
817 |
jsr166 |
1.9 |
t.start(); |
818 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
819 |
|
|
t.interrupt(); |
820 |
|
|
t.join(SHORT_DELAY_MS); |
821 |
|
|
assertFalse(t.isAlive()); |
822 |
dl |
1.1 |
} |
823 |
|
|
|
824 |
|
|
/** |
825 |
|
|
* awaitUntil is interruptible |
826 |
|
|
*/ |
827 |
jsr166 |
1.9 |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
828 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
829 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
830 |
jsr166 |
1.11 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
831 |
|
|
public void realRun() throws InterruptedException { |
832 |
jsr166 |
1.10 |
sync.acquire(1); |
833 |
|
|
java.util.Date d = new java.util.Date(); |
834 |
|
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
835 |
|
|
}}); |
836 |
dl |
1.1 |
|
837 |
jsr166 |
1.9 |
t.start(); |
838 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
839 |
|
|
t.interrupt(); |
840 |
|
|
t.join(SHORT_DELAY_MS); |
841 |
|
|
assertFalse(t.isAlive()); |
842 |
dl |
1.1 |
} |
843 |
|
|
|
844 |
|
|
/** |
845 |
|
|
* signalAll wakes up all threads |
846 |
|
|
*/ |
847 |
jsr166 |
1.9 |
public void testSignalAll() throws InterruptedException { |
848 |
jsr166 |
1.11 |
final Mutex sync = new Mutex(); |
849 |
dl |
1.1 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
850 |
jsr166 |
1.11 |
Thread t1 = new Thread(new CheckedRunnable() { |
851 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
852 |
|
|
sync.acquire(1); |
853 |
|
|
c.await(); |
854 |
|
|
sync.release(1); |
855 |
|
|
}}); |
856 |
|
|
|
857 |
jsr166 |
1.11 |
Thread t2 = new Thread(new CheckedRunnable() { |
858 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
859 |
|
|
sync.acquire(1); |
860 |
|
|
c.await(); |
861 |
|
|
sync.release(1); |
862 |
|
|
}}); |
863 |
dl |
1.1 |
|
864 |
jsr166 |
1.9 |
t1.start(); |
865 |
|
|
t2.start(); |
866 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
867 |
|
|
sync.acquire(1); |
868 |
|
|
c.signalAll(); |
869 |
|
|
sync.release(1); |
870 |
|
|
t1.join(SHORT_DELAY_MS); |
871 |
|
|
t2.join(SHORT_DELAY_MS); |
872 |
|
|
assertFalse(t1.isAlive()); |
873 |
|
|
assertFalse(t2.isAlive()); |
874 |
dl |
1.1 |
} |
875 |
|
|
|
876 |
|
|
|
877 |
|
|
/** |
878 |
|
|
* toString indicates current state |
879 |
|
|
*/ |
880 |
|
|
public void testToString() { |
881 |
|
|
Mutex sync = new Mutex(); |
882 |
|
|
String us = sync.toString(); |
883 |
|
|
assertTrue(us.indexOf("State = 0") >= 0); |
884 |
|
|
sync.acquire(1); |
885 |
|
|
String ls = sync.toString(); |
886 |
|
|
assertTrue(ls.indexOf("State = " + Mutex.LOCKED) >= 0); |
887 |
|
|
} |
888 |
|
|
|
889 |
|
|
/** |
890 |
|
|
* A serialized AQS deserializes with current state |
891 |
|
|
*/ |
892 |
jsr166 |
1.9 |
public void testSerialization() throws Exception { |
893 |
dl |
1.1 |
Mutex l = new Mutex(); |
894 |
|
|
l.acquire(1); |
895 |
|
|
assertTrue(l.isHeldExclusively()); |
896 |
|
|
|
897 |
jsr166 |
1.9 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
898 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
899 |
|
|
out.writeObject(l); |
900 |
|
|
out.close(); |
901 |
|
|
|
902 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
903 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
904 |
|
|
Mutex r = (Mutex) in.readObject(); |
905 |
|
|
assertTrue(r.isHeldExclusively()); |
906 |
dl |
1.1 |
} |
907 |
|
|
|
908 |
|
|
|
909 |
|
|
/** |
910 |
|
|
* tryReleaseShared setting state changes getState |
911 |
|
|
*/ |
912 |
|
|
public void testGetStateWithReleaseShared() { |
913 |
jsr166 |
1.11 |
final BooleanLatch l = new BooleanLatch(); |
914 |
|
|
assertFalse(l.isSignalled()); |
915 |
|
|
l.releaseShared(0); |
916 |
|
|
assertTrue(l.isSignalled()); |
917 |
dl |
1.1 |
} |
918 |
|
|
|
919 |
|
|
/** |
920 |
|
|
* releaseShared has no effect when already signalled |
921 |
|
|
*/ |
922 |
|
|
public void testReleaseShared() { |
923 |
jsr166 |
1.11 |
final BooleanLatch l = new BooleanLatch(); |
924 |
|
|
assertFalse(l.isSignalled()); |
925 |
|
|
l.releaseShared(0); |
926 |
|
|
assertTrue(l.isSignalled()); |
927 |
|
|
l.releaseShared(0); |
928 |
|
|
assertTrue(l.isSignalled()); |
929 |
dl |
1.1 |
} |
930 |
|
|
|
931 |
|
|
/** |
932 |
|
|
* acquireSharedInterruptibly returns after release, but not before |
933 |
|
|
*/ |
934 |
jsr166 |
1.9 |
public void testAcquireSharedInterruptibly() throws InterruptedException { |
935 |
jsr166 |
1.11 |
final BooleanLatch l = new BooleanLatch(); |
936 |
dl |
1.1 |
|
937 |
jsr166 |
1.11 |
Thread t = new Thread(new CheckedRunnable() { |
938 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
939 |
jsr166 |
1.17 |
assertFalse(l.isSignalled()); |
940 |
jsr166 |
1.10 |
l.acquireSharedInterruptibly(0); |
941 |
jsr166 |
1.17 |
assertTrue(l.isSignalled()); |
942 |
jsr166 |
1.10 |
}}); |
943 |
jsr166 |
1.9 |
|
944 |
|
|
t.start(); |
945 |
|
|
assertFalse(l.isSignalled()); |
946 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
947 |
|
|
l.releaseShared(0); |
948 |
|
|
assertTrue(l.isSignalled()); |
949 |
|
|
t.join(); |
950 |
dl |
1.1 |
} |
951 |
jsr166 |
1.5 |
|
952 |
dl |
1.1 |
|
953 |
|
|
/** |
954 |
|
|
* acquireSharedTimed returns after release |
955 |
|
|
*/ |
956 |
jsr166 |
1.19 |
public void testAquireSharedTimed() throws InterruptedException { |
957 |
jsr166 |
1.11 |
final BooleanLatch l = new BooleanLatch(); |
958 |
dl |
1.1 |
|
959 |
jsr166 |
1.11 |
Thread t = new Thread(new CheckedRunnable() { |
960 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
961 |
jsr166 |
1.15 |
assertFalse(l.isSignalled()); |
962 |
|
|
long nanos = MILLISECONDS.toNanos(MEDIUM_DELAY_MS); |
963 |
|
|
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
964 |
|
|
assertTrue(l.isSignalled()); |
965 |
jsr166 |
1.10 |
}}); |
966 |
jsr166 |
1.9 |
|
967 |
|
|
t.start(); |
968 |
|
|
assertFalse(l.isSignalled()); |
969 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
970 |
|
|
l.releaseShared(0); |
971 |
|
|
assertTrue(l.isSignalled()); |
972 |
|
|
t.join(); |
973 |
dl |
1.1 |
} |
974 |
jsr166 |
1.5 |
|
975 |
dl |
1.1 |
/** |
976 |
|
|
* acquireSharedInterruptibly throws IE if interrupted before released |
977 |
|
|
*/ |
978 |
jsr166 |
1.17 |
public void testAcquireSharedInterruptibly_InterruptedException() |
979 |
|
|
throws InterruptedException { |
980 |
dl |
1.1 |
final BooleanLatch l = new BooleanLatch(); |
981 |
jsr166 |
1.11 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
982 |
|
|
public void realRun() throws InterruptedException { |
983 |
jsr166 |
1.17 |
assertFalse(l.isSignalled()); |
984 |
jsr166 |
1.10 |
l.acquireSharedInterruptibly(0); |
985 |
|
|
}}); |
986 |
jsr166 |
1.9 |
|
987 |
jsr166 |
1.11 |
t.start(); |
988 |
jsr166 |
1.9 |
assertFalse(l.isSignalled()); |
989 |
|
|
t.interrupt(); |
990 |
|
|
t.join(); |
991 |
dl |
1.1 |
} |
992 |
|
|
|
993 |
|
|
/** |
994 |
|
|
* acquireSharedTimed throws IE if interrupted before released |
995 |
|
|
*/ |
996 |
jsr166 |
1.9 |
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
997 |
dl |
1.1 |
final BooleanLatch l = new BooleanLatch(); |
998 |
jsr166 |
1.11 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
999 |
|
|
public void realRun() throws InterruptedException { |
1000 |
jsr166 |
1.15 |
assertFalse(l.isSignalled()); |
1001 |
|
|
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1002 |
|
|
l.tryAcquireSharedNanos(0, nanos); |
1003 |
jsr166 |
1.10 |
}}); |
1004 |
jsr166 |
1.9 |
|
1005 |
dl |
1.1 |
t.start(); |
1006 |
jsr166 |
1.9 |
Thread.sleep(SHORT_DELAY_MS); |
1007 |
|
|
assertFalse(l.isSignalled()); |
1008 |
|
|
t.interrupt(); |
1009 |
|
|
t.join(); |
1010 |
dl |
1.1 |
} |
1011 |
|
|
|
1012 |
|
|
/** |
1013 |
|
|
* acquireSharedTimed times out if not released before timeout |
1014 |
|
|
*/ |
1015 |
jsr166 |
1.9 |
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1016 |
dl |
1.1 |
final BooleanLatch l = new BooleanLatch(); |
1017 |
jsr166 |
1.11 |
Thread t = new Thread(new CheckedRunnable() { |
1018 |
jsr166 |
1.10 |
public void realRun() throws InterruptedException { |
1019 |
jsr166 |
1.15 |
assertFalse(l.isSignalled()); |
1020 |
|
|
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1021 |
|
|
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1022 |
jsr166 |
1.10 |
}}); |
1023 |
jsr166 |
1.9 |
|
1024 |
dl |
1.1 |
t.start(); |
1025 |
jsr166 |
1.9 |
Thread.sleep(SHORT_DELAY_MS); |
1026 |
|
|
assertFalse(l.isSignalled()); |
1027 |
|
|
t.join(); |
1028 |
dl |
1.1 |
} |
1029 |
|
|
|
1030 |
|
|
} |