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