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 |
dl |
1.34 |
* Synchronization implementation for semaphore. Uses AQS state |
128 |
|
|
* to represent permits. Subclassed into fair and nonfair |
129 |
|
|
* versions. |
130 |
dl |
1.25 |
*/ |
131 |
dl |
1.34 |
abstract static class Sync extends AbstractQueuedSynchronizer { |
132 |
|
|
Sync(int permits) { |
133 |
dl |
1.29 |
setState(permits); |
134 |
dl |
1.23 |
} |
135 |
|
|
|
136 |
dl |
1.34 |
final int getPermits() { |
137 |
dl |
1.29 |
return getState(); |
138 |
|
|
} |
139 |
|
|
|
140 |
dl |
1.34 |
final int nonfairTryAcquireShared(int acquires) { |
141 |
dl |
1.23 |
for (;;) { |
142 |
dl |
1.29 |
int available = getState(); |
143 |
dl |
1.23 |
int remaining = available - acquires; |
144 |
|
|
if (remaining < 0 || |
145 |
dl |
1.29 |
compareAndSetState(available, remaining)) |
146 |
dl |
1.23 |
return remaining; |
147 |
|
|
} |
148 |
|
|
} |
149 |
|
|
|
150 |
dl |
1.34 |
protected final boolean tryReleaseShared(int releases) { |
151 |
dl |
1.23 |
for (;;) { |
152 |
dl |
1.29 |
int p = getState(); |
153 |
|
|
if (compareAndSetState(p, p + releases)) |
154 |
dl |
1.23 |
return true; |
155 |
|
|
} |
156 |
|
|
} |
157 |
dl |
1.29 |
|
158 |
dl |
1.34 |
final void reducePermits(int reductions) { |
159 |
dl |
1.29 |
for (;;) { |
160 |
|
|
int current = getState(); |
161 |
|
|
int next = current - reductions; |
162 |
|
|
if (compareAndSetState(current, next)) |
163 |
|
|
return; |
164 |
|
|
} |
165 |
|
|
} |
166 |
dl |
1.31 |
|
167 |
dl |
1.34 |
final int drainPermits() { |
168 |
dl |
1.31 |
for (;;) { |
169 |
|
|
int current = getState(); |
170 |
|
|
if (current == 0 || compareAndSetState(current, 0)) |
171 |
|
|
return current; |
172 |
|
|
} |
173 |
|
|
} |
174 |
dl |
1.23 |
} |
175 |
dl |
1.16 |
|
176 |
tim |
1.1 |
/** |
177 |
dl |
1.34 |
* NonFair version |
178 |
|
|
*/ |
179 |
|
|
final static class NonfairSync extends Sync { |
180 |
|
|
NonfairSync(int permits) { |
181 |
|
|
super(permits); |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
protected int tryAcquireShared(int acquires) { |
185 |
|
|
return nonfairTryAcquireShared(acquires); |
186 |
|
|
} |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
/** |
190 |
|
|
* Fair version |
191 |
|
|
*/ |
192 |
|
|
final static class FairSync extends Sync { |
193 |
|
|
FairSync(int permits) { |
194 |
|
|
super(permits); |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
protected int tryAcquireShared(int acquires) { |
198 |
|
|
Thread current = Thread.currentThread(); |
199 |
|
|
for (;;) { |
200 |
|
|
if (!isFirst(current)) |
201 |
|
|
return -1; |
202 |
|
|
int available = getState(); |
203 |
|
|
int remaining = available - acquires; |
204 |
|
|
if (remaining < 0 || |
205 |
|
|
compareAndSetState(available, remaining)) |
206 |
|
|
return remaining; |
207 |
|
|
} |
208 |
|
|
} |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
/** |
212 |
tim |
1.1 |
* Construct a <tt>Semaphore</tt> with the given number of |
213 |
dl |
1.19 |
* permits and the given fairness setting. |
214 |
dl |
1.16 |
* @param permits the initial number of permits available. This |
215 |
|
|
* value may be negative, in which case releases must |
216 |
|
|
* occur before any acquires will be granted. |
217 |
|
|
* @param fair true if this semaphore will guarantee first-in |
218 |
|
|
* first-out granting of permits under contention, else false. |
219 |
tim |
1.1 |
*/ |
220 |
dl |
1.16 |
public Semaphore(int permits, boolean fair) { |
221 |
dl |
1.34 |
sync = (fair)? new FairSync(permits) : new NonfairSync(permits); |
222 |
tim |
1.1 |
} |
223 |
|
|
|
224 |
|
|
/** |
225 |
|
|
* Acquires a permit from this semaphore, blocking until one is |
226 |
|
|
* available, or the thread is {@link Thread#interrupt interrupted}. |
227 |
|
|
* |
228 |
|
|
* <p>Acquires a permit, if one is available and returns immediately, |
229 |
|
|
* reducing the number of available permits by one. |
230 |
|
|
* <p>If no permit is available then the current thread becomes |
231 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
232 |
|
|
* one of two things happens: |
233 |
|
|
* <ul> |
234 |
|
|
* <li>Some other thread invokes the {@link #release} method for this |
235 |
dl |
1.16 |
* semaphore and the current thread is next to be assigned a permit; or |
236 |
tim |
1.1 |
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
237 |
|
|
* thread. |
238 |
|
|
* </ul> |
239 |
|
|
* |
240 |
|
|
* <p>If the current thread: |
241 |
|
|
* <ul> |
242 |
|
|
* <li>has its interrupted status set on entry to this method; or |
243 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting |
244 |
|
|
* for a permit, |
245 |
|
|
* </ul> |
246 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
247 |
|
|
* interrupted status is cleared. |
248 |
|
|
* |
249 |
|
|
* @throws InterruptedException if the current thread is interrupted |
250 |
|
|
* |
251 |
|
|
* @see Thread#interrupt |
252 |
|
|
*/ |
253 |
dl |
1.2 |
public void acquire() throws InterruptedException { |
254 |
dl |
1.23 |
sync.acquireSharedInterruptibly(1); |
255 |
dl |
1.2 |
} |
256 |
tim |
1.1 |
|
257 |
|
|
/** |
258 |
|
|
* Acquires a permit from this semaphore, blocking until one is |
259 |
|
|
* available. |
260 |
|
|
* |
261 |
|
|
* <p>Acquires a permit, if one is available and returns immediately, |
262 |
|
|
* reducing the number of available permits by one. |
263 |
|
|
* <p>If no permit is available then the current thread becomes |
264 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
265 |
|
|
* some other thread invokes the {@link #release} method for this |
266 |
dl |
1.16 |
* semaphore and the current thread is next to be assigned a permit. |
267 |
tim |
1.1 |
* |
268 |
|
|
* <p>If the current thread |
269 |
|
|
* is {@link Thread#interrupt interrupted} while waiting |
270 |
|
|
* for a permit then it will continue to wait, but the time at which |
271 |
|
|
* the thread is assigned a permit may change compared to the time it |
272 |
|
|
* would have received the permit had no interruption occurred. When the |
273 |
|
|
* thread does return from this method its interrupt status will be set. |
274 |
|
|
* |
275 |
|
|
*/ |
276 |
dl |
1.2 |
public void acquireUninterruptibly() { |
277 |
dl |
1.23 |
sync.acquireSharedUninterruptibly(1); |
278 |
dl |
1.2 |
} |
279 |
tim |
1.1 |
|
280 |
|
|
/** |
281 |
|
|
* Acquires a permit from this semaphore, only if one is available at the |
282 |
|
|
* time of invocation. |
283 |
|
|
* <p>Acquires a permit, if one is available and returns immediately, |
284 |
|
|
* with the value <tt>true</tt>, |
285 |
|
|
* reducing the number of available permits by one. |
286 |
|
|
* |
287 |
|
|
* <p>If no permit is available then this method will return |
288 |
|
|
* immediately with the value <tt>false</tt>. |
289 |
|
|
* |
290 |
dl |
1.27 |
* <p>Even when this semaphore has been set to use a |
291 |
|
|
* fair ordering policy, a call to <tt>tryAcquire()</tt> <em>will</em> |
292 |
|
|
* immediately acquire a permit if one is available, whether or not |
293 |
|
|
* other threads are currently waiting. |
294 |
|
|
* This "barging" behavior can be useful in certain |
295 |
|
|
* circumstances, even though it breaks fairness. If you want to honor |
296 |
|
|
* the fairness setting, then use |
297 |
|
|
* {@link #tryAcquire(long, TimeUnit) tryAcquire(0, TimeUnit.SECONDS) } |
298 |
|
|
* which is almost equivalent (it also detects interruption). |
299 |
|
|
* |
300 |
tim |
1.1 |
* @return <tt>true</tt> if a permit was acquired and <tt>false</tt> |
301 |
|
|
* otherwise. |
302 |
|
|
*/ |
303 |
|
|
public boolean tryAcquire() { |
304 |
dl |
1.34 |
return sync.nonfairTryAcquireShared(1) >= 0; |
305 |
tim |
1.1 |
} |
306 |
|
|
|
307 |
|
|
/** |
308 |
|
|
* Acquires a permit from this semaphore, if one becomes available |
309 |
|
|
* within the given waiting time and the |
310 |
|
|
* current thread has not been {@link Thread#interrupt interrupted}. |
311 |
|
|
* <p>Acquires a permit, if one is available and returns immediately, |
312 |
|
|
* with the value <tt>true</tt>, |
313 |
|
|
* reducing the number of available permits by one. |
314 |
|
|
* <p>If no permit is available then |
315 |
|
|
* the current thread becomes disabled for thread scheduling |
316 |
|
|
* purposes and lies dormant until one of three things happens: |
317 |
|
|
* <ul> |
318 |
|
|
* <li>Some other thread invokes the {@link #release} method for this |
319 |
dl |
1.16 |
* semaphore and the current thread is next to be assigned a permit; or |
320 |
tim |
1.1 |
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
321 |
|
|
* thread; or |
322 |
|
|
* <li>The specified waiting time elapses. |
323 |
|
|
* </ul> |
324 |
|
|
* <p>If a permit is acquired then the value <tt>true</tt> is returned. |
325 |
|
|
* <p>If the current thread: |
326 |
|
|
* <ul> |
327 |
|
|
* <li>has its interrupted status set on entry to this method; or |
328 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting to acquire |
329 |
|
|
* a permit, |
330 |
|
|
* </ul> |
331 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
332 |
|
|
* interrupted status is cleared. |
333 |
|
|
* <p>If the specified waiting time elapses then the value <tt>false</tt> |
334 |
|
|
* is returned. |
335 |
dl |
1.16 |
* If the time is less than or equal to zero, the method will not wait |
336 |
|
|
* at all. |
337 |
tim |
1.1 |
* |
338 |
|
|
* @param timeout the maximum time to wait for a permit |
339 |
dl |
1.13 |
* @param unit the time unit of the <tt>timeout</tt> argument. |
340 |
tim |
1.1 |
* @return <tt>true</tt> if a permit was acquired and <tt>false</tt> |
341 |
|
|
* if the waiting time elapsed before a permit was acquired. |
342 |
|
|
* |
343 |
|
|
* @throws InterruptedException if the current thread is interrupted |
344 |
|
|
* |
345 |
|
|
* @see Thread#interrupt |
346 |
|
|
* |
347 |
|
|
*/ |
348 |
dl |
1.16 |
public boolean tryAcquire(long timeout, TimeUnit unit) |
349 |
tim |
1.1 |
throws InterruptedException { |
350 |
dl |
1.23 |
return sync.acquireSharedTimed(1, unit.toNanos(timeout)); |
351 |
tim |
1.1 |
} |
352 |
|
|
|
353 |
|
|
/** |
354 |
|
|
* Releases a permit, returning it to the semaphore. |
355 |
|
|
* <p>Releases a permit, increasing the number of available permits |
356 |
|
|
* by one. |
357 |
|
|
* If any threads are blocking trying to acquire a permit, then one |
358 |
|
|
* is selected and given the permit that was just released. |
359 |
|
|
* That thread is re-enabled for thread scheduling purposes. |
360 |
|
|
* <p>There is no requirement that a thread that releases a permit must |
361 |
|
|
* have acquired that permit by calling {@link #acquire}. |
362 |
|
|
* Correct usage of a semaphore is established by programming convention |
363 |
|
|
* in the application. |
364 |
|
|
*/ |
365 |
dl |
1.2 |
public void release() { |
366 |
dl |
1.23 |
sync.releaseShared(1); |
367 |
dl |
1.16 |
} |
368 |
|
|
|
369 |
|
|
/** |
370 |
|
|
* Acquires the given number of permits from this semaphore, |
371 |
|
|
* blocking until all are available, |
372 |
|
|
* or the thread is {@link Thread#interrupt interrupted}. |
373 |
|
|
* |
374 |
|
|
* <p>Acquires the given number of permits, if they are available, |
375 |
|
|
* and returns immediately, |
376 |
|
|
* reducing the number of available permits by the given amount. |
377 |
|
|
* |
378 |
|
|
* <p>If insufficient permits are available then the current thread becomes |
379 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
380 |
|
|
* one of two things happens: |
381 |
|
|
* <ul> |
382 |
|
|
* <li>Some other thread invokes one of the {@link #release() release} |
383 |
|
|
* methods for this semaphore, the current thread is next to be assigned |
384 |
|
|
* permits and the number of available permits satisfies this request; or |
385 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
386 |
|
|
* thread. |
387 |
|
|
* </ul> |
388 |
|
|
* |
389 |
|
|
* <p>If the current thread: |
390 |
|
|
* <ul> |
391 |
|
|
* <li>has its interrupted status set on entry to this method; or |
392 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting |
393 |
|
|
* for a permit, |
394 |
|
|
* </ul> |
395 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
396 |
|
|
* interrupted status is cleared. |
397 |
dl |
1.19 |
* Any permits that were to be assigned to this thread are instead |
398 |
dl |
1.16 |
* assigned to the next waiting thread(s), as if |
399 |
|
|
* they had been made available by a call to {@link #release()}. |
400 |
|
|
* |
401 |
|
|
* @param permits the number of permits to acquire |
402 |
|
|
* |
403 |
|
|
* @throws InterruptedException if the current thread is interrupted |
404 |
|
|
* @throws IllegalArgumentException if permits less than zero. |
405 |
|
|
* |
406 |
|
|
* @see Thread#interrupt |
407 |
|
|
*/ |
408 |
|
|
public void acquire(int permits) throws InterruptedException { |
409 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
410 |
dl |
1.23 |
sync.acquireSharedInterruptibly(permits); |
411 |
dl |
1.16 |
} |
412 |
|
|
|
413 |
|
|
/** |
414 |
|
|
* Acquires the given number of permits from this semaphore, |
415 |
|
|
* blocking until all are available. |
416 |
|
|
* |
417 |
|
|
* <p>Acquires the given number of permits, if they are available, |
418 |
|
|
* and returns immediately, |
419 |
|
|
* reducing the number of available permits by the given amount. |
420 |
|
|
* |
421 |
|
|
* <p>If insufficient permits are available then the current thread becomes |
422 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
423 |
|
|
* some other thread invokes one of the {@link #release() release} |
424 |
|
|
* methods for this semaphore, the current thread is next to be assigned |
425 |
|
|
* permits and the number of available permits satisfies this request. |
426 |
|
|
* |
427 |
|
|
* <p>If the current thread |
428 |
|
|
* is {@link Thread#interrupt interrupted} while waiting |
429 |
|
|
* for permits then it will continue to wait and its position in the |
430 |
|
|
* queue is not affected. When the |
431 |
|
|
* thread does return from this method its interrupt status will be set. |
432 |
|
|
* |
433 |
|
|
* @param permits the number of permits to acquire |
434 |
|
|
* @throws IllegalArgumentException if permits less than zero. |
435 |
|
|
* |
436 |
|
|
*/ |
437 |
|
|
public void acquireUninterruptibly(int permits) { |
438 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
439 |
dl |
1.23 |
sync.acquireSharedUninterruptibly(permits); |
440 |
dl |
1.16 |
} |
441 |
|
|
|
442 |
|
|
/** |
443 |
|
|
* Acquires the given number of permits from this semaphore, only if |
444 |
|
|
* all are available at the time of invocation. |
445 |
|
|
* <p>Acquires the given number of permits, if they are available, and |
446 |
|
|
* returns immediately, with the value <tt>true</tt>, |
447 |
|
|
* reducing the number of available permits by the given amount. |
448 |
|
|
* |
449 |
|
|
* <p>If insufficient permits are available then this method will return |
450 |
|
|
* immediately with the value <tt>false</tt> and the number of available |
451 |
|
|
* permits is unchanged. |
452 |
|
|
* |
453 |
dl |
1.27 |
* <p>Even when this semaphore has been set to use a fair ordering |
454 |
|
|
* policy, a call to <tt>tryAcquire</tt> <em>will</em> |
455 |
|
|
* immediately acquire a permit if one is available, whether or |
456 |
|
|
* not other threads are currently waiting. This |
457 |
|
|
* "barging" behavior can be useful in certain |
458 |
|
|
* circumstances, even though it breaks fairness. If you want to |
459 |
|
|
* honor the fairness setting, then use {@link #tryAcquire(int, |
460 |
|
|
* long, TimeUnit) tryAcquire(permits, 0, TimeUnit.SECONDS) } |
461 |
|
|
* which is almost equivalent (it also detects interruption). |
462 |
|
|
* |
463 |
dl |
1.16 |
* @param permits the number of permits to acquire |
464 |
|
|
* |
465 |
|
|
* @return <tt>true</tt> if the permits were acquired and <tt>false</tt> |
466 |
|
|
* otherwise. |
467 |
|
|
* @throws IllegalArgumentException if permits less than zero. |
468 |
|
|
*/ |
469 |
|
|
public boolean tryAcquire(int permits) { |
470 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
471 |
dl |
1.34 |
return sync.nonfairTryAcquireShared(permits) >= 0; |
472 |
dl |
1.16 |
} |
473 |
|
|
|
474 |
|
|
/** |
475 |
|
|
* Acquires the given number of permits from this semaphore, if all |
476 |
|
|
* become available within the given waiting time and the |
477 |
|
|
* current thread has not been {@link Thread#interrupt interrupted}. |
478 |
|
|
* <p>Acquires the given number of permits, if they are available and |
479 |
|
|
* returns immediately, with the value <tt>true</tt>, |
480 |
|
|
* reducing the number of available permits by the given amount. |
481 |
|
|
* <p>If insufficient permits are available then |
482 |
|
|
* the current thread becomes disabled for thread scheduling |
483 |
|
|
* purposes and lies dormant until one of three things happens: |
484 |
|
|
* <ul> |
485 |
|
|
* <li>Some other thread invokes one of the {@link #release() release} |
486 |
|
|
* methods for this semaphore, the current thread is next to be assigned |
487 |
|
|
* permits and the number of available permits satisfies this request; or |
488 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
489 |
|
|
* thread; or |
490 |
|
|
* <li>The specified waiting time elapses. |
491 |
|
|
* </ul> |
492 |
|
|
* <p>If the permits are acquired then the value <tt>true</tt> is returned. |
493 |
|
|
* <p>If the current thread: |
494 |
|
|
* <ul> |
495 |
|
|
* <li>has its interrupted status set on entry to this method; or |
496 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting to acquire |
497 |
|
|
* the permits, |
498 |
|
|
* </ul> |
499 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
500 |
|
|
* interrupted status is cleared. |
501 |
|
|
* Any permits that were to be assigned to this thread, are instead |
502 |
|
|
* assigned to the next waiting thread(s), as if |
503 |
|
|
* they had been made available by a call to {@link #release()}. |
504 |
|
|
* |
505 |
|
|
* <p>If the specified waiting time elapses then the value <tt>false</tt> |
506 |
|
|
* is returned. |
507 |
|
|
* If the time is |
508 |
|
|
* less than or equal to zero, the method will not wait at all. |
509 |
|
|
* Any permits that were to be assigned to this thread, are instead |
510 |
|
|
* assigned to the next waiting thread(s), as if |
511 |
|
|
* they had been made available by a call to {@link #release()}. |
512 |
|
|
* |
513 |
|
|
* @param permits the number of permits to acquire |
514 |
|
|
* @param timeout the maximum time to wait for the permits |
515 |
|
|
* @param unit the time unit of the <tt>timeout</tt> argument. |
516 |
|
|
* @return <tt>true</tt> if all permits were acquired and <tt>false</tt> |
517 |
|
|
* if the waiting time elapsed before all permits were acquired. |
518 |
|
|
* |
519 |
|
|
* @throws InterruptedException if the current thread is interrupted |
520 |
|
|
* @throws IllegalArgumentException if permits less than zero. |
521 |
|
|
* |
522 |
|
|
* @see Thread#interrupt |
523 |
|
|
* |
524 |
|
|
*/ |
525 |
|
|
public boolean tryAcquire(int permits, long timeout, TimeUnit unit) |
526 |
|
|
throws InterruptedException { |
527 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
528 |
dl |
1.23 |
return sync.acquireSharedTimed(permits, unit.toNanos(timeout)); |
529 |
dl |
1.16 |
} |
530 |
|
|
|
531 |
|
|
/** |
532 |
|
|
* Releases the given number of permits, returning them to the semaphore. |
533 |
|
|
* <p>Releases the given number of permits, increasing the number of |
534 |
|
|
* available permits by that amount. |
535 |
|
|
* If any threads are blocking trying to acquire permits, then the |
536 |
dl |
1.19 |
* one that has been waiting the longest |
537 |
dl |
1.16 |
* is selected and given the permits that were just released. |
538 |
|
|
* If the number of available permits satisfies that thread's request |
539 |
|
|
* then that thread is re-enabled for thread scheduling purposes; otherwise |
540 |
|
|
* the thread continues to wait. If there are still permits available |
541 |
|
|
* after the first thread's request has been satisfied, then those permits |
542 |
|
|
* are assigned to the next waiting thread. If it is satisfied then it is |
543 |
|
|
* re-enabled for thread scheduling purposes. This continues until there |
544 |
|
|
* are insufficient permits to satisfy the next waiting thread, or there |
545 |
|
|
* are no more waiting threads. |
546 |
|
|
* |
547 |
|
|
* <p>There is no requirement that a thread that releases a permit must |
548 |
|
|
* have acquired that permit by calling {@link Semaphore#acquire acquire}. |
549 |
|
|
* Correct usage of a semaphore is established by programming convention |
550 |
|
|
* in the application. |
551 |
|
|
* |
552 |
|
|
* @param permits the number of permits to release |
553 |
|
|
* @throws IllegalArgumentException if permits less than zero. |
554 |
|
|
*/ |
555 |
|
|
public void release(int permits) { |
556 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
557 |
dl |
1.23 |
sync.releaseShared(permits); |
558 |
dl |
1.2 |
} |
559 |
tim |
1.1 |
|
560 |
|
|
/** |
561 |
|
|
* Return the current number of permits available in this semaphore. |
562 |
|
|
* <p>This method is typically used for debugging and testing purposes. |
563 |
|
|
* @return the number of permits available in this semaphore. |
564 |
|
|
*/ |
565 |
dl |
1.16 |
public int availablePermits() { |
566 |
dl |
1.29 |
return sync.getPermits(); |
567 |
tim |
1.1 |
} |
568 |
dl |
1.15 |
|
569 |
|
|
/** |
570 |
dl |
1.31 |
* Acquire and return all permits that are immediately available. |
571 |
|
|
* @return the number of permits |
572 |
|
|
*/ |
573 |
|
|
public int drainPermits() { |
574 |
|
|
return sync.drainPermits(); |
575 |
|
|
} |
576 |
|
|
|
577 |
|
|
/** |
578 |
dl |
1.15 |
* Shrink the number of available permits by the indicated |
579 |
|
|
* reduction. This method can be useful in subclasses that |
580 |
dl |
1.31 |
* use semaphores to track resources that become |
581 |
dl |
1.15 |
* unavailable. This method differs from <tt>acquire</tt> |
582 |
|
|
* in that it does not block waiting for permits to become |
583 |
|
|
* available. |
584 |
|
|
* @param reduction the number of permits to remove |
585 |
|
|
* @throws IllegalArgumentException if reduction is negative |
586 |
|
|
*/ |
587 |
dl |
1.16 |
protected void reducePermits(int reduction) { |
588 |
dl |
1.15 |
if (reduction < 0) throw new IllegalArgumentException(); |
589 |
dl |
1.29 |
sync.reducePermits(reduction); |
590 |
dl |
1.16 |
} |
591 |
|
|
|
592 |
|
|
/** |
593 |
|
|
* Return true if this semaphore has fairness set true. |
594 |
|
|
* @return true if this semaphore has fairness set true. |
595 |
|
|
*/ |
596 |
|
|
public boolean isFair() { |
597 |
dl |
1.34 |
return sync instanceof FairSync; |
598 |
dl |
1.22 |
} |
599 |
|
|
|
600 |
|
|
/** |
601 |
dl |
1.23 |
* Queries whether any threads are waiting to acquire. Note that |
602 |
|
|
* because cancellations may occur at any time, a <tt>true</tt> |
603 |
|
|
* return does not guarantee that any other thread will ever |
604 |
|
|
* acquire. This method is designed primarily for use in |
605 |
|
|
* monitoring of the system state. |
606 |
|
|
* |
607 |
|
|
* @return true if there may be other threads waiting to acquire |
608 |
|
|
* the lock. |
609 |
dl |
1.22 |
*/ |
610 |
dl |
1.25 |
public final boolean hasQueuedThreads() { |
611 |
|
|
return sync.hasQueuedThreads(); |
612 |
dl |
1.22 |
} |
613 |
|
|
|
614 |
|
|
/** |
615 |
dl |
1.23 |
* Returns an estimate of the number of threads waiting to |
616 |
|
|
* acquire. The value is only an estimate because the number of |
617 |
|
|
* threads may change dynamically while this method traverses |
618 |
|
|
* internal data structures. This method is designed for use in |
619 |
|
|
* monitoring of the system state, not for synchronization |
620 |
|
|
* control. |
621 |
|
|
* @return the estimated number of threads waiting for this lock |
622 |
dl |
1.22 |
*/ |
623 |
dl |
1.23 |
public final int getQueueLength() { |
624 |
|
|
return sync.getQueueLength(); |
625 |
dl |
1.22 |
} |
626 |
|
|
|
627 |
|
|
/** |
628 |
dl |
1.23 |
* Returns a collection containing threads that may be waiting to |
629 |
|
|
* acquire. Because the actual set of threads may change |
630 |
|
|
* dynamically while constructing this result, the returned |
631 |
|
|
* collection is only a best-effort estimate. The elements of the |
632 |
|
|
* returned collection are in no particular order. This method is |
633 |
|
|
* designed to facilitate construction of subclasses that provide |
634 |
|
|
* more extensive monitoring facilities. |
635 |
|
|
* @return the collection of threads |
636 |
dl |
1.22 |
*/ |
637 |
dl |
1.23 |
protected Collection<Thread> getQueuedThreads() { |
638 |
|
|
return sync.getQueuedThreads(); |
639 |
dl |
1.16 |
} |
640 |
tim |
1.1 |
} |