1 |
dl |
1.2 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.24 |
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
dl |
1.2 |
*/ |
6 |
|
|
|
7 |
tim |
1.1 |
package java.util.concurrent; |
8 |
dl |
1.16 |
import java.util.*; |
9 |
dl |
1.5 |
import java.util.concurrent.locks.*; |
10 |
dl |
1.16 |
import java.util.concurrent.atomic.*; |
11 |
tim |
1.1 |
|
12 |
|
|
/** |
13 |
|
|
* A counting semaphore. Conceptually, a semaphore maintains a set of |
14 |
|
|
* permits. Each {@link #acquire} blocks if necessary until a permit is |
15 |
|
|
* available, and then takes it. Each {@link #release} adds a permit, |
16 |
|
|
* potentially releasing a blocking acquirer. |
17 |
|
|
* However, no actual permit objects are used; the <tt>Semaphore</tt> just |
18 |
|
|
* keeps a count of the number available and acts accordingly. |
19 |
|
|
* |
20 |
dl |
1.16 |
* <p>Semaphores are often used to restrict the number of threads than can |
21 |
tim |
1.1 |
* access some (physical or logical) resource. For example, here is |
22 |
|
|
* a class that uses a semaphore to control access to a pool of items: |
23 |
|
|
* <pre> |
24 |
|
|
* class Pool { |
25 |
|
|
* private static final MAX_AVAILABLE = 100; |
26 |
dl |
1.16 |
* private final Semaphore available = new Semaphore(MAX_AVAILABLE, true); |
27 |
tim |
1.1 |
* |
28 |
|
|
* public Object getItem() throws InterruptedException { |
29 |
|
|
* available.acquire(); |
30 |
|
|
* return getNextAvailableItem(); |
31 |
|
|
* } |
32 |
|
|
* |
33 |
|
|
* public void putItem(Object x) { |
34 |
|
|
* if (markAsUnused(x)) |
35 |
|
|
* available.release(); |
36 |
|
|
* } |
37 |
|
|
* |
38 |
|
|
* // Not a particularly efficient data structure; just for demo |
39 |
|
|
* |
40 |
|
|
* protected Object[] items = ... whatever kinds of items being managed |
41 |
|
|
* protected boolean[] used = new boolean[MAX_AVAILABLE]; |
42 |
|
|
* |
43 |
|
|
* protected synchronized Object getNextAvailableItem() { |
44 |
|
|
* for (int i = 0; i < MAX_AVAILABLE; ++i) { |
45 |
|
|
* if (!used[i]) { |
46 |
|
|
* used[i] = true; |
47 |
|
|
* return items[i]; |
48 |
|
|
* } |
49 |
|
|
* } |
50 |
|
|
* return null; // not reached |
51 |
|
|
* } |
52 |
|
|
* |
53 |
|
|
* protected synchronized boolean markAsUnused(Object item) { |
54 |
|
|
* for (int i = 0; i < MAX_AVAILABLE; ++i) { |
55 |
|
|
* if (item == items[i]) { |
56 |
|
|
* if (used[i]) { |
57 |
|
|
* used[i] = false; |
58 |
|
|
* return true; |
59 |
tim |
1.8 |
* } else |
60 |
tim |
1.1 |
* return false; |
61 |
|
|
* } |
62 |
|
|
* } |
63 |
|
|
* return false; |
64 |
|
|
* } |
65 |
|
|
* |
66 |
|
|
* } |
67 |
|
|
* </pre> |
68 |
|
|
* |
69 |
dl |
1.16 |
* <p>Before obtaining an item each thread must acquire a permit from |
70 |
|
|
* the semaphore, guaranteeing that an item is available for use. When |
71 |
|
|
* the thread has finished with the item it is returned back to the |
72 |
|
|
* pool and a permit is returned to the semaphore, allowing another |
73 |
|
|
* thread to acquire that item. Note that no synchronization lock is |
74 |
|
|
* held when {@link #acquire} is called as that would prevent an item |
75 |
|
|
* from being returned to the pool. The semaphore encapsulates the |
76 |
|
|
* synchronization needed to restrict access to the pool, separately |
77 |
|
|
* from any synchronization needed to maintain the consistency of the |
78 |
|
|
* pool itself. |
79 |
|
|
* |
80 |
|
|
* <p>A semaphore initialized to one, and which is used such that it |
81 |
|
|
* only has at most one permit available, can serve as a mutual |
82 |
|
|
* exclusion lock. This is more commonly known as a <em>binary |
83 |
|
|
* semaphore</em>, because it only has two states: one permit |
84 |
|
|
* available, or zero permits available. When used in this way, the |
85 |
|
|
* binary semaphore has the property (unlike many {@link Lock} |
86 |
dl |
1.18 |
* implementations), that the "lock" can be released by a |
87 |
dl |
1.16 |
* thread other than the owner (as semaphores have no notion of |
88 |
|
|
* ownership). This can be useful in some specialized contexts, such |
89 |
|
|
* as deadlock recovery. |
90 |
|
|
* |
91 |
|
|
* <p> The constructor for this class accepts a <em>fairness</em> |
92 |
|
|
* parameter. When set false, this class makes no guarantees about the |
93 |
dl |
1.19 |
* order in which threads acquire permits. In particular, <em>barging</em> is |
94 |
dl |
1.16 |
* permitted, that is, a thread invoking {@link #acquire} can be |
95 |
|
|
* allocated a permit ahead of a thread that has been waiting. When |
96 |
|
|
* fairness is set true, the semaphore guarantees that threads |
97 |
|
|
* invoking any of the {@link #acquire() acquire} methods are |
98 |
|
|
* allocated permits in the order in which their invocation of those |
99 |
|
|
* methods was processed (first-in-first-out; FIFO). Note that FIFO |
100 |
|
|
* ordering necessarily applies to specific internal points of |
101 |
|
|
* execution within these methods. So, it is possible for one thread |
102 |
|
|
* to invoke <tt>acquire</tt> before another, but reach the ordering |
103 |
|
|
* point after the other, and similarly upon return from the method. |
104 |
|
|
* |
105 |
|
|
* <p>Generally, semaphores used to control resource access should be |
106 |
|
|
* initialized as fair, to ensure that no thread is starved out from |
107 |
|
|
* accessing a resource. When using semaphores for other kinds of |
108 |
|
|
* synchronization control, the throughput advantages of non-fair |
109 |
|
|
* ordering often outweigh fairness considerations. |
110 |
|
|
* |
111 |
|
|
* <p>This class also provides convenience methods to {@link |
112 |
|
|
* #acquire(int) acquire} and {@link #release(int) release} multiple |
113 |
|
|
* permits at a time. Beware of the increased risk of indefinite |
114 |
dl |
1.19 |
* postponement when these methods are used without fairness set true. |
115 |
tim |
1.1 |
* |
116 |
|
|
* @since 1.5 |
117 |
dl |
1.4 |
* @author Doug Lea |
118 |
tim |
1.1 |
* |
119 |
|
|
*/ |
120 |
dl |
1.16 |
|
121 |
dl |
1.23 |
public class Semaphore implements java.io.Serializable { |
122 |
dl |
1.16 |
private static final long serialVersionUID = -3222578661600680210L; |
123 |
dl |
1.25 |
/** All mechanics via AbstractQueuedSynchronizer subclass */ |
124 |
dl |
1.23 |
private final Sync sync; |
125 |
|
|
|
126 |
dl |
1.25 |
/** |
127 |
|
|
* Synchronization implementation for semaphore |
128 |
|
|
*/ |
129 |
dl |
1.23 |
private final static class Sync extends AbstractQueuedSynchronizer { |
130 |
|
|
final boolean fair; |
131 |
|
|
Sync(int permits, boolean fair) { |
132 |
|
|
this.fair = fair; |
133 |
dl |
1.25 |
state().set(permits); |
134 |
dl |
1.23 |
} |
135 |
|
|
|
136 |
dl |
1.26 |
public int acquireSharedState(boolean isQueued, int acquires) { |
137 |
dl |
1.25 |
final AtomicInteger perms = state(); |
138 |
|
|
if (!isQueued && fair && hasQueuedThreads()) |
139 |
dl |
1.23 |
return -1; |
140 |
|
|
for (;;) { |
141 |
|
|
int available = perms.get(); |
142 |
|
|
int remaining = available - acquires; |
143 |
|
|
if (remaining < 0 || |
144 |
|
|
perms.compareAndSet(available, remaining)) |
145 |
|
|
return remaining; |
146 |
|
|
} |
147 |
|
|
} |
148 |
|
|
|
149 |
dl |
1.26 |
public boolean releaseSharedState(int releases) { |
150 |
dl |
1.25 |
final AtomicInteger perms = state(); |
151 |
dl |
1.23 |
for (;;) { |
152 |
|
|
int p = perms.get(); |
153 |
|
|
if (perms.compareAndSet(p, p + releases)) |
154 |
|
|
return true; |
155 |
|
|
} |
156 |
|
|
} |
157 |
|
|
|
158 |
dl |
1.26 |
public int acquireExclusiveState(boolean isQueued, int acquires) { |
159 |
dl |
1.23 |
throw new UnsupportedOperationException(); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
public final boolean releaseExclusiveState(int releases) { |
163 |
|
|
throw new UnsupportedOperationException(); |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
public final void checkConditionAccess(Thread thread, boolean waiting) { |
167 |
|
|
throw new UnsupportedOperationException(); |
168 |
|
|
} |
169 |
|
|
} |
170 |
dl |
1.16 |
|
171 |
tim |
1.1 |
/** |
172 |
|
|
* Construct a <tt>Semaphore</tt> with the given number of |
173 |
dl |
1.19 |
* permits and the given fairness setting. |
174 |
dl |
1.16 |
* @param permits the initial number of permits available. This |
175 |
|
|
* value may be negative, in which case releases must |
176 |
|
|
* occur before any acquires will be granted. |
177 |
|
|
* @param fair true if this semaphore will guarantee first-in |
178 |
|
|
* first-out granting of permits under contention, else false. |
179 |
tim |
1.1 |
*/ |
180 |
dl |
1.16 |
public Semaphore(int permits, boolean fair) { |
181 |
dl |
1.23 |
sync = new Sync(permits, fair); |
182 |
tim |
1.1 |
} |
183 |
|
|
|
184 |
|
|
/** |
185 |
|
|
* Acquires a permit from this semaphore, blocking until one is |
186 |
|
|
* available, or the thread is {@link Thread#interrupt interrupted}. |
187 |
|
|
* |
188 |
|
|
* <p>Acquires a permit, if one is available and returns immediately, |
189 |
|
|
* reducing the number of available permits by one. |
190 |
|
|
* <p>If no permit is available then the current thread becomes |
191 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
192 |
|
|
* one of two things happens: |
193 |
|
|
* <ul> |
194 |
|
|
* <li>Some other thread invokes the {@link #release} method for this |
195 |
dl |
1.16 |
* semaphore and the current thread is next to be assigned a permit; or |
196 |
tim |
1.1 |
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
197 |
|
|
* thread. |
198 |
|
|
* </ul> |
199 |
|
|
* |
200 |
|
|
* <p>If the current thread: |
201 |
|
|
* <ul> |
202 |
|
|
* <li>has its interrupted status set on entry to this method; or |
203 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting |
204 |
|
|
* for a permit, |
205 |
|
|
* </ul> |
206 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
207 |
|
|
* interrupted status is cleared. |
208 |
|
|
* |
209 |
|
|
* @throws InterruptedException if the current thread is interrupted |
210 |
|
|
* |
211 |
|
|
* @see Thread#interrupt |
212 |
|
|
*/ |
213 |
dl |
1.2 |
public void acquire() throws InterruptedException { |
214 |
dl |
1.23 |
sync.acquireSharedInterruptibly(1); |
215 |
dl |
1.2 |
} |
216 |
tim |
1.1 |
|
217 |
|
|
/** |
218 |
|
|
* Acquires a permit from this semaphore, blocking until one is |
219 |
|
|
* available. |
220 |
|
|
* |
221 |
|
|
* <p>Acquires a permit, if one is available and returns immediately, |
222 |
|
|
* reducing the number of available permits by one. |
223 |
|
|
* <p>If no permit is available then the current thread becomes |
224 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
225 |
|
|
* some other thread invokes the {@link #release} method for this |
226 |
dl |
1.16 |
* semaphore and the current thread is next to be assigned a permit. |
227 |
tim |
1.1 |
* |
228 |
|
|
* <p>If the current thread |
229 |
|
|
* is {@link Thread#interrupt interrupted} while waiting |
230 |
|
|
* for a permit then it will continue to wait, but the time at which |
231 |
|
|
* the thread is assigned a permit may change compared to the time it |
232 |
|
|
* would have received the permit had no interruption occurred. When the |
233 |
|
|
* thread does return from this method its interrupt status will be set. |
234 |
|
|
* |
235 |
|
|
*/ |
236 |
dl |
1.2 |
public void acquireUninterruptibly() { |
237 |
dl |
1.23 |
sync.acquireSharedUninterruptibly(1); |
238 |
dl |
1.2 |
} |
239 |
tim |
1.1 |
|
240 |
|
|
/** |
241 |
|
|
* Acquires a permit from this semaphore, only if one is available at the |
242 |
|
|
* time of invocation. |
243 |
|
|
* <p>Acquires a permit, if one is available and returns immediately, |
244 |
|
|
* with the value <tt>true</tt>, |
245 |
|
|
* reducing the number of available permits by one. |
246 |
|
|
* |
247 |
|
|
* <p>If no permit is available then this method will return |
248 |
|
|
* immediately with the value <tt>false</tt>. |
249 |
|
|
* |
250 |
|
|
* @return <tt>true</tt> if a permit was acquired and <tt>false</tt> |
251 |
|
|
* otherwise. |
252 |
|
|
*/ |
253 |
|
|
public boolean tryAcquire() { |
254 |
dl |
1.26 |
return sync.acquireSharedState(false, 1) >= 0; |
255 |
tim |
1.1 |
} |
256 |
|
|
|
257 |
|
|
/** |
258 |
|
|
* Acquires a permit from this semaphore, if one becomes available |
259 |
|
|
* within the given waiting time and the |
260 |
|
|
* current thread has not been {@link Thread#interrupt interrupted}. |
261 |
|
|
* <p>Acquires a permit, if one is available and returns immediately, |
262 |
|
|
* with the value <tt>true</tt>, |
263 |
|
|
* reducing the number of available permits by one. |
264 |
|
|
* <p>If no permit is available then |
265 |
|
|
* the current thread becomes disabled for thread scheduling |
266 |
|
|
* purposes and lies dormant until one of three things happens: |
267 |
|
|
* <ul> |
268 |
|
|
* <li>Some other thread invokes the {@link #release} method for this |
269 |
dl |
1.16 |
* semaphore and the current thread is next to be assigned a permit; or |
270 |
tim |
1.1 |
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
271 |
|
|
* thread; or |
272 |
|
|
* <li>The specified waiting time elapses. |
273 |
|
|
* </ul> |
274 |
|
|
* <p>If a permit is acquired then the value <tt>true</tt> is returned. |
275 |
|
|
* <p>If the current thread: |
276 |
|
|
* <ul> |
277 |
|
|
* <li>has its interrupted status set on entry to this method; or |
278 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting to acquire |
279 |
|
|
* a permit, |
280 |
|
|
* </ul> |
281 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
282 |
|
|
* interrupted status is cleared. |
283 |
|
|
* <p>If the specified waiting time elapses then the value <tt>false</tt> |
284 |
|
|
* is returned. |
285 |
dl |
1.16 |
* If the time is less than or equal to zero, the method will not wait |
286 |
|
|
* at all. |
287 |
tim |
1.1 |
* |
288 |
|
|
* @param timeout the maximum time to wait for a permit |
289 |
dl |
1.13 |
* @param unit the time unit of the <tt>timeout</tt> argument. |
290 |
tim |
1.1 |
* @return <tt>true</tt> if a permit was acquired and <tt>false</tt> |
291 |
|
|
* if the waiting time elapsed before a permit was acquired. |
292 |
|
|
* |
293 |
|
|
* @throws InterruptedException if the current thread is interrupted |
294 |
|
|
* |
295 |
|
|
* @see Thread#interrupt |
296 |
|
|
* |
297 |
|
|
*/ |
298 |
dl |
1.16 |
public boolean tryAcquire(long timeout, TimeUnit unit) |
299 |
tim |
1.1 |
throws InterruptedException { |
300 |
dl |
1.23 |
return sync.acquireSharedTimed(1, unit.toNanos(timeout)); |
301 |
tim |
1.1 |
} |
302 |
|
|
|
303 |
|
|
/** |
304 |
|
|
* Releases a permit, returning it to the semaphore. |
305 |
|
|
* <p>Releases a permit, increasing the number of available permits |
306 |
|
|
* by one. |
307 |
|
|
* If any threads are blocking trying to acquire a permit, then one |
308 |
|
|
* is selected and given the permit that was just released. |
309 |
|
|
* That thread is re-enabled for thread scheduling purposes. |
310 |
|
|
* <p>There is no requirement that a thread that releases a permit must |
311 |
|
|
* have acquired that permit by calling {@link #acquire}. |
312 |
|
|
* Correct usage of a semaphore is established by programming convention |
313 |
|
|
* in the application. |
314 |
|
|
*/ |
315 |
dl |
1.2 |
public void release() { |
316 |
dl |
1.23 |
sync.releaseShared(1); |
317 |
dl |
1.16 |
} |
318 |
|
|
|
319 |
|
|
/** |
320 |
|
|
* Acquires the given number of permits from this semaphore, |
321 |
|
|
* blocking until all are available, |
322 |
|
|
* or the thread is {@link Thread#interrupt interrupted}. |
323 |
|
|
* |
324 |
|
|
* <p>Acquires the given number of permits, if they are available, |
325 |
|
|
* and returns immediately, |
326 |
|
|
* reducing the number of available permits by the given amount. |
327 |
|
|
* |
328 |
|
|
* <p>If insufficient permits are available then the current thread becomes |
329 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
330 |
|
|
* one of two things happens: |
331 |
|
|
* <ul> |
332 |
|
|
* <li>Some other thread invokes one of the {@link #release() release} |
333 |
|
|
* methods for this semaphore, the current thread is next to be assigned |
334 |
|
|
* permits and the number of available permits satisfies this request; or |
335 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
336 |
|
|
* thread. |
337 |
|
|
* </ul> |
338 |
|
|
* |
339 |
|
|
* <p>If the current thread: |
340 |
|
|
* <ul> |
341 |
|
|
* <li>has its interrupted status set on entry to this method; or |
342 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting |
343 |
|
|
* for a permit, |
344 |
|
|
* </ul> |
345 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
346 |
|
|
* interrupted status is cleared. |
347 |
dl |
1.19 |
* Any permits that were to be assigned to this thread are instead |
348 |
dl |
1.16 |
* assigned to the next waiting thread(s), as if |
349 |
|
|
* they had been made available by a call to {@link #release()}. |
350 |
|
|
* |
351 |
|
|
* @param permits the number of permits to acquire |
352 |
|
|
* |
353 |
|
|
* @throws InterruptedException if the current thread is interrupted |
354 |
|
|
* @throws IllegalArgumentException if permits less than zero. |
355 |
|
|
* |
356 |
|
|
* @see Thread#interrupt |
357 |
|
|
*/ |
358 |
|
|
public void acquire(int permits) throws InterruptedException { |
359 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
360 |
dl |
1.23 |
sync.acquireSharedInterruptibly(permits); |
361 |
dl |
1.16 |
} |
362 |
|
|
|
363 |
|
|
/** |
364 |
|
|
* Acquires the given number of permits from this semaphore, |
365 |
|
|
* blocking until all are available. |
366 |
|
|
* |
367 |
|
|
* <p>Acquires the given number of permits, if they are available, |
368 |
|
|
* and returns immediately, |
369 |
|
|
* reducing the number of available permits by the given amount. |
370 |
|
|
* |
371 |
|
|
* <p>If insufficient permits are available then the current thread becomes |
372 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
373 |
|
|
* some other thread invokes one of the {@link #release() release} |
374 |
|
|
* methods for this semaphore, the current thread is next to be assigned |
375 |
|
|
* permits and the number of available permits satisfies this request. |
376 |
|
|
* |
377 |
|
|
* <p>If the current thread |
378 |
|
|
* is {@link Thread#interrupt interrupted} while waiting |
379 |
|
|
* for permits then it will continue to wait and its position in the |
380 |
|
|
* queue is not affected. When the |
381 |
|
|
* thread does return from this method its interrupt status will be set. |
382 |
|
|
* |
383 |
|
|
* @param permits the number of permits to acquire |
384 |
|
|
* @throws IllegalArgumentException if permits less than zero. |
385 |
|
|
* |
386 |
|
|
*/ |
387 |
|
|
public void acquireUninterruptibly(int permits) { |
388 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
389 |
dl |
1.23 |
sync.acquireSharedUninterruptibly(permits); |
390 |
dl |
1.16 |
} |
391 |
|
|
|
392 |
|
|
/** |
393 |
|
|
* Acquires the given number of permits from this semaphore, only if |
394 |
|
|
* all are available at the time of invocation. |
395 |
|
|
* <p>Acquires the given number of permits, if they are available, and |
396 |
|
|
* returns immediately, with the value <tt>true</tt>, |
397 |
|
|
* reducing the number of available permits by the given amount. |
398 |
|
|
* |
399 |
|
|
* <p>If insufficient permits are available then this method will return |
400 |
|
|
* immediately with the value <tt>false</tt> and the number of available |
401 |
|
|
* permits is unchanged. |
402 |
|
|
* |
403 |
|
|
* @param permits the number of permits to acquire |
404 |
|
|
* |
405 |
|
|
* @return <tt>true</tt> if the permits were acquired and <tt>false</tt> |
406 |
|
|
* otherwise. |
407 |
|
|
* @throws IllegalArgumentException if permits less than zero. |
408 |
|
|
*/ |
409 |
|
|
public boolean tryAcquire(int permits) { |
410 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
411 |
dl |
1.26 |
return sync.acquireSharedState(false, permits) >= 0; |
412 |
dl |
1.16 |
} |
413 |
|
|
|
414 |
|
|
/** |
415 |
|
|
* Acquires the given number of permits from this semaphore, if all |
416 |
|
|
* become available within the given waiting time and the |
417 |
|
|
* current thread has not been {@link Thread#interrupt interrupted}. |
418 |
|
|
* <p>Acquires the given number of permits, if they are available and |
419 |
|
|
* returns immediately, with the value <tt>true</tt>, |
420 |
|
|
* reducing the number of available permits by the given amount. |
421 |
|
|
* <p>If insufficient permits are available then |
422 |
|
|
* the current thread becomes disabled for thread scheduling |
423 |
|
|
* purposes and lies dormant until one of three things happens: |
424 |
|
|
* <ul> |
425 |
|
|
* <li>Some other thread invokes one of the {@link #release() release} |
426 |
|
|
* methods for this semaphore, the current thread is next to be assigned |
427 |
|
|
* permits and the number of available permits satisfies this request; or |
428 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
429 |
|
|
* thread; or |
430 |
|
|
* <li>The specified waiting time elapses. |
431 |
|
|
* </ul> |
432 |
|
|
* <p>If the permits are acquired then the value <tt>true</tt> is returned. |
433 |
|
|
* <p>If the current thread: |
434 |
|
|
* <ul> |
435 |
|
|
* <li>has its interrupted status set on entry to this method; or |
436 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting to acquire |
437 |
|
|
* the permits, |
438 |
|
|
* </ul> |
439 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
440 |
|
|
* interrupted status is cleared. |
441 |
|
|
* Any permits that were to be assigned to this thread, are instead |
442 |
|
|
* assigned to the next waiting thread(s), as if |
443 |
|
|
* they had been made available by a call to {@link #release()}. |
444 |
|
|
* |
445 |
|
|
* <p>If the specified waiting time elapses then the value <tt>false</tt> |
446 |
|
|
* is returned. |
447 |
|
|
* If the time is |
448 |
|
|
* less than or equal to zero, the method will not wait at all. |
449 |
|
|
* Any permits that were to be assigned to this thread, are instead |
450 |
|
|
* assigned to the next waiting thread(s), as if |
451 |
|
|
* they had been made available by a call to {@link #release()}. |
452 |
|
|
* |
453 |
|
|
* @param permits the number of permits to acquire |
454 |
|
|
* @param timeout the maximum time to wait for the permits |
455 |
|
|
* @param unit the time unit of the <tt>timeout</tt> argument. |
456 |
|
|
* @return <tt>true</tt> if all permits were acquired and <tt>false</tt> |
457 |
|
|
* if the waiting time elapsed before all permits were acquired. |
458 |
|
|
* |
459 |
|
|
* @throws InterruptedException if the current thread is interrupted |
460 |
|
|
* @throws IllegalArgumentException if permits less than zero. |
461 |
|
|
* |
462 |
|
|
* @see Thread#interrupt |
463 |
|
|
* |
464 |
|
|
*/ |
465 |
|
|
public boolean tryAcquire(int permits, long timeout, TimeUnit unit) |
466 |
|
|
throws InterruptedException { |
467 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
468 |
dl |
1.23 |
return sync.acquireSharedTimed(permits, unit.toNanos(timeout)); |
469 |
dl |
1.16 |
} |
470 |
|
|
|
471 |
|
|
|
472 |
|
|
/** |
473 |
|
|
* Releases the given number of permits, returning them to the semaphore. |
474 |
|
|
* <p>Releases the given number of permits, increasing the number of |
475 |
|
|
* available permits by that amount. |
476 |
|
|
* If any threads are blocking trying to acquire permits, then the |
477 |
dl |
1.19 |
* one that has been waiting the longest |
478 |
dl |
1.16 |
* is selected and given the permits that were just released. |
479 |
|
|
* If the number of available permits satisfies that thread's request |
480 |
|
|
* then that thread is re-enabled for thread scheduling purposes; otherwise |
481 |
|
|
* the thread continues to wait. If there are still permits available |
482 |
|
|
* after the first thread's request has been satisfied, then those permits |
483 |
|
|
* are assigned to the next waiting thread. If it is satisfied then it is |
484 |
|
|
* re-enabled for thread scheduling purposes. This continues until there |
485 |
|
|
* are insufficient permits to satisfy the next waiting thread, or there |
486 |
|
|
* are no more waiting threads. |
487 |
|
|
* |
488 |
|
|
* <p>There is no requirement that a thread that releases a permit must |
489 |
|
|
* have acquired that permit by calling {@link Semaphore#acquire acquire}. |
490 |
|
|
* Correct usage of a semaphore is established by programming convention |
491 |
|
|
* in the application. |
492 |
|
|
* |
493 |
|
|
* @param permits the number of permits to release |
494 |
|
|
* @throws IllegalArgumentException if permits less than zero. |
495 |
|
|
*/ |
496 |
|
|
public void release(int permits) { |
497 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
498 |
dl |
1.23 |
sync.releaseShared(permits); |
499 |
dl |
1.2 |
} |
500 |
tim |
1.1 |
|
501 |
|
|
/** |
502 |
|
|
* Return the current number of permits available in this semaphore. |
503 |
|
|
* <p>This method is typically used for debugging and testing purposes. |
504 |
|
|
* @return the number of permits available in this semaphore. |
505 |
|
|
*/ |
506 |
dl |
1.16 |
public int availablePermits() { |
507 |
dl |
1.25 |
return sync.state().get(); |
508 |
tim |
1.1 |
} |
509 |
dl |
1.15 |
|
510 |
|
|
/** |
511 |
|
|
* Shrink the number of available permits by the indicated |
512 |
|
|
* reduction. This method can be useful in subclasses that |
513 |
|
|
* use semaphores to track available resources that become |
514 |
|
|
* unavailable. This method differs from <tt>acquire</tt> |
515 |
|
|
* in that it does not block waiting for permits to become |
516 |
|
|
* available. |
517 |
|
|
* @param reduction the number of permits to remove |
518 |
|
|
* @throws IllegalArgumentException if reduction is negative |
519 |
|
|
*/ |
520 |
dl |
1.16 |
protected void reducePermits(int reduction) { |
521 |
dl |
1.15 |
if (reduction < 0) throw new IllegalArgumentException(); |
522 |
dl |
1.25 |
sync.state().getAndAdd(-reduction); |
523 |
dl |
1.16 |
} |
524 |
|
|
|
525 |
|
|
/** |
526 |
|
|
* Return true if this semaphore has fairness set true. |
527 |
|
|
* @return true if this semaphore has fairness set true. |
528 |
|
|
*/ |
529 |
|
|
public boolean isFair() { |
530 |
dl |
1.23 |
return sync.fair; |
531 |
dl |
1.22 |
} |
532 |
|
|
|
533 |
|
|
|
534 |
|
|
/** |
535 |
dl |
1.23 |
* Queries whether any threads are waiting to acquire. Note that |
536 |
|
|
* because cancellations may occur at any time, a <tt>true</tt> |
537 |
|
|
* return does not guarantee that any other thread will ever |
538 |
|
|
* acquire. This method is designed primarily for use in |
539 |
|
|
* monitoring of the system state. |
540 |
|
|
* |
541 |
|
|
* @return true if there may be other threads waiting to acquire |
542 |
|
|
* the lock. |
543 |
dl |
1.22 |
*/ |
544 |
dl |
1.25 |
public final boolean hasQueuedThreads() { |
545 |
|
|
return sync.hasQueuedThreads(); |
546 |
dl |
1.22 |
} |
547 |
|
|
|
548 |
|
|
|
549 |
|
|
/** |
550 |
dl |
1.23 |
* Returns an estimate of the number of threads waiting to |
551 |
|
|
* acquire. The value is only an estimate because the number of |
552 |
|
|
* threads may change dynamically while this method traverses |
553 |
|
|
* internal data structures. This method is designed for use in |
554 |
|
|
* monitoring of the system state, not for synchronization |
555 |
|
|
* control. |
556 |
|
|
* @return the estimated number of threads waiting for this lock |
557 |
dl |
1.22 |
*/ |
558 |
dl |
1.23 |
public final int getQueueLength() { |
559 |
|
|
return sync.getQueueLength(); |
560 |
dl |
1.22 |
} |
561 |
|
|
|
562 |
|
|
/** |
563 |
dl |
1.23 |
* Returns a collection containing threads that may be waiting to |
564 |
|
|
* acquire. Because the actual set of threads may change |
565 |
|
|
* dynamically while constructing this result, the returned |
566 |
|
|
* collection is only a best-effort estimate. The elements of the |
567 |
|
|
* returned collection are in no particular order. This method is |
568 |
|
|
* designed to facilitate construction of subclasses that provide |
569 |
|
|
* more extensive monitoring facilities. |
570 |
|
|
* @return the collection of threads |
571 |
dl |
1.22 |
*/ |
572 |
dl |
1.23 |
protected Collection<Thread> getQueuedThreads() { |
573 |
|
|
return sync.getQueuedThreads(); |
574 |
dl |
1.16 |
} |
575 |
dl |
1.23 |
|
576 |
tim |
1.1 |
} |