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 { |
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 |
|
|
52 |
|
* A runnable calling acquire that expects to be |
53 |
|
* interrupted |
54 |
|
*/ |
55 |
< |
class InterruptedLockRunnable implements Runnable { |
55 |
> |
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
56 |
|
final Semaphore lock; |
57 |
|
InterruptedLockRunnable(Semaphore l) { lock = l; } |
58 |
< |
public void run() { |
59 |
< |
try { |
58 |
< |
lock.acquire(); |
59 |
< |
threadShouldThrow(); |
60 |
< |
} catch (InterruptedException success) {} |
58 |
> |
public void realRun() throws InterruptedException { |
59 |
> |
lock.acquire(); |
60 |
|
} |
61 |
|
} |
62 |
|
|
64 |
|
* Zero, negative, and positive initial values are allowed in constructor |
65 |
|
*/ |
66 |
|
public void testConstructor() { |
67 |
< |
Semaphore s0 = new Semaphore(0, false); |
68 |
< |
assertEquals(0, s0.availablePermits()); |
69 |
< |
assertFalse(s0.isFair()); |
70 |
< |
Semaphore s1 = new Semaphore(-1, false); |
71 |
< |
assertEquals(-1, s1.availablePermits()); |
72 |
< |
assertFalse(s1.isFair()); |
73 |
< |
Semaphore s2 = new Semaphore(-1, false); |
75 |
< |
assertEquals(-1, s2.availablePermits()); |
76 |
< |
assertFalse(s2.isFair()); |
67 |
> |
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 |
|
} |
75 |
|
|
76 |
|
/** |
77 |
|
* Constructor without fairness argument behaves as nonfair |
78 |
|
*/ |
79 |
|
public void testConstructor2() { |
80 |
< |
Semaphore s0 = new Semaphore(0); |
81 |
< |
assertEquals(0, s0.availablePermits()); |
82 |
< |
assertFalse(s0.isFair()); |
83 |
< |
Semaphore s1 = new Semaphore(-1); |
84 |
< |
assertEquals(-1, s1.availablePermits()); |
88 |
< |
assertFalse(s1.isFair()); |
89 |
< |
Semaphore s2 = new Semaphore(-1); |
90 |
< |
assertEquals(-1, s2.availablePermits()); |
91 |
< |
assertFalse(s2.isFair()); |
80 |
> |
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 |
|
} |
86 |
|
|
87 |
|
/** |
99 |
|
/** |
100 |
|
* Acquire and release of semaphore succeed if initially available |
101 |
|
*/ |
102 |
< |
public void testAcquireReleaseInSameThread() { |
102 |
> |
public void testAcquireReleaseInSameThread() |
103 |
> |
throws InterruptedException { |
104 |
|
Semaphore s = new Semaphore(1, false); |
105 |
< |
try { |
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 |
< |
s.acquire(); |
115 |
< |
s.release(); |
122 |
< |
assertEquals(1, s.availablePermits()); |
123 |
< |
} catch (InterruptedException e) { |
124 |
< |
unexpectedException(); |
125 |
< |
} |
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(); |
115 |
> |
assertEquals(1, s.availablePermits()); |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
|
* Uninterruptible acquire and release of semaphore succeed if |
120 |
|
* initially available |
121 |
|
*/ |
122 |
< |
public void testAcquireUninterruptiblyReleaseInSameThread() { |
122 |
> |
public void testAcquireUninterruptiblyReleaseInSameThread() |
123 |
> |
throws InterruptedException { |
124 |
|
Semaphore s = new Semaphore(1, false); |
125 |
< |
try { |
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 |
< |
s.acquireUninterruptibly(); |
135 |
< |
s.release(); |
145 |
< |
assertEquals(1, s.availablePermits()); |
146 |
< |
} finally { |
147 |
< |
} |
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(); |
135 |
> |
assertEquals(1, s.availablePermits()); |
136 |
|
} |
137 |
|
|
138 |
|
/** |
139 |
|
* Timed Acquire and release of semaphore succeed if |
140 |
|
* initially available |
141 |
|
*/ |
142 |
< |
public void testTimedAcquireReleaseInSameThread() { |
142 |
> |
public void testTimedAcquireReleaseInSameThread() |
143 |
> |
throws InterruptedException { |
144 |
|
Semaphore s = new Semaphore(1, false); |
145 |
< |
try { |
146 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
147 |
< |
s.release(); |
148 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
149 |
< |
s.release(); |
150 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
151 |
< |
s.release(); |
152 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
153 |
< |
s.release(); |
154 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
155 |
< |
s.release(); |
167 |
< |
assertEquals(1, s.availablePermits()); |
168 |
< |
} catch (InterruptedException e) { |
169 |
< |
unexpectedException(); |
170 |
< |
} |
145 |
> |
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 |
|
} |
157 |
|
|
158 |
|
/** |
159 |
|
* A release in one thread enables an acquire in another thread |
160 |
|
*/ |
161 |
< |
public void testAcquireReleaseInDifferentThreads() { |
161 |
> |
public void testAcquireReleaseInDifferentThreads() |
162 |
> |
throws InterruptedException { |
163 |
|
final Semaphore s = new Semaphore(0, false); |
164 |
< |
Thread t = new Thread(new Runnable() { |
165 |
< |
public void run() { |
166 |
< |
try { |
167 |
< |
s.acquire(); |
168 |
< |
s.release(); |
169 |
< |
s.release(); |
170 |
< |
s.acquire(); |
171 |
< |
} catch (InterruptedException ie) { |
172 |
< |
threadUnexpectedException(); |
173 |
< |
} |
174 |
< |
} |
175 |
< |
}); |
176 |
< |
try { |
177 |
< |
t.start(); |
178 |
< |
Thread.sleep(SHORT_DELAY_MS); |
179 |
< |
s.release(); |
194 |
< |
s.release(); |
195 |
< |
s.acquire(); |
196 |
< |
s.acquire(); |
197 |
< |
s.release(); |
198 |
< |
t.join(); |
199 |
< |
} catch (InterruptedException e) { |
200 |
< |
unexpectedException(); |
201 |
< |
} |
164 |
> |
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 |
|
} |
181 |
|
|
182 |
|
/** |
183 |
|
* A release in one thread enables an uninterruptible acquire in another thread |
184 |
|
*/ |
185 |
< |
public void testUninterruptibleAcquireReleaseInDifferentThreads() { |
185 |
> |
public void testUninterruptibleAcquireReleaseInDifferentThreads() |
186 |
> |
throws InterruptedException { |
187 |
|
final Semaphore s = new Semaphore(0, false); |
188 |
< |
Thread t = new Thread(new Runnable() { |
189 |
< |
public void run() { |
190 |
< |
s.acquireUninterruptibly(); |
191 |
< |
s.release(); |
192 |
< |
s.release(); |
193 |
< |
s.acquireUninterruptibly(); |
194 |
< |
} |
195 |
< |
}); |
196 |
< |
try { |
197 |
< |
t.start(); |
198 |
< |
Thread.sleep(SHORT_DELAY_MS); |
199 |
< |
s.release(); |
200 |
< |
s.release(); |
201 |
< |
s.acquireUninterruptibly(); |
202 |
< |
s.acquireUninterruptibly(); |
203 |
< |
s.release(); |
225 |
< |
t.join(); |
226 |
< |
} catch (InterruptedException e) { |
227 |
< |
unexpectedException(); |
228 |
< |
} |
188 |
> |
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 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
/** |
208 |
|
* A release in one thread enables a timed acquire in another thread |
209 |
|
*/ |
210 |
< |
public void testTimedAcquireReleaseInDifferentThreads() { |
210 |
> |
public void testTimedAcquireReleaseInDifferentThreads() |
211 |
> |
throws InterruptedException { |
212 |
|
final Semaphore s = new Semaphore(1, false); |
213 |
< |
Thread t = new Thread(new Runnable() { |
214 |
< |
public void run() { |
215 |
< |
try { |
216 |
< |
s.release(); |
217 |
< |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
218 |
< |
s.release(); |
219 |
< |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
220 |
< |
|
221 |
< |
} catch (InterruptedException ie) { |
222 |
< |
threadUnexpectedException(); |
223 |
< |
} |
224 |
< |
} |
225 |
< |
}); |
226 |
< |
try { |
227 |
< |
t.start(); |
252 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
253 |
< |
s.release(); |
254 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
255 |
< |
s.release(); |
256 |
< |
s.release(); |
257 |
< |
t.join(); |
258 |
< |
} catch (InterruptedException e) { |
259 |
< |
unexpectedException(); |
260 |
< |
} |
213 |
> |
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 |
|
} |
229 |
|
|
230 |
|
/** |
231 |
|
* A waiting acquire blocks interruptibly |
232 |
|
*/ |
233 |
< |
public void testAcquire_InterruptedException() { |
233 |
> |
public void testAcquire_InterruptedException() |
234 |
> |
throws InterruptedException { |
235 |
|
final Semaphore s = new Semaphore(0, false); |
236 |
< |
Thread t = new Thread(new Runnable() { |
237 |
< |
public void run() { |
238 |
< |
try { |
239 |
< |
s.acquire(); |
240 |
< |
threadShouldThrow(); |
273 |
< |
} catch (InterruptedException success) {} |
274 |
< |
} |
275 |
< |
}); |
236 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
237 |
> |
public void realRun() throws InterruptedException { |
238 |
> |
s.acquire(); |
239 |
> |
}}); |
240 |
> |
|
241 |
|
t.start(); |
242 |
< |
try { |
243 |
< |
Thread.sleep(SHORT_DELAY_MS); |
244 |
< |
t.interrupt(); |
280 |
< |
t.join(); |
281 |
< |
} catch (InterruptedException e) { |
282 |
< |
unexpectedException(); |
283 |
< |
} |
242 |
> |
Thread.sleep(SHORT_DELAY_MS); |
243 |
> |
t.interrupt(); |
244 |
> |
t.join(); |
245 |
|
} |
246 |
|
|
247 |
|
/** |
248 |
|
* A waiting timed acquire blocks interruptibly |
249 |
|
*/ |
250 |
< |
public void testTryAcquire_InterruptedException() { |
250 |
> |
public void testTryAcquire_InterruptedException() |
251 |
> |
throws InterruptedException { |
252 |
|
final Semaphore s = new Semaphore(0, false); |
253 |
< |
Thread t = new Thread(new Runnable() { |
254 |
< |
public void run() { |
255 |
< |
try { |
256 |
< |
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
257 |
< |
threadShouldThrow(); |
296 |
< |
} catch (InterruptedException success) { |
297 |
< |
} |
298 |
< |
} |
299 |
< |
}); |
253 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
254 |
> |
public void realRun() throws InterruptedException { |
255 |
> |
s.tryAcquire(MEDIUM_DELAY_MS, MILLISECONDS); |
256 |
> |
}}); |
257 |
> |
|
258 |
|
t.start(); |
259 |
< |
try { |
260 |
< |
Thread.sleep(SHORT_DELAY_MS); |
261 |
< |
t.interrupt(); |
304 |
< |
t.join(); |
305 |
< |
} catch (InterruptedException e) { |
306 |
< |
unexpectedException(); |
307 |
< |
} |
259 |
> |
Thread.sleep(SHORT_DELAY_MS); |
260 |
> |
t.interrupt(); |
261 |
> |
t.join(); |
262 |
|
} |
263 |
|
|
264 |
|
/** |
265 |
|
* hasQueuedThreads reports whether there are waiting threads |
266 |
|
*/ |
267 |
< |
public void testHasQueuedThreads() { |
267 |
> |
public void testHasQueuedThreads() throws InterruptedException { |
268 |
|
final Semaphore lock = new Semaphore(1, false); |
269 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
270 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
271 |
< |
try { |
272 |
< |
assertFalse(lock.hasQueuedThreads()); |
273 |
< |
lock.acquireUninterruptibly(); |
274 |
< |
t1.start(); |
275 |
< |
Thread.sleep(SHORT_DELAY_MS); |
276 |
< |
assertTrue(lock.hasQueuedThreads()); |
277 |
< |
t2.start(); |
278 |
< |
Thread.sleep(SHORT_DELAY_MS); |
279 |
< |
assertTrue(lock.hasQueuedThreads()); |
280 |
< |
t1.interrupt(); |
281 |
< |
Thread.sleep(SHORT_DELAY_MS); |
282 |
< |
assertTrue(lock.hasQueuedThreads()); |
283 |
< |
lock.release(); |
284 |
< |
Thread.sleep(SHORT_DELAY_MS); |
285 |
< |
assertFalse(lock.hasQueuedThreads()); |
286 |
< |
t1.join(); |
333 |
< |
t2.join(); |
334 |
< |
} catch (Exception e) { |
335 |
< |
unexpectedException(); |
336 |
< |
} |
271 |
> |
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 |
|
} |
288 |
|
|
289 |
|
/** |
290 |
|
* getQueueLength reports number of waiting threads |
291 |
|
*/ |
292 |
< |
public void testGetQueueLength() { |
292 |
> |
public void testGetQueueLength() throws InterruptedException { |
293 |
|
final Semaphore lock = new Semaphore(1, false); |
294 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
295 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
296 |
< |
try { |
297 |
< |
assertEquals(0, lock.getQueueLength()); |
298 |
< |
lock.acquireUninterruptibly(); |
299 |
< |
t1.start(); |
300 |
< |
Thread.sleep(SHORT_DELAY_MS); |
301 |
< |
assertEquals(1, lock.getQueueLength()); |
302 |
< |
t2.start(); |
303 |
< |
Thread.sleep(SHORT_DELAY_MS); |
304 |
< |
assertEquals(2, lock.getQueueLength()); |
305 |
< |
t1.interrupt(); |
306 |
< |
Thread.sleep(SHORT_DELAY_MS); |
307 |
< |
assertEquals(1, lock.getQueueLength()); |
308 |
< |
lock.release(); |
309 |
< |
Thread.sleep(SHORT_DELAY_MS); |
310 |
< |
assertEquals(0, lock.getQueueLength()); |
311 |
< |
t1.join(); |
362 |
< |
t2.join(); |
363 |
< |
} catch (Exception e) { |
364 |
< |
unexpectedException(); |
365 |
< |
} |
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(); |
312 |
|
} |
313 |
|
|
314 |
|
/** |
315 |
|
* getQueuedThreads includes waiting threads |
316 |
|
*/ |
317 |
< |
public void testGetQueuedThreads() { |
317 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
318 |
|
final PublicSemaphore lock = new PublicSemaphore(1, false); |
319 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
320 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
321 |
< |
try { |
322 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
323 |
< |
lock.acquireUninterruptibly(); |
324 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
325 |
< |
t1.start(); |
326 |
< |
Thread.sleep(SHORT_DELAY_MS); |
327 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
328 |
< |
t2.start(); |
329 |
< |
Thread.sleep(SHORT_DELAY_MS); |
330 |
< |
assertTrue(lock.getQueuedThreads().contains(t1)); |
331 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
332 |
< |
t1.interrupt(); |
333 |
< |
Thread.sleep(SHORT_DELAY_MS); |
334 |
< |
assertFalse(lock.getQueuedThreads().contains(t1)); |
335 |
< |
assertTrue(lock.getQueuedThreads().contains(t2)); |
336 |
< |
lock.release(); |
337 |
< |
Thread.sleep(SHORT_DELAY_MS); |
338 |
< |
assertTrue(lock.getQueuedThreads().isEmpty()); |
339 |
< |
t1.join(); |
394 |
< |
t2.join(); |
395 |
< |
} catch (Exception e) { |
396 |
< |
unexpectedException(); |
397 |
< |
} |
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(); |
340 |
|
} |
341 |
|
|
342 |
|
/** |
368 |
|
/** |
369 |
|
* a deserialized serialized semaphore has same number of permits |
370 |
|
*/ |
371 |
< |
public void testSerialization() { |
371 |
> |
public void testSerialization() throws Exception { |
372 |
|
Semaphore l = new Semaphore(3, false); |
373 |
< |
try { |
374 |
< |
l.acquire(); |
375 |
< |
l.release(); |
376 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
377 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
378 |
< |
out.writeObject(l); |
379 |
< |
out.close(); |
380 |
< |
|
381 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
382 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
383 |
< |
Semaphore r = (Semaphore) in.readObject(); |
384 |
< |
assertEquals(3, r.availablePermits()); |
385 |
< |
assertFalse(r.isFair()); |
386 |
< |
r.acquire(); |
445 |
< |
r.release(); |
446 |
< |
} catch (Exception e) { |
447 |
< |
unexpectedException(); |
448 |
< |
} |
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 |
|
} |
388 |
|
|
389 |
|
|
426 |
|
/** |
427 |
|
* Acquire and release of semaphore succeed if initially available |
428 |
|
*/ |
429 |
< |
public void testAcquireReleaseInSameThread_fair() { |
429 |
> |
public void testAcquireReleaseInSameThread_fair() |
430 |
> |
throws InterruptedException { |
431 |
|
Semaphore s = new Semaphore(1, true); |
432 |
< |
try { |
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 |
< |
s.acquire(); |
442 |
< |
s.release(); |
504 |
< |
assertEquals(1, s.availablePermits()); |
505 |
< |
} catch (InterruptedException e) { |
506 |
< |
unexpectedException(); |
507 |
< |
} |
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(); |
442 |
> |
assertEquals(1, s.availablePermits()); |
443 |
|
} |
444 |
|
|
445 |
|
/** |
446 |
|
* Acquire(n) and release(n) of semaphore succeed if initially available |
447 |
|
*/ |
448 |
< |
public void testAcquireReleaseNInSameThread_fair() { |
448 |
> |
public void testAcquireReleaseNInSameThread_fair() |
449 |
> |
throws InterruptedException { |
450 |
|
Semaphore s = new Semaphore(1, true); |
451 |
< |
try { |
452 |
< |
s.release(1); |
453 |
< |
s.acquire(1); |
454 |
< |
s.release(2); |
455 |
< |
s.acquire(2); |
456 |
< |
s.release(3); |
457 |
< |
s.acquire(3); |
458 |
< |
s.release(4); |
459 |
< |
s.acquire(4); |
460 |
< |
s.release(5); |
461 |
< |
s.acquire(5); |
526 |
< |
assertEquals(1, s.availablePermits()); |
527 |
< |
} catch (InterruptedException e) { |
528 |
< |
unexpectedException(); |
529 |
< |
} |
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); |
461 |
> |
assertEquals(1, s.availablePermits()); |
462 |
|
} |
463 |
|
|
464 |
|
/** |
466 |
|
*/ |
467 |
|
public void testAcquireUninterruptiblyReleaseNInSameThread_fair() { |
468 |
|
Semaphore s = new Semaphore(1, true); |
469 |
< |
try { |
470 |
< |
s.release(1); |
471 |
< |
s.acquireUninterruptibly(1); |
472 |
< |
s.release(2); |
473 |
< |
s.acquireUninterruptibly(2); |
474 |
< |
s.release(3); |
475 |
< |
s.acquireUninterruptibly(3); |
476 |
< |
s.release(4); |
477 |
< |
s.acquireUninterruptibly(4); |
478 |
< |
s.release(5); |
479 |
< |
s.acquireUninterruptibly(5); |
548 |
< |
assertEquals(1, s.availablePermits()); |
549 |
< |
} finally { |
550 |
< |
} |
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); |
479 |
> |
assertEquals(1, s.availablePermits()); |
480 |
|
} |
481 |
|
|
482 |
|
/** |
483 |
|
* release(n) in one thread enables timed acquire(n) in another thread |
484 |
|
*/ |
485 |
< |
public void testTimedAcquireReleaseNInSameThread_fair() { |
485 |
> |
public void testTimedAcquireReleaseNInSameThread_fair() |
486 |
> |
throws InterruptedException { |
487 |
|
Semaphore s = new Semaphore(1, true); |
488 |
< |
try { |
489 |
< |
s.release(1); |
490 |
< |
assertTrue(s.tryAcquire(1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
491 |
< |
s.release(2); |
492 |
< |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
493 |
< |
s.release(3); |
494 |
< |
assertTrue(s.tryAcquire(3, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
495 |
< |
s.release(4); |
496 |
< |
assertTrue(s.tryAcquire(4, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
497 |
< |
s.release(5); |
498 |
< |
assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
569 |
< |
assertEquals(1, s.availablePermits()); |
570 |
< |
} catch (InterruptedException e) { |
571 |
< |
unexpectedException(); |
572 |
< |
} |
488 |
> |
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 |
|
} |
500 |
|
|
501 |
|
/** |
502 |
|
* release in one thread enables timed acquire in another thread |
503 |
|
*/ |
504 |
< |
public void testTimedAcquireReleaseInSameThread_fair() { |
504 |
> |
public void testTimedAcquireReleaseInSameThread_fair() |
505 |
> |
throws InterruptedException { |
506 |
|
Semaphore s = new Semaphore(1, true); |
507 |
< |
try { |
508 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
509 |
< |
s.release(); |
510 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
511 |
< |
s.release(); |
512 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
513 |
< |
s.release(); |
514 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
515 |
< |
s.release(); |
516 |
< |
assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
517 |
< |
s.release(); |
591 |
< |
assertEquals(1, s.availablePermits()); |
592 |
< |
} catch (InterruptedException e) { |
593 |
< |
unexpectedException(); |
594 |
< |
} |
507 |
> |
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 |
|
} |
519 |
|
|
520 |
|
/** |
521 |
|
* A release in one thread enables an acquire in another thread |
522 |
|
*/ |
523 |
< |
public void testAcquireReleaseInDifferentThreads_fair() { |
523 |
> |
public void testAcquireReleaseInDifferentThreads_fair() |
524 |
> |
throws InterruptedException { |
525 |
|
final Semaphore s = new Semaphore(0, true); |
526 |
< |
Thread t = new Thread(new Runnable() { |
527 |
< |
public void run() { |
528 |
< |
try { |
529 |
< |
s.acquire(); |
530 |
< |
s.acquire(); |
531 |
< |
s.acquire(); |
532 |
< |
s.acquire(); |
533 |
< |
} catch (InterruptedException ie) { |
534 |
< |
threadUnexpectedException(); |
535 |
< |
} |
536 |
< |
} |
537 |
< |
}); |
538 |
< |
try { |
539 |
< |
t.start(); |
540 |
< |
Thread.sleep(SHORT_DELAY_MS); |
541 |
< |
s.release(); |
542 |
< |
s.release(); |
543 |
< |
s.release(); |
620 |
< |
s.release(); |
621 |
< |
s.release(); |
622 |
< |
s.release(); |
623 |
< |
t.join(); |
624 |
< |
assertEquals(2, s.availablePermits()); |
625 |
< |
} catch (InterruptedException e) { |
626 |
< |
unexpectedException(); |
627 |
< |
} |
526 |
> |
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 |
|
} |
545 |
|
|
546 |
|
/** |
547 |
|
* release(n) in one thread enables acquire(n) in another thread |
548 |
|
*/ |
549 |
< |
public void testAcquireReleaseNInDifferentThreads_fair() { |
549 |
> |
public void testAcquireReleaseNInDifferentThreads_fair() |
550 |
> |
throws InterruptedException { |
551 |
|
final Semaphore s = new Semaphore(0, true); |
552 |
< |
Thread t = new Thread(new Runnable() { |
553 |
< |
public void run() { |
554 |
< |
try { |
555 |
< |
s.acquire(); |
556 |
< |
s.release(2); |
557 |
< |
s.acquire(); |
558 |
< |
} catch (InterruptedException ie) { |
559 |
< |
threadUnexpectedException(); |
560 |
< |
} |
561 |
< |
} |
562 |
< |
}); |
563 |
< |
try { |
564 |
< |
t.start(); |
648 |
< |
Thread.sleep(SHORT_DELAY_MS); |
649 |
< |
s.release(2); |
650 |
< |
s.acquire(2); |
651 |
< |
s.release(1); |
652 |
< |
t.join(); |
653 |
< |
} catch (InterruptedException e) { |
654 |
< |
unexpectedException(); |
655 |
< |
} |
552 |
> |
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 |
|
} |
566 |
|
|
567 |
|
/** |
568 |
|
* release(n) in one thread enables acquire(n) in another thread |
569 |
|
*/ |
570 |
< |
public void testAcquireReleaseNInDifferentThreads_fair2() { |
570 |
> |
public void testAcquireReleaseNInDifferentThreads_fair2() |
571 |
> |
throws InterruptedException { |
572 |
|
final Semaphore s = new Semaphore(0, true); |
573 |
< |
Thread t = new Thread(new Runnable() { |
574 |
< |
public void run() { |
575 |
< |
try { |
576 |
< |
s.acquire(2); |
577 |
< |
s.acquire(2); |
578 |
< |
s.release(4); |
579 |
< |
} catch (InterruptedException ie) { |
580 |
< |
threadUnexpectedException(); |
581 |
< |
} |
582 |
< |
} |
583 |
< |
}); |
584 |
< |
try { |
585 |
< |
t.start(); |
586 |
< |
Thread.sleep(SHORT_DELAY_MS); |
677 |
< |
s.release(6); |
678 |
< |
s.acquire(2); |
679 |
< |
s.acquire(2); |
680 |
< |
s.release(2); |
681 |
< |
t.join(); |
682 |
< |
} catch (InterruptedException e) { |
683 |
< |
unexpectedException(); |
684 |
< |
} |
573 |
> |
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 |
|
} |
588 |
|
|
589 |
|
|
688 |
– |
|
689 |
– |
|
690 |
– |
|
590 |
|
/** |
591 |
|
* release in one thread enables timed acquire in another thread |
592 |
|
*/ |
593 |
< |
public void testTimedAcquireReleaseInDifferentThreads_fair() { |
593 |
> |
public void testTimedAcquireReleaseInDifferentThreads_fair() |
594 |
> |
throws InterruptedException { |
595 |
|
final Semaphore s = new Semaphore(1, true); |
596 |
< |
Thread t = new Thread(new Runnable() { |
597 |
< |
public void run() { |
598 |
< |
try { |
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 |
< |
threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
604 |
< |
|
705 |
< |
} catch (InterruptedException ie) { |
706 |
< |
threadUnexpectedException(); |
707 |
< |
} |
708 |
< |
} |
709 |
< |
}); |
596 |
> |
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 |
|
t.start(); |
606 |
< |
try { |
607 |
< |
s.release(); |
608 |
< |
s.release(); |
609 |
< |
s.release(); |
610 |
< |
s.release(); |
611 |
< |
s.release(); |
717 |
< |
t.join(); |
718 |
< |
} catch (InterruptedException e) { |
719 |
< |
unexpectedException(); |
720 |
< |
} |
606 |
> |
s.release(); |
607 |
> |
s.release(); |
608 |
> |
s.release(); |
609 |
> |
s.release(); |
610 |
> |
s.release(); |
611 |
> |
t.join(); |
612 |
|
} |
613 |
|
|
614 |
|
/** |
615 |
|
* release(n) in one thread enables timed acquire(n) in another thread |
616 |
|
*/ |
617 |
< |
public void testTimedAcquireReleaseNInDifferentThreads_fair() { |
617 |
> |
public void testTimedAcquireReleaseNInDifferentThreads_fair() |
618 |
> |
throws InterruptedException { |
619 |
|
final Semaphore s = new Semaphore(2, true); |
620 |
< |
Thread t = new Thread(new Runnable() { |
621 |
< |
public void run() { |
622 |
< |
try { |
623 |
< |
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
624 |
< |
s.release(2); |
625 |
< |
threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
626 |
< |
s.release(2); |
627 |
< |
} catch (InterruptedException ie) { |
736 |
< |
threadUnexpectedException(); |
737 |
< |
} |
738 |
< |
} |
739 |
< |
}); |
620 |
> |
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 |
|
t.start(); |
629 |
< |
try { |
630 |
< |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
631 |
< |
s.release(2); |
632 |
< |
assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
633 |
< |
s.release(2); |
746 |
< |
t.join(); |
747 |
< |
} catch (InterruptedException e) { |
748 |
< |
unexpectedException(); |
749 |
< |
} |
629 |
> |
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 |
|
} |
635 |
|
|
636 |
|
/** |
637 |
|
* A waiting acquire blocks interruptibly |
638 |
|
*/ |
639 |
< |
public void testAcquire_InterruptedException_fair() { |
639 |
> |
public void testAcquire_InterruptedException_fair() |
640 |
> |
throws InterruptedException { |
641 |
|
final Semaphore s = new Semaphore(0, true); |
642 |
< |
Thread t = new Thread(new Runnable() { |
643 |
< |
public void run() { |
644 |
< |
try { |
645 |
< |
s.acquire(); |
646 |
< |
threadShouldThrow(); |
762 |
< |
} catch (InterruptedException success) {} |
763 |
< |
} |
764 |
< |
}); |
642 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
643 |
> |
public void realRun() throws InterruptedException { |
644 |
> |
s.acquire(); |
645 |
> |
}}); |
646 |
> |
|
647 |
|
t.start(); |
648 |
< |
try { |
649 |
< |
Thread.sleep(SHORT_DELAY_MS); |
650 |
< |
t.interrupt(); |
769 |
< |
t.join(); |
770 |
< |
} catch (InterruptedException e) { |
771 |
< |
unexpectedException(); |
772 |
< |
} |
648 |
> |
Thread.sleep(SHORT_DELAY_MS); |
649 |
> |
t.interrupt(); |
650 |
> |
t.join(); |
651 |
|
} |
652 |
|
|
653 |
|
/** |
654 |
|
* A waiting acquire(n) blocks interruptibly |
655 |
|
*/ |
656 |
< |
public void testAcquireN_InterruptedException_fair() { |
656 |
> |
public void testAcquireN_InterruptedException_fair() |
657 |
> |
throws InterruptedException { |
658 |
|
final Semaphore s = new Semaphore(2, true); |
659 |
< |
Thread t = new Thread(new Runnable() { |
660 |
< |
public void run() { |
661 |
< |
try { |
662 |
< |
s.acquire(3); |
663 |
< |
threadShouldThrow(); |
785 |
< |
} catch (InterruptedException success) {} |
786 |
< |
} |
787 |
< |
}); |
659 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
660 |
> |
public void realRun() throws InterruptedException { |
661 |
> |
s.acquire(3); |
662 |
> |
}}); |
663 |
> |
|
664 |
|
t.start(); |
665 |
< |
try { |
666 |
< |
Thread.sleep(SHORT_DELAY_MS); |
667 |
< |
t.interrupt(); |
792 |
< |
t.join(); |
793 |
< |
} catch (InterruptedException e) { |
794 |
< |
unexpectedException(); |
795 |
< |
} |
665 |
> |
Thread.sleep(SHORT_DELAY_MS); |
666 |
> |
t.interrupt(); |
667 |
> |
t.join(); |
668 |
|
} |
669 |
|
|
670 |
|
/** |
671 |
|
* A waiting tryAcquire blocks interruptibly |
672 |
|
*/ |
673 |
< |
public void testTryAcquire_InterruptedException_fair() { |
673 |
> |
public void testTryAcquire_InterruptedException_fair() |
674 |
> |
throws InterruptedException { |
675 |
|
final Semaphore s = new Semaphore(0, true); |
676 |
< |
Thread t = new Thread(new Runnable() { |
677 |
< |
public void run() { |
678 |
< |
try { |
679 |
< |
s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
680 |
< |
threadShouldThrow(); |
808 |
< |
} catch (InterruptedException success) { |
809 |
< |
} |
810 |
< |
} |
811 |
< |
}); |
676 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
677 |
> |
public void realRun() throws InterruptedException { |
678 |
> |
s.tryAcquire(MEDIUM_DELAY_MS, MILLISECONDS); |
679 |
> |
}}); |
680 |
> |
|
681 |
|
t.start(); |
682 |
< |
try { |
683 |
< |
Thread.sleep(SHORT_DELAY_MS); |
684 |
< |
t.interrupt(); |
816 |
< |
t.join(); |
817 |
< |
} catch (InterruptedException e) { |
818 |
< |
unexpectedException(); |
819 |
< |
} |
682 |
> |
Thread.sleep(SHORT_DELAY_MS); |
683 |
> |
t.interrupt(); |
684 |
> |
t.join(); |
685 |
|
} |
686 |
|
|
687 |
|
/** |
688 |
|
* A waiting tryAcquire(n) blocks interruptibly |
689 |
|
*/ |
690 |
< |
public void testTryAcquireN_InterruptedException_fair() { |
690 |
> |
public void testTryAcquireN_InterruptedException_fair() |
691 |
> |
throws InterruptedException { |
692 |
|
final Semaphore s = new Semaphore(1, true); |
693 |
< |
Thread t = new Thread(new Runnable() { |
694 |
< |
public void run() { |
695 |
< |
try { |
696 |
< |
s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
697 |
< |
threadShouldThrow(); |
832 |
< |
} catch (InterruptedException success) { |
833 |
< |
} |
834 |
< |
} |
835 |
< |
}); |
693 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
694 |
> |
public void realRun() throws InterruptedException { |
695 |
> |
s.tryAcquire(4, MEDIUM_DELAY_MS, MILLISECONDS); |
696 |
> |
}}); |
697 |
> |
|
698 |
|
t.start(); |
699 |
< |
try { |
700 |
< |
Thread.sleep(SHORT_DELAY_MS); |
701 |
< |
t.interrupt(); |
840 |
< |
t.join(); |
841 |
< |
} catch (InterruptedException e) { |
842 |
< |
unexpectedException(); |
843 |
< |
} |
699 |
> |
Thread.sleep(SHORT_DELAY_MS); |
700 |
> |
t.interrupt(); |
701 |
> |
t.join(); |
702 |
|
} |
703 |
|
|
704 |
|
/** |
705 |
|
* getQueueLength reports number of waiting threads |
706 |
|
*/ |
707 |
< |
public void testGetQueueLength_fair() { |
707 |
> |
public void testGetQueueLength_fair() throws InterruptedException { |
708 |
|
final Semaphore lock = new Semaphore(1, true); |
709 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
710 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
711 |
< |
try { |
712 |
< |
assertEquals(0, lock.getQueueLength()); |
713 |
< |
lock.acquireUninterruptibly(); |
714 |
< |
t1.start(); |
715 |
< |
Thread.sleep(SHORT_DELAY_MS); |
716 |
< |
assertEquals(1, lock.getQueueLength()); |
717 |
< |
t2.start(); |
718 |
< |
Thread.sleep(SHORT_DELAY_MS); |
719 |
< |
assertEquals(2, lock.getQueueLength()); |
720 |
< |
t1.interrupt(); |
721 |
< |
Thread.sleep(SHORT_DELAY_MS); |
722 |
< |
assertEquals(1, lock.getQueueLength()); |
723 |
< |
lock.release(); |
724 |
< |
Thread.sleep(SHORT_DELAY_MS); |
725 |
< |
assertEquals(0, lock.getQueueLength()); |
726 |
< |
t1.join(); |
869 |
< |
t2.join(); |
870 |
< |
} catch (Exception e) { |
871 |
< |
unexpectedException(); |
872 |
< |
} |
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(); |
727 |
|
} |
728 |
|
|
729 |
|
|
730 |
|
/** |
731 |
|
* a deserialized serialized semaphore has same number of permits |
732 |
|
*/ |
733 |
< |
public void testSerialization_fair() { |
733 |
> |
public void testSerialization_fair() throws Exception { |
734 |
|
Semaphore l = new Semaphore(3, true); |
735 |
|
|
736 |
< |
try { |
737 |
< |
l.acquire(); |
738 |
< |
l.release(); |
739 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
740 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
741 |
< |
out.writeObject(l); |
742 |
< |
out.close(); |
743 |
< |
|
744 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
745 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
746 |
< |
Semaphore r = (Semaphore) in.readObject(); |
747 |
< |
assertEquals(3, r.availablePermits()); |
748 |
< |
assertTrue(r.isFair()); |
749 |
< |
r.acquire(); |
896 |
< |
r.release(); |
897 |
< |
} catch (Exception e) { |
898 |
< |
unexpectedException(); |
899 |
< |
} |
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(); |
749 |
> |
r.release(); |
750 |
|
} |
751 |
|
|
752 |
|
/** |