1 |
dl |
1.1 |
/* |
2 |
dl |
1.7 |
* 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.15 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
jsr166 |
1.19 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
dl |
1.2 |
import java.io.*; |
14 |
dl |
1.1 |
|
15 |
dl |
1.3 |
public class SemaphoreTest extends JSR166TestCase { |
16 |
dl |
1.1 |
public static void main(String[] args) { |
17 |
jsr166 |
1.15 |
junit.textui.TestRunner.run (suite()); |
18 |
dl |
1.1 |
} |
19 |
|
|
public static Test suite() { |
20 |
|
|
return new TestSuite(SemaphoreTest.class); |
21 |
|
|
} |
22 |
|
|
|
23 |
dl |
1.4 |
/** |
24 |
dl |
1.6 |
* Subclass to expose protected methods |
25 |
|
|
*/ |
26 |
|
|
static class PublicSemaphore extends Semaphore { |
27 |
|
|
PublicSemaphore(int p, boolean f) { super(p, f); } |
28 |
jsr166 |
1.15 |
public Collection<Thread> getQueuedThreads() { |
29 |
|
|
return super.getQueuedThreads(); |
30 |
dl |
1.6 |
} |
31 |
jsr166 |
1.15 |
public void reducePermits(int p) { |
32 |
dl |
1.6 |
super.reducePermits(p); |
33 |
|
|
} |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
/** |
37 |
|
|
* A runnable calling acquire |
38 |
|
|
*/ |
39 |
jsr166 |
1.19 |
class InterruptibleLockRunnable extends CheckedRunnable { |
40 |
dl |
1.6 |
final Semaphore lock; |
41 |
|
|
InterruptibleLockRunnable(Semaphore l) { lock = l; } |
42 |
jsr166 |
1.19 |
public void realRun() { |
43 |
dl |
1.6 |
try { |
44 |
|
|
lock.acquire(); |
45 |
jsr166 |
1.19 |
} |
46 |
|
|
catch (InterruptedException ignored) {} |
47 |
dl |
1.6 |
} |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
/** |
52 |
|
|
* A runnable calling acquire that expects to be |
53 |
|
|
* interrupted |
54 |
|
|
*/ |
55 |
jsr166 |
1.19 |
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
56 |
dl |
1.6 |
final Semaphore lock; |
57 |
|
|
InterruptedLockRunnable(Semaphore l) { lock = l; } |
58 |
jsr166 |
1.19 |
public void realRun() throws InterruptedException { |
59 |
|
|
lock.acquire(); |
60 |
dl |
1.6 |
} |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
/** |
64 |
dl |
1.5 |
* Zero, negative, and positive initial values are allowed in constructor |
65 |
dl |
1.4 |
*/ |
66 |
dl |
1.5 |
public void testConstructor() { |
67 |
jsr166 |
1.19 |
for (int permits : new int[] { -1, 0, 1 }) { |
68 |
|
|
for (boolean fair : new boolean[] { false, true }) { |
69 |
|
|
Semaphore s = new Semaphore(permits, fair); |
70 |
|
|
assertEquals(permits, s.availablePermits()); |
71 |
|
|
assertEquals(fair, s.isFair()); |
72 |
|
|
} |
73 |
|
|
} |
74 |
dl |
1.13 |
} |
75 |
|
|
|
76 |
|
|
/** |
77 |
|
|
* Constructor without fairness argument behaves as nonfair |
78 |
|
|
*/ |
79 |
|
|
public void testConstructor2() { |
80 |
jsr166 |
1.19 |
for (int permits : new int[] { -1, 0, 1 }) { |
81 |
|
|
Semaphore s = new Semaphore(permits); |
82 |
|
|
assertEquals(permits, s.availablePermits()); |
83 |
|
|
assertFalse(s.isFair()); |
84 |
|
|
} |
85 |
dl |
1.1 |
} |
86 |
|
|
|
87 |
dl |
1.4 |
/** |
88 |
dl |
1.9 |
* tryAcquire succeeds when sufficient permits, else fails |
89 |
dl |
1.4 |
*/ |
90 |
dl |
1.1 |
public void testTryAcquireInSameThread() { |
91 |
dl |
1.6 |
Semaphore s = new Semaphore(2, false); |
92 |
dl |
1.1 |
assertEquals(2, s.availablePermits()); |
93 |
|
|
assertTrue(s.tryAcquire()); |
94 |
|
|
assertTrue(s.tryAcquire()); |
95 |
|
|
assertEquals(0, s.availablePermits()); |
96 |
|
|
assertFalse(s.tryAcquire()); |
97 |
|
|
} |
98 |
|
|
|
99 |
dl |
1.4 |
/** |
100 |
dl |
1.5 |
* Acquire and release of semaphore succeed if initially available |
101 |
dl |
1.4 |
*/ |
102 |
jsr166 |
1.19 |
public void testAcquireReleaseInSameThread() |
103 |
|
|
throws InterruptedException { |
104 |
dl |
1.6 |
Semaphore s = new Semaphore(1, false); |
105 |
jsr166 |
1.19 |
s.acquire(); |
106 |
|
|
s.release(); |
107 |
|
|
s.acquire(); |
108 |
|
|
s.release(); |
109 |
|
|
s.acquire(); |
110 |
|
|
s.release(); |
111 |
|
|
s.acquire(); |
112 |
|
|
s.release(); |
113 |
|
|
s.acquire(); |
114 |
|
|
s.release(); |
115 |
|
|
assertEquals(1, s.availablePermits()); |
116 |
dl |
1.1 |
} |
117 |
|
|
|
118 |
dl |
1.4 |
/** |
119 |
dl |
1.5 |
* Uninterruptible acquire and release of semaphore succeed if |
120 |
|
|
* initially available |
121 |
dl |
1.4 |
*/ |
122 |
jsr166 |
1.19 |
public void testAcquireUninterruptiblyReleaseInSameThread() |
123 |
|
|
throws InterruptedException { |
124 |
dl |
1.6 |
Semaphore s = new Semaphore(1, false); |
125 |
jsr166 |
1.19 |
s.acquireUninterruptibly(); |
126 |
|
|
s.release(); |
127 |
|
|
s.acquireUninterruptibly(); |
128 |
|
|
s.release(); |
129 |
|
|
s.acquireUninterruptibly(); |
130 |
|
|
s.release(); |
131 |
|
|
s.acquireUninterruptibly(); |
132 |
|
|
s.release(); |
133 |
|
|
s.acquireUninterruptibly(); |
134 |
|
|
s.release(); |
135 |
|
|
assertEquals(1, s.availablePermits()); |
136 |
dl |
1.1 |
} |
137 |
|
|
|
138 |
dl |
1.5 |
/** |
139 |
|
|
* Timed Acquire and release of semaphore succeed if |
140 |
|
|
* initially available |
141 |
|
|
*/ |
142 |
jsr166 |
1.19 |
public void testTimedAcquireReleaseInSameThread() |
143 |
|
|
throws InterruptedException { |
144 |
dl |
1.6 |
Semaphore s = new Semaphore(1, false); |
145 |
jsr166 |
1.19 |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
146 |
|
|
s.release(); |
147 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
148 |
|
|
s.release(); |
149 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
150 |
|
|
s.release(); |
151 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
152 |
|
|
s.release(); |
153 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
154 |
|
|
s.release(); |
155 |
|
|
assertEquals(1, s.availablePermits()); |
156 |
dl |
1.5 |
} |
157 |
dl |
1.1 |
|
158 |
dl |
1.4 |
/** |
159 |
dl |
1.5 |
* A release in one thread enables an acquire in another thread |
160 |
dl |
1.4 |
*/ |
161 |
jsr166 |
1.19 |
public void testAcquireReleaseInDifferentThreads() |
162 |
|
|
throws InterruptedException { |
163 |
dl |
1.6 |
final Semaphore s = new Semaphore(0, false); |
164 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedRunnable() { |
165 |
|
|
public void realRun() throws InterruptedException { |
166 |
|
|
s.acquire(); |
167 |
|
|
s.release(); |
168 |
|
|
s.release(); |
169 |
|
|
s.acquire(); |
170 |
|
|
}}); |
171 |
|
|
|
172 |
|
|
t.start(); |
173 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
174 |
|
|
s.release(); |
175 |
|
|
s.release(); |
176 |
|
|
s.acquire(); |
177 |
|
|
s.acquire(); |
178 |
|
|
s.release(); |
179 |
|
|
t.join(); |
180 |
dl |
1.1 |
} |
181 |
|
|
|
182 |
dl |
1.4 |
/** |
183 |
dl |
1.5 |
* A release in one thread enables an uninterruptible acquire in another thread |
184 |
dl |
1.4 |
*/ |
185 |
jsr166 |
1.19 |
public void testUninterruptibleAcquireReleaseInDifferentThreads() |
186 |
|
|
throws InterruptedException { |
187 |
dl |
1.6 |
final Semaphore s = new Semaphore(0, false); |
188 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedRunnable() { |
189 |
|
|
public void realRun() throws InterruptedException { |
190 |
|
|
s.acquireUninterruptibly(); |
191 |
|
|
s.release(); |
192 |
|
|
s.release(); |
193 |
|
|
s.acquireUninterruptibly(); |
194 |
|
|
}}); |
195 |
|
|
|
196 |
|
|
t.start(); |
197 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
198 |
|
|
s.release(); |
199 |
|
|
s.release(); |
200 |
|
|
s.acquireUninterruptibly(); |
201 |
|
|
s.acquireUninterruptibly(); |
202 |
|
|
s.release(); |
203 |
|
|
t.join(); |
204 |
dl |
1.1 |
} |
205 |
|
|
|
206 |
dl |
1.5 |
|
207 |
dl |
1.4 |
/** |
208 |
dl |
1.5 |
* A release in one thread enables a timed acquire in another thread |
209 |
dl |
1.4 |
*/ |
210 |
jsr166 |
1.19 |
public void testTimedAcquireReleaseInDifferentThreads() |
211 |
|
|
throws InterruptedException { |
212 |
dl |
1.6 |
final Semaphore s = new Semaphore(1, false); |
213 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedRunnable() { |
214 |
|
|
public void realRun() throws InterruptedException { |
215 |
|
|
s.release(); |
216 |
|
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
217 |
|
|
s.release(); |
218 |
|
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
219 |
|
|
}}); |
220 |
|
|
|
221 |
|
|
t.start(); |
222 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
223 |
|
|
s.release(); |
224 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
225 |
|
|
s.release(); |
226 |
|
|
s.release(); |
227 |
|
|
t.join(); |
228 |
dl |
1.1 |
} |
229 |
|
|
|
230 |
dl |
1.4 |
/** |
231 |
dl |
1.5 |
* A waiting acquire blocks interruptibly |
232 |
dl |
1.4 |
*/ |
233 |
jsr166 |
1.19 |
public void testAcquire_InterruptedException() |
234 |
|
|
throws InterruptedException { |
235 |
dl |
1.6 |
final Semaphore s = new Semaphore(0, false); |
236 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
237 |
|
|
public void realRun() throws InterruptedException { |
238 |
|
|
s.acquire(); |
239 |
|
|
}}); |
240 |
|
|
|
241 |
dl |
1.1 |
t.start(); |
242 |
jsr166 |
1.19 |
Thread.sleep(SHORT_DELAY_MS); |
243 |
|
|
t.interrupt(); |
244 |
|
|
t.join(); |
245 |
dl |
1.1 |
} |
246 |
jsr166 |
1.15 |
|
247 |
dl |
1.4 |
/** |
248 |
dl |
1.5 |
* A waiting timed acquire blocks interruptibly |
249 |
dl |
1.4 |
*/ |
250 |
jsr166 |
1.19 |
public void testTryAcquire_InterruptedException() |
251 |
|
|
throws InterruptedException { |
252 |
dl |
1.6 |
final Semaphore s = new Semaphore(0, false); |
253 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
254 |
|
|
public void realRun() throws InterruptedException { |
255 |
|
|
s.tryAcquire(MEDIUM_DELAY_MS, MILLISECONDS); |
256 |
|
|
}}); |
257 |
|
|
|
258 |
dl |
1.1 |
t.start(); |
259 |
jsr166 |
1.19 |
Thread.sleep(SHORT_DELAY_MS); |
260 |
|
|
t.interrupt(); |
261 |
|
|
t.join(); |
262 |
dl |
1.1 |
} |
263 |
dl |
1.2 |
|
264 |
dl |
1.4 |
/** |
265 |
dl |
1.8 |
* hasQueuedThreads reports whether there are waiting threads |
266 |
|
|
*/ |
267 |
jsr166 |
1.19 |
public void testHasQueuedThreads() throws InterruptedException { |
268 |
dl |
1.8 |
final Semaphore lock = new Semaphore(1, false); |
269 |
|
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
270 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
271 |
jsr166 |
1.19 |
assertFalse(lock.hasQueuedThreads()); |
272 |
|
|
lock.acquireUninterruptibly(); |
273 |
|
|
t1.start(); |
274 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
275 |
|
|
assertTrue(lock.hasQueuedThreads()); |
276 |
|
|
t2.start(); |
277 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
278 |
|
|
assertTrue(lock.hasQueuedThreads()); |
279 |
|
|
t1.interrupt(); |
280 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
281 |
|
|
assertTrue(lock.hasQueuedThreads()); |
282 |
|
|
lock.release(); |
283 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
284 |
|
|
assertFalse(lock.hasQueuedThreads()); |
285 |
|
|
t1.join(); |
286 |
|
|
t2.join(); |
287 |
jsr166 |
1.15 |
} |
288 |
dl |
1.8 |
|
289 |
|
|
/** |
290 |
dl |
1.6 |
* getQueueLength reports number of waiting threads |
291 |
|
|
*/ |
292 |
jsr166 |
1.19 |
public void testGetQueueLength() throws InterruptedException { |
293 |
dl |
1.6 |
final Semaphore lock = new Semaphore(1, false); |
294 |
|
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
295 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
296 |
jsr166 |
1.19 |
assertEquals(0, lock.getQueueLength()); |
297 |
|
|
lock.acquireUninterruptibly(); |
298 |
|
|
t1.start(); |
299 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
300 |
|
|
assertEquals(1, lock.getQueueLength()); |
301 |
|
|
t2.start(); |
302 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
303 |
|
|
assertEquals(2, lock.getQueueLength()); |
304 |
|
|
t1.interrupt(); |
305 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
306 |
|
|
assertEquals(1, lock.getQueueLength()); |
307 |
|
|
lock.release(); |
308 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
309 |
|
|
assertEquals(0, lock.getQueueLength()); |
310 |
|
|
t1.join(); |
311 |
|
|
t2.join(); |
312 |
jsr166 |
1.15 |
} |
313 |
dl |
1.6 |
|
314 |
|
|
/** |
315 |
|
|
* getQueuedThreads includes waiting threads |
316 |
|
|
*/ |
317 |
jsr166 |
1.19 |
public void testGetQueuedThreads() throws InterruptedException { |
318 |
dl |
1.6 |
final PublicSemaphore lock = new PublicSemaphore(1, false); |
319 |
|
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
320 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
321 |
jsr166 |
1.19 |
assertTrue(lock.getQueuedThreads().isEmpty()); |
322 |
|
|
lock.acquireUninterruptibly(); |
323 |
|
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
324 |
|
|
t1.start(); |
325 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
326 |
|
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
327 |
|
|
t2.start(); |
328 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
329 |
|
|
assertTrue(lock.getQueuedThreads().contains(t1)); |
330 |
|
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
331 |
|
|
t1.interrupt(); |
332 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
333 |
|
|
assertFalse(lock.getQueuedThreads().contains(t1)); |
334 |
|
|
assertTrue(lock.getQueuedThreads().contains(t2)); |
335 |
|
|
lock.release(); |
336 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
337 |
|
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
338 |
|
|
t1.join(); |
339 |
|
|
t2.join(); |
340 |
jsr166 |
1.15 |
} |
341 |
dl |
1.6 |
|
342 |
dl |
1.11 |
/** |
343 |
|
|
* drainPermits reports and removes given number of permits |
344 |
|
|
*/ |
345 |
|
|
public void testDrainPermits() { |
346 |
|
|
Semaphore s = new Semaphore(0, false); |
347 |
|
|
assertEquals(0, s.availablePermits()); |
348 |
|
|
assertEquals(0, s.drainPermits()); |
349 |
|
|
s.release(10); |
350 |
|
|
assertEquals(10, s.availablePermits()); |
351 |
|
|
assertEquals(10, s.drainPermits()); |
352 |
|
|
assertEquals(0, s.availablePermits()); |
353 |
|
|
assertEquals(0, s.drainPermits()); |
354 |
|
|
} |
355 |
dl |
1.6 |
|
356 |
|
|
/** |
357 |
|
|
* reducePermits reduces number of permits |
358 |
|
|
*/ |
359 |
|
|
public void testReducePermits() { |
360 |
|
|
PublicSemaphore s = new PublicSemaphore(10, false); |
361 |
|
|
assertEquals(10, s.availablePermits()); |
362 |
|
|
s.reducePermits(1); |
363 |
|
|
assertEquals(9, s.availablePermits()); |
364 |
|
|
s.reducePermits(10); |
365 |
|
|
assertEquals(-1, s.availablePermits()); |
366 |
|
|
} |
367 |
|
|
|
368 |
|
|
/** |
369 |
dl |
1.5 |
* a deserialized serialized semaphore has same number of permits |
370 |
dl |
1.4 |
*/ |
371 |
jsr166 |
1.19 |
public void testSerialization() throws Exception { |
372 |
dl |
1.6 |
Semaphore l = new Semaphore(3, false); |
373 |
jsr166 |
1.19 |
l.acquire(); |
374 |
|
|
l.release(); |
375 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
376 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
377 |
|
|
out.writeObject(l); |
378 |
|
|
out.close(); |
379 |
|
|
|
380 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
381 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
382 |
|
|
Semaphore r = (Semaphore) in.readObject(); |
383 |
|
|
assertEquals(3, r.availablePermits()); |
384 |
|
|
assertFalse(r.isFair()); |
385 |
|
|
r.acquire(); |
386 |
|
|
r.release(); |
387 |
dl |
1.6 |
} |
388 |
|
|
|
389 |
|
|
|
390 |
|
|
/** |
391 |
|
|
* Zero, negative, and positive initial values are allowed in constructor |
392 |
|
|
*/ |
393 |
|
|
public void testConstructor_fair() { |
394 |
|
|
Semaphore s0 = new Semaphore(0, true); |
395 |
|
|
assertEquals(0, s0.availablePermits()); |
396 |
|
|
assertTrue(s0.isFair()); |
397 |
|
|
Semaphore s1 = new Semaphore(-1, true); |
398 |
|
|
assertEquals(-1, s1.availablePermits()); |
399 |
|
|
Semaphore s2 = new Semaphore(-1, true); |
400 |
|
|
assertEquals(-1, s2.availablePermits()); |
401 |
|
|
} |
402 |
|
|
|
403 |
|
|
/** |
404 |
dl |
1.9 |
* tryAcquire succeeds when sufficient permits, else fails |
405 |
dl |
1.6 |
*/ |
406 |
|
|
public void testTryAcquireInSameThread_fair() { |
407 |
|
|
Semaphore s = new Semaphore(2, true); |
408 |
|
|
assertEquals(2, s.availablePermits()); |
409 |
|
|
assertTrue(s.tryAcquire()); |
410 |
|
|
assertTrue(s.tryAcquire()); |
411 |
|
|
assertEquals(0, s.availablePermits()); |
412 |
|
|
assertFalse(s.tryAcquire()); |
413 |
|
|
} |
414 |
|
|
|
415 |
|
|
/** |
416 |
dl |
1.9 |
* tryAcquire(n) succeeds when sufficient permits, else fails |
417 |
dl |
1.6 |
*/ |
418 |
|
|
public void testTryAcquireNInSameThread_fair() { |
419 |
|
|
Semaphore s = new Semaphore(2, true); |
420 |
|
|
assertEquals(2, s.availablePermits()); |
421 |
|
|
assertTrue(s.tryAcquire(2)); |
422 |
|
|
assertEquals(0, s.availablePermits()); |
423 |
|
|
assertFalse(s.tryAcquire()); |
424 |
|
|
} |
425 |
|
|
|
426 |
|
|
/** |
427 |
|
|
* Acquire and release of semaphore succeed if initially available |
428 |
|
|
*/ |
429 |
jsr166 |
1.19 |
public void testAcquireReleaseInSameThread_fair() |
430 |
|
|
throws InterruptedException { |
431 |
dl |
1.6 |
Semaphore s = new Semaphore(1, true); |
432 |
jsr166 |
1.19 |
s.acquire(); |
433 |
|
|
s.release(); |
434 |
|
|
s.acquire(); |
435 |
|
|
s.release(); |
436 |
|
|
s.acquire(); |
437 |
|
|
s.release(); |
438 |
|
|
s.acquire(); |
439 |
|
|
s.release(); |
440 |
|
|
s.acquire(); |
441 |
|
|
s.release(); |
442 |
|
|
assertEquals(1, s.availablePermits()); |
443 |
dl |
1.6 |
} |
444 |
|
|
|
445 |
|
|
/** |
446 |
|
|
* Acquire(n) and release(n) of semaphore succeed if initially available |
447 |
|
|
*/ |
448 |
jsr166 |
1.19 |
public void testAcquireReleaseNInSameThread_fair() |
449 |
|
|
throws InterruptedException { |
450 |
dl |
1.6 |
Semaphore s = new Semaphore(1, true); |
451 |
jsr166 |
1.19 |
s.release(1); |
452 |
|
|
s.acquire(1); |
453 |
|
|
s.release(2); |
454 |
|
|
s.acquire(2); |
455 |
|
|
s.release(3); |
456 |
|
|
s.acquire(3); |
457 |
|
|
s.release(4); |
458 |
|
|
s.acquire(4); |
459 |
|
|
s.release(5); |
460 |
|
|
s.acquire(5); |
461 |
|
|
assertEquals(1, s.availablePermits()); |
462 |
dl |
1.6 |
} |
463 |
|
|
|
464 |
|
|
/** |
465 |
|
|
* Acquire(n) and release(n) of semaphore succeed if initially available |
466 |
|
|
*/ |
467 |
|
|
public void testAcquireUninterruptiblyReleaseNInSameThread_fair() { |
468 |
|
|
Semaphore s = new Semaphore(1, true); |
469 |
jsr166 |
1.19 |
s.release(1); |
470 |
|
|
s.acquireUninterruptibly(1); |
471 |
|
|
s.release(2); |
472 |
|
|
s.acquireUninterruptibly(2); |
473 |
|
|
s.release(3); |
474 |
|
|
s.acquireUninterruptibly(3); |
475 |
|
|
s.release(4); |
476 |
|
|
s.acquireUninterruptibly(4); |
477 |
|
|
s.release(5); |
478 |
|
|
s.acquireUninterruptibly(5); |
479 |
|
|
assertEquals(1, s.availablePermits()); |
480 |
dl |
1.6 |
} |
481 |
|
|
|
482 |
|
|
/** |
483 |
|
|
* release(n) in one thread enables timed acquire(n) in another thread |
484 |
|
|
*/ |
485 |
jsr166 |
1.19 |
public void testTimedAcquireReleaseNInSameThread_fair() |
486 |
|
|
throws InterruptedException { |
487 |
dl |
1.6 |
Semaphore s = new Semaphore(1, true); |
488 |
jsr166 |
1.19 |
s.release(1); |
489 |
|
|
assertTrue(s.tryAcquire(1, SHORT_DELAY_MS, MILLISECONDS)); |
490 |
|
|
s.release(2); |
491 |
|
|
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
492 |
|
|
s.release(3); |
493 |
|
|
assertTrue(s.tryAcquire(3, SHORT_DELAY_MS, MILLISECONDS)); |
494 |
|
|
s.release(4); |
495 |
|
|
assertTrue(s.tryAcquire(4, SHORT_DELAY_MS, MILLISECONDS)); |
496 |
|
|
s.release(5); |
497 |
|
|
assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, MILLISECONDS)); |
498 |
|
|
assertEquals(1, s.availablePermits()); |
499 |
dl |
1.6 |
} |
500 |
|
|
|
501 |
|
|
/** |
502 |
|
|
* release in one thread enables timed acquire in another thread |
503 |
|
|
*/ |
504 |
jsr166 |
1.19 |
public void testTimedAcquireReleaseInSameThread_fair() |
505 |
|
|
throws InterruptedException { |
506 |
dl |
1.6 |
Semaphore s = new Semaphore(1, true); |
507 |
jsr166 |
1.19 |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
508 |
|
|
s.release(); |
509 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
510 |
|
|
s.release(); |
511 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
512 |
|
|
s.release(); |
513 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
514 |
|
|
s.release(); |
515 |
|
|
assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
516 |
|
|
s.release(); |
517 |
|
|
assertEquals(1, s.availablePermits()); |
518 |
dl |
1.6 |
} |
519 |
|
|
|
520 |
|
|
/** |
521 |
|
|
* A release in one thread enables an acquire in another thread |
522 |
|
|
*/ |
523 |
jsr166 |
1.19 |
public void testAcquireReleaseInDifferentThreads_fair() |
524 |
|
|
throws InterruptedException { |
525 |
dl |
1.6 |
final Semaphore s = new Semaphore(0, true); |
526 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedRunnable() { |
527 |
|
|
public void realRun() throws InterruptedException { |
528 |
|
|
s.acquire(); |
529 |
|
|
s.acquire(); |
530 |
|
|
s.acquire(); |
531 |
|
|
s.acquire(); |
532 |
|
|
}}); |
533 |
|
|
|
534 |
|
|
t.start(); |
535 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
536 |
|
|
s.release(); |
537 |
|
|
s.release(); |
538 |
|
|
s.release(); |
539 |
|
|
s.release(); |
540 |
|
|
s.release(); |
541 |
|
|
s.release(); |
542 |
|
|
t.join(); |
543 |
|
|
assertEquals(2, s.availablePermits()); |
544 |
dl |
1.6 |
} |
545 |
|
|
|
546 |
|
|
/** |
547 |
|
|
* release(n) in one thread enables acquire(n) in another thread |
548 |
|
|
*/ |
549 |
jsr166 |
1.19 |
public void testAcquireReleaseNInDifferentThreads_fair() |
550 |
|
|
throws InterruptedException { |
551 |
dl |
1.6 |
final Semaphore s = new Semaphore(0, true); |
552 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedRunnable() { |
553 |
|
|
public void realRun() throws InterruptedException { |
554 |
|
|
s.acquire(); |
555 |
|
|
s.release(2); |
556 |
|
|
s.acquire(); |
557 |
|
|
}}); |
558 |
|
|
|
559 |
|
|
t.start(); |
560 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
561 |
|
|
s.release(2); |
562 |
|
|
s.acquire(2); |
563 |
|
|
s.release(1); |
564 |
|
|
t.join(); |
565 |
dl |
1.6 |
} |
566 |
|
|
|
567 |
dl |
1.12 |
/** |
568 |
|
|
* release(n) in one thread enables acquire(n) in another thread |
569 |
|
|
*/ |
570 |
jsr166 |
1.19 |
public void testAcquireReleaseNInDifferentThreads_fair2() |
571 |
|
|
throws InterruptedException { |
572 |
dl |
1.12 |
final Semaphore s = new Semaphore(0, true); |
573 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedRunnable() { |
574 |
|
|
public void realRun() throws InterruptedException { |
575 |
|
|
s.acquire(2); |
576 |
|
|
s.acquire(2); |
577 |
|
|
s.release(4); |
578 |
|
|
}}); |
579 |
|
|
|
580 |
|
|
t.start(); |
581 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
582 |
|
|
s.release(6); |
583 |
|
|
s.acquire(2); |
584 |
|
|
s.acquire(2); |
585 |
|
|
s.release(2); |
586 |
|
|
t.join(); |
587 |
dl |
1.12 |
} |
588 |
|
|
|
589 |
|
|
|
590 |
dl |
1.6 |
/** |
591 |
|
|
* release in one thread enables timed acquire in another thread |
592 |
|
|
*/ |
593 |
jsr166 |
1.19 |
public void testTimedAcquireReleaseInDifferentThreads_fair() |
594 |
|
|
throws InterruptedException { |
595 |
dl |
1.6 |
final Semaphore s = new Semaphore(1, true); |
596 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedRunnable() { |
597 |
|
|
public void realRun() throws InterruptedException { |
598 |
|
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
599 |
|
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
600 |
|
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
601 |
|
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
602 |
|
|
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS)); |
603 |
|
|
}}); |
604 |
|
|
|
605 |
dl |
1.6 |
t.start(); |
606 |
jsr166 |
1.19 |
s.release(); |
607 |
|
|
s.release(); |
608 |
|
|
s.release(); |
609 |
|
|
s.release(); |
610 |
|
|
s.release(); |
611 |
|
|
t.join(); |
612 |
dl |
1.6 |
} |
613 |
|
|
|
614 |
|
|
/** |
615 |
|
|
* release(n) in one thread enables timed acquire(n) in another thread |
616 |
|
|
*/ |
617 |
jsr166 |
1.19 |
public void testTimedAcquireReleaseNInDifferentThreads_fair() |
618 |
|
|
throws InterruptedException { |
619 |
dl |
1.6 |
final Semaphore s = new Semaphore(2, true); |
620 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedRunnable() { |
621 |
|
|
public void realRun() throws InterruptedException { |
622 |
|
|
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
623 |
|
|
s.release(2); |
624 |
|
|
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
625 |
|
|
s.release(2); |
626 |
|
|
}}); |
627 |
|
|
|
628 |
dl |
1.6 |
t.start(); |
629 |
jsr166 |
1.19 |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
630 |
|
|
s.release(2); |
631 |
|
|
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS)); |
632 |
|
|
s.release(2); |
633 |
|
|
t.join(); |
634 |
dl |
1.6 |
} |
635 |
|
|
|
636 |
|
|
/** |
637 |
|
|
* A waiting acquire blocks interruptibly |
638 |
|
|
*/ |
639 |
jsr166 |
1.19 |
public void testAcquire_InterruptedException_fair() |
640 |
|
|
throws InterruptedException { |
641 |
dl |
1.6 |
final Semaphore s = new Semaphore(0, true); |
642 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
643 |
|
|
public void realRun() throws InterruptedException { |
644 |
|
|
s.acquire(); |
645 |
|
|
}}); |
646 |
|
|
|
647 |
dl |
1.6 |
t.start(); |
648 |
jsr166 |
1.19 |
Thread.sleep(SHORT_DELAY_MS); |
649 |
|
|
t.interrupt(); |
650 |
|
|
t.join(); |
651 |
dl |
1.6 |
} |
652 |
|
|
|
653 |
|
|
/** |
654 |
|
|
* A waiting acquire(n) blocks interruptibly |
655 |
|
|
*/ |
656 |
jsr166 |
1.19 |
public void testAcquireN_InterruptedException_fair() |
657 |
|
|
throws InterruptedException { |
658 |
dl |
1.6 |
final Semaphore s = new Semaphore(2, true); |
659 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
660 |
|
|
public void realRun() throws InterruptedException { |
661 |
|
|
s.acquire(3); |
662 |
|
|
}}); |
663 |
|
|
|
664 |
dl |
1.6 |
t.start(); |
665 |
jsr166 |
1.19 |
Thread.sleep(SHORT_DELAY_MS); |
666 |
|
|
t.interrupt(); |
667 |
|
|
t.join(); |
668 |
dl |
1.6 |
} |
669 |
jsr166 |
1.15 |
|
670 |
dl |
1.6 |
/** |
671 |
|
|
* A waiting tryAcquire blocks interruptibly |
672 |
|
|
*/ |
673 |
jsr166 |
1.19 |
public void testTryAcquire_InterruptedException_fair() |
674 |
|
|
throws InterruptedException { |
675 |
dl |
1.6 |
final Semaphore s = new Semaphore(0, true); |
676 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
677 |
|
|
public void realRun() throws InterruptedException { |
678 |
|
|
s.tryAcquire(MEDIUM_DELAY_MS, MILLISECONDS); |
679 |
|
|
}}); |
680 |
|
|
|
681 |
dl |
1.6 |
t.start(); |
682 |
jsr166 |
1.19 |
Thread.sleep(SHORT_DELAY_MS); |
683 |
|
|
t.interrupt(); |
684 |
|
|
t.join(); |
685 |
dl |
1.6 |
} |
686 |
|
|
|
687 |
|
|
/** |
688 |
|
|
* A waiting tryAcquire(n) blocks interruptibly |
689 |
|
|
*/ |
690 |
jsr166 |
1.19 |
public void testTryAcquireN_InterruptedException_fair() |
691 |
|
|
throws InterruptedException { |
692 |
dl |
1.6 |
final Semaphore s = new Semaphore(1, true); |
693 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
694 |
|
|
public void realRun() throws InterruptedException { |
695 |
|
|
s.tryAcquire(4, MEDIUM_DELAY_MS, MILLISECONDS); |
696 |
|
|
}}); |
697 |
|
|
|
698 |
dl |
1.6 |
t.start(); |
699 |
jsr166 |
1.19 |
Thread.sleep(SHORT_DELAY_MS); |
700 |
|
|
t.interrupt(); |
701 |
|
|
t.join(); |
702 |
dl |
1.6 |
} |
703 |
|
|
|
704 |
|
|
/** |
705 |
|
|
* getQueueLength reports number of waiting threads |
706 |
|
|
*/ |
707 |
jsr166 |
1.19 |
public void testGetQueueLength_fair() throws InterruptedException { |
708 |
dl |
1.6 |
final Semaphore lock = new Semaphore(1, true); |
709 |
|
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
710 |
|
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
711 |
jsr166 |
1.19 |
assertEquals(0, lock.getQueueLength()); |
712 |
|
|
lock.acquireUninterruptibly(); |
713 |
|
|
t1.start(); |
714 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
715 |
|
|
assertEquals(1, lock.getQueueLength()); |
716 |
|
|
t2.start(); |
717 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
718 |
|
|
assertEquals(2, lock.getQueueLength()); |
719 |
|
|
t1.interrupt(); |
720 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
721 |
|
|
assertEquals(1, lock.getQueueLength()); |
722 |
|
|
lock.release(); |
723 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
724 |
|
|
assertEquals(0, lock.getQueueLength()); |
725 |
|
|
t1.join(); |
726 |
|
|
t2.join(); |
727 |
jsr166 |
1.15 |
} |
728 |
dl |
1.6 |
|
729 |
|
|
|
730 |
|
|
/** |
731 |
|
|
* a deserialized serialized semaphore has same number of permits |
732 |
|
|
*/ |
733 |
jsr166 |
1.19 |
public void testSerialization_fair() throws Exception { |
734 |
dl |
1.6 |
Semaphore l = new Semaphore(3, true); |
735 |
|
|
|
736 |
jsr166 |
1.19 |
l.acquire(); |
737 |
|
|
l.release(); |
738 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
739 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
740 |
|
|
out.writeObject(l); |
741 |
|
|
out.close(); |
742 |
|
|
|
743 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
744 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
745 |
|
|
Semaphore r = (Semaphore) in.readObject(); |
746 |
|
|
assertEquals(3, r.availablePermits()); |
747 |
|
|
assertTrue(r.isFair()); |
748 |
|
|
r.acquire(); |
749 |
|
|
r.release(); |
750 |
dl |
1.2 |
} |
751 |
dl |
1.6 |
|
752 |
dl |
1.14 |
/** |
753 |
|
|
* toString indicates current number of permits |
754 |
|
|
*/ |
755 |
|
|
public void testToString() { |
756 |
|
|
Semaphore s = new Semaphore(0); |
757 |
|
|
String us = s.toString(); |
758 |
|
|
assertTrue(us.indexOf("Permits = 0") >= 0); |
759 |
|
|
s.release(); |
760 |
|
|
String s1 = s.toString(); |
761 |
|
|
assertTrue(s1.indexOf("Permits = 1") >= 0); |
762 |
|
|
s.release(); |
763 |
|
|
String s2 = s.toString(); |
764 |
|
|
assertTrue(s2.indexOf("Permits = 2") >= 0); |
765 |
|
|
} |
766 |
dl |
1.2 |
|
767 |
dl |
1.1 |
} |