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 |
jsr166 |
1.57 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
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 |
tim |
1.1 |
|
11 |
|
|
/** |
12 |
|
|
* A counting semaphore. Conceptually, a semaphore maintains a set of |
13 |
|
|
* permits. Each {@link #acquire} blocks if necessary until a permit is |
14 |
|
|
* available, and then takes it. Each {@link #release} adds a permit, |
15 |
|
|
* potentially releasing a blocking acquirer. |
16 |
jsr166 |
1.51 |
* However, no actual permit objects are used; the {@code Semaphore} just |
17 |
tim |
1.1 |
* keeps a count of the number available and acts accordingly. |
18 |
|
|
* |
19 |
dl |
1.16 |
* <p>Semaphores are often used to restrict the number of threads than can |
20 |
tim |
1.1 |
* access some (physical or logical) resource. For example, here is |
21 |
|
|
* a class that uses a semaphore to control access to a pool of items: |
22 |
|
|
* <pre> |
23 |
|
|
* class Pool { |
24 |
dl |
1.44 |
* private static final int MAX_AVAILABLE = 100; |
25 |
dl |
1.16 |
* private final Semaphore available = new Semaphore(MAX_AVAILABLE, true); |
26 |
tim |
1.1 |
* |
27 |
|
|
* public Object getItem() throws InterruptedException { |
28 |
|
|
* available.acquire(); |
29 |
|
|
* return getNextAvailableItem(); |
30 |
|
|
* } |
31 |
|
|
* |
32 |
|
|
* public void putItem(Object x) { |
33 |
|
|
* if (markAsUnused(x)) |
34 |
|
|
* available.release(); |
35 |
|
|
* } |
36 |
|
|
* |
37 |
|
|
* // Not a particularly efficient data structure; just for demo |
38 |
|
|
* |
39 |
|
|
* protected Object[] items = ... whatever kinds of items being managed |
40 |
|
|
* protected boolean[] used = new boolean[MAX_AVAILABLE]; |
41 |
|
|
* |
42 |
|
|
* protected synchronized Object getNextAvailableItem() { |
43 |
|
|
* for (int i = 0; i < MAX_AVAILABLE; ++i) { |
44 |
|
|
* if (!used[i]) { |
45 |
|
|
* used[i] = true; |
46 |
|
|
* return items[i]; |
47 |
|
|
* } |
48 |
|
|
* } |
49 |
|
|
* return null; // not reached |
50 |
|
|
* } |
51 |
|
|
* |
52 |
|
|
* protected synchronized boolean markAsUnused(Object item) { |
53 |
|
|
* for (int i = 0; i < MAX_AVAILABLE; ++i) { |
54 |
|
|
* if (item == items[i]) { |
55 |
|
|
* if (used[i]) { |
56 |
|
|
* used[i] = false; |
57 |
|
|
* return true; |
58 |
tim |
1.8 |
* } else |
59 |
tim |
1.1 |
* return false; |
60 |
|
|
* } |
61 |
|
|
* } |
62 |
|
|
* return false; |
63 |
|
|
* } |
64 |
|
|
* |
65 |
|
|
* } |
66 |
|
|
* </pre> |
67 |
|
|
* |
68 |
dl |
1.16 |
* <p>Before obtaining an item each thread must acquire a permit from |
69 |
|
|
* the semaphore, guaranteeing that an item is available for use. When |
70 |
|
|
* the thread has finished with the item it is returned back to the |
71 |
|
|
* pool and a permit is returned to the semaphore, allowing another |
72 |
|
|
* thread to acquire that item. Note that no synchronization lock is |
73 |
|
|
* held when {@link #acquire} is called as that would prevent an item |
74 |
|
|
* from being returned to the pool. The semaphore encapsulates the |
75 |
|
|
* synchronization needed to restrict access to the pool, separately |
76 |
|
|
* from any synchronization needed to maintain the consistency of the |
77 |
|
|
* pool itself. |
78 |
|
|
* |
79 |
|
|
* <p>A semaphore initialized to one, and which is used such that it |
80 |
|
|
* only has at most one permit available, can serve as a mutual |
81 |
|
|
* exclusion lock. This is more commonly known as a <em>binary |
82 |
|
|
* semaphore</em>, because it only has two states: one permit |
83 |
|
|
* available, or zero permits available. When used in this way, the |
84 |
|
|
* binary semaphore has the property (unlike many {@link Lock} |
85 |
dl |
1.18 |
* implementations), that the "lock" can be released by a |
86 |
dl |
1.16 |
* thread other than the owner (as semaphores have no notion of |
87 |
|
|
* ownership). This can be useful in some specialized contexts, such |
88 |
|
|
* as deadlock recovery. |
89 |
|
|
* |
90 |
dl |
1.36 |
* <p> The constructor for this class optionally accepts a |
91 |
|
|
* <em>fairness</em> parameter. When set false, this class makes no |
92 |
|
|
* guarantees about the order in which threads acquire permits. In |
93 |
|
|
* particular, <em>barging</em> is permitted, that is, a thread |
94 |
|
|
* invoking {@link #acquire} can be allocated a permit ahead of a |
95 |
dl |
1.43 |
* thread that has been waiting - logically the new thread places itself at |
96 |
|
|
* the head of the queue of waiting threads. When fairness is set true, the |
97 |
dl |
1.36 |
* semaphore guarantees that threads invoking any of the {@link |
98 |
dl |
1.43 |
* #acquire() acquire} methods are selected to obtain permits in the order in |
99 |
dl |
1.36 |
* which their invocation of those methods was processed |
100 |
|
|
* (first-in-first-out; FIFO). Note that FIFO ordering necessarily |
101 |
|
|
* applies to specific internal points of execution within these |
102 |
|
|
* methods. So, it is possible for one thread to invoke |
103 |
jsr166 |
1.51 |
* {@code acquire} before another, but reach the ordering point after |
104 |
dl |
1.36 |
* the other, and similarly upon return from the method. |
105 |
jsr166 |
1.45 |
* Also note that the untimed {@link #tryAcquire() tryAcquire} methods do not |
106 |
dl |
1.43 |
* honor the fairness setting, but will take any permits that are |
107 |
|
|
* available. |
108 |
dl |
1.16 |
* |
109 |
|
|
* <p>Generally, semaphores used to control resource access should be |
110 |
|
|
* initialized as fair, to ensure that no thread is starved out from |
111 |
|
|
* accessing a resource. When using semaphores for other kinds of |
112 |
|
|
* synchronization control, the throughput advantages of non-fair |
113 |
|
|
* ordering often outweigh fairness considerations. |
114 |
|
|
* |
115 |
|
|
* <p>This class also provides convenience methods to {@link |
116 |
|
|
* #acquire(int) acquire} and {@link #release(int) release} multiple |
117 |
|
|
* permits at a time. Beware of the increased risk of indefinite |
118 |
dl |
1.19 |
* postponement when these methods are used without fairness set true. |
119 |
tim |
1.1 |
* |
120 |
jsr166 |
1.50 |
* <p>Memory consistency effects: Actions in a thread prior to calling |
121 |
|
|
* a "release" method such as {@code release()} |
122 |
|
|
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
123 |
|
|
* actions following a successful "acquire" method such as {@code acquire()} |
124 |
|
|
* in another thread. |
125 |
jsr166 |
1.49 |
* |
126 |
tim |
1.1 |
* @since 1.5 |
127 |
dl |
1.4 |
* @author Doug Lea |
128 |
tim |
1.1 |
* |
129 |
|
|
*/ |
130 |
dl |
1.16 |
|
131 |
dl |
1.23 |
public class Semaphore implements java.io.Serializable { |
132 |
dl |
1.16 |
private static final long serialVersionUID = -3222578661600680210L; |
133 |
dl |
1.25 |
/** All mechanics via AbstractQueuedSynchronizer subclass */ |
134 |
dl |
1.23 |
private final Sync sync; |
135 |
|
|
|
136 |
dl |
1.25 |
/** |
137 |
dl |
1.34 |
* Synchronization implementation for semaphore. Uses AQS state |
138 |
|
|
* to represent permits. Subclassed into fair and nonfair |
139 |
|
|
* versions. |
140 |
dl |
1.25 |
*/ |
141 |
dl |
1.34 |
abstract static class Sync extends AbstractQueuedSynchronizer { |
142 |
dl |
1.46 |
private static final long serialVersionUID = 1192457210091910933L; |
143 |
|
|
|
144 |
dl |
1.34 |
Sync(int permits) { |
145 |
dl |
1.29 |
setState(permits); |
146 |
dl |
1.23 |
} |
147 |
jsr166 |
1.45 |
|
148 |
dl |
1.34 |
final int getPermits() { |
149 |
dl |
1.29 |
return getState(); |
150 |
|
|
} |
151 |
|
|
|
152 |
dl |
1.34 |
final int nonfairTryAcquireShared(int acquires) { |
153 |
dl |
1.23 |
for (;;) { |
154 |
dl |
1.29 |
int available = getState(); |
155 |
dl |
1.23 |
int remaining = available - acquires; |
156 |
|
|
if (remaining < 0 || |
157 |
dl |
1.29 |
compareAndSetState(available, remaining)) |
158 |
dl |
1.23 |
return remaining; |
159 |
|
|
} |
160 |
|
|
} |
161 |
jsr166 |
1.45 |
|
162 |
dl |
1.34 |
protected final boolean tryReleaseShared(int releases) { |
163 |
dl |
1.23 |
for (;;) { |
164 |
jsr166 |
1.54 |
int current = getState(); |
165 |
|
|
int next = current + releases; |
166 |
|
|
if (next < current) // overflow |
167 |
|
|
throw new Error("Maximum permit count exceeded"); |
168 |
|
|
if (compareAndSetState(current, next)) |
169 |
dl |
1.23 |
return true; |
170 |
|
|
} |
171 |
|
|
} |
172 |
dl |
1.29 |
|
173 |
dl |
1.34 |
final void reducePermits(int reductions) { |
174 |
dl |
1.29 |
for (;;) { |
175 |
|
|
int current = getState(); |
176 |
|
|
int next = current - reductions; |
177 |
jsr166 |
1.54 |
if (next > current) // underflow |
178 |
|
|
throw new Error("Permit count underflow"); |
179 |
dl |
1.29 |
if (compareAndSetState(current, next)) |
180 |
|
|
return; |
181 |
|
|
} |
182 |
|
|
} |
183 |
dl |
1.31 |
|
184 |
dl |
1.34 |
final int drainPermits() { |
185 |
dl |
1.31 |
for (;;) { |
186 |
|
|
int current = getState(); |
187 |
|
|
if (current == 0 || compareAndSetState(current, 0)) |
188 |
|
|
return current; |
189 |
|
|
} |
190 |
|
|
} |
191 |
dl |
1.23 |
} |
192 |
dl |
1.16 |
|
193 |
tim |
1.1 |
/** |
194 |
dl |
1.34 |
* NonFair version |
195 |
|
|
*/ |
196 |
jsr166 |
1.55 |
static final class NonfairSync extends Sync { |
197 |
dl |
1.46 |
private static final long serialVersionUID = -2694183684443567898L; |
198 |
|
|
|
199 |
dl |
1.34 |
NonfairSync(int permits) { |
200 |
|
|
super(permits); |
201 |
|
|
} |
202 |
jsr166 |
1.45 |
|
203 |
dl |
1.34 |
protected int tryAcquireShared(int acquires) { |
204 |
|
|
return nonfairTryAcquireShared(acquires); |
205 |
|
|
} |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
/** |
209 |
|
|
* Fair version |
210 |
|
|
*/ |
211 |
jsr166 |
1.55 |
static final class FairSync extends Sync { |
212 |
dl |
1.46 |
private static final long serialVersionUID = 2014338818796000944L; |
213 |
|
|
|
214 |
dl |
1.34 |
FairSync(int permits) { |
215 |
|
|
super(permits); |
216 |
|
|
} |
217 |
jsr166 |
1.45 |
|
218 |
dl |
1.34 |
protected int tryAcquireShared(int acquires) { |
219 |
|
|
for (;;) { |
220 |
jsr166 |
1.53 |
if (hasQueuedPredecessors()) |
221 |
dl |
1.34 |
return -1; |
222 |
|
|
int available = getState(); |
223 |
|
|
int remaining = available - acquires; |
224 |
|
|
if (remaining < 0 || |
225 |
|
|
compareAndSetState(available, remaining)) |
226 |
|
|
return remaining; |
227 |
|
|
} |
228 |
|
|
} |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
/** |
232 |
jsr166 |
1.51 |
* Creates a {@code Semaphore} with the given number of |
233 |
dl |
1.36 |
* permits and nonfair fairness setting. |
234 |
jsr166 |
1.51 |
* |
235 |
|
|
* @param permits the initial number of permits available. |
236 |
|
|
* This value may be negative, in which case releases |
237 |
|
|
* must occur before any acquires will be granted. |
238 |
dl |
1.36 |
*/ |
239 |
jsr166 |
1.45 |
public Semaphore(int permits) { |
240 |
dl |
1.36 |
sync = new NonfairSync(permits); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
/** |
244 |
jsr166 |
1.51 |
* Creates a {@code Semaphore} with the given number of |
245 |
dl |
1.19 |
* permits and the given fairness setting. |
246 |
jsr166 |
1.51 |
* |
247 |
|
|
* @param permits the initial number of permits available. |
248 |
|
|
* This value may be negative, in which case releases |
249 |
|
|
* must occur before any acquires will be granted. |
250 |
|
|
* @param fair {@code true} if this semaphore will guarantee |
251 |
|
|
* first-in first-out granting of permits under contention, |
252 |
|
|
* else {@code false} |
253 |
tim |
1.1 |
*/ |
254 |
jsr166 |
1.45 |
public Semaphore(int permits, boolean fair) { |
255 |
jsr166 |
1.56 |
sync = fair ? new FairSync(permits) : new NonfairSync(permits); |
256 |
tim |
1.1 |
} |
257 |
|
|
|
258 |
|
|
/** |
259 |
|
|
* Acquires a permit from this semaphore, blocking until one is |
260 |
jsr166 |
1.51 |
* available, or the thread is {@linkplain Thread#interrupt interrupted}. |
261 |
tim |
1.1 |
* |
262 |
|
|
* <p>Acquires a permit, if one is available and returns immediately, |
263 |
|
|
* reducing the number of available permits by one. |
264 |
jsr166 |
1.51 |
* |
265 |
tim |
1.1 |
* <p>If no permit is available then the current thread becomes |
266 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
267 |
|
|
* one of two things happens: |
268 |
|
|
* <ul> |
269 |
|
|
* <li>Some other thread invokes the {@link #release} method for this |
270 |
dl |
1.16 |
* semaphore and the current thread is next to be assigned a permit; or |
271 |
jsr166 |
1.51 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} |
272 |
|
|
* the current thread. |
273 |
tim |
1.1 |
* </ul> |
274 |
|
|
* |
275 |
|
|
* <p>If the current thread: |
276 |
|
|
* <ul> |
277 |
|
|
* <li>has its interrupted status set on entry to this method; or |
278 |
jsr166 |
1.51 |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting |
279 |
tim |
1.1 |
* for a permit, |
280 |
|
|
* </ul> |
281 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
282 |
|
|
* interrupted status is cleared. |
283 |
|
|
* |
284 |
|
|
* @throws InterruptedException if the current thread is interrupted |
285 |
|
|
*/ |
286 |
dl |
1.2 |
public void acquire() throws InterruptedException { |
287 |
dl |
1.23 |
sync.acquireSharedInterruptibly(1); |
288 |
dl |
1.2 |
} |
289 |
tim |
1.1 |
|
290 |
|
|
/** |
291 |
|
|
* Acquires a permit from this semaphore, blocking until one is |
292 |
|
|
* available. |
293 |
|
|
* |
294 |
|
|
* <p>Acquires a permit, if one is available and returns immediately, |
295 |
|
|
* reducing the number of available permits by one. |
296 |
jsr166 |
1.51 |
* |
297 |
tim |
1.1 |
* <p>If no permit is available then the current thread becomes |
298 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
299 |
|
|
* some other thread invokes the {@link #release} method for this |
300 |
dl |
1.16 |
* semaphore and the current thread is next to be assigned a permit. |
301 |
tim |
1.1 |
* |
302 |
jsr166 |
1.51 |
* <p>If the current thread is {@linkplain Thread#interrupt interrupted} |
303 |
|
|
* while waiting for a permit then it will continue to wait, but the |
304 |
|
|
* time at which the thread is assigned a permit may change compared to |
305 |
|
|
* the time it would have received the permit had no interruption |
306 |
|
|
* occurred. When the thread does return from this method its interrupt |
307 |
|
|
* status will be set. |
308 |
tim |
1.1 |
*/ |
309 |
dl |
1.2 |
public void acquireUninterruptibly() { |
310 |
dl |
1.39 |
sync.acquireShared(1); |
311 |
dl |
1.2 |
} |
312 |
tim |
1.1 |
|
313 |
|
|
/** |
314 |
jsr166 |
1.45 |
* Acquires a permit from this semaphore, only if one is available at the |
315 |
tim |
1.1 |
* time of invocation. |
316 |
jsr166 |
1.51 |
* |
317 |
tim |
1.1 |
* <p>Acquires a permit, if one is available and returns immediately, |
318 |
jsr166 |
1.51 |
* with the value {@code true}, |
319 |
tim |
1.1 |
* reducing the number of available permits by one. |
320 |
|
|
* |
321 |
|
|
* <p>If no permit is available then this method will return |
322 |
jsr166 |
1.51 |
* immediately with the value {@code false}. |
323 |
tim |
1.1 |
* |
324 |
dl |
1.27 |
* <p>Even when this semaphore has been set to use a |
325 |
jsr166 |
1.51 |
* fair ordering policy, a call to {@code tryAcquire()} <em>will</em> |
326 |
dl |
1.27 |
* immediately acquire a permit if one is available, whether or not |
327 |
jsr166 |
1.45 |
* other threads are currently waiting. |
328 |
|
|
* This "barging" behavior can be useful in certain |
329 |
dl |
1.27 |
* circumstances, even though it breaks fairness. If you want to honor |
330 |
jsr166 |
1.45 |
* the fairness setting, then use |
331 |
dl |
1.27 |
* {@link #tryAcquire(long, TimeUnit) tryAcquire(0, TimeUnit.SECONDS) } |
332 |
|
|
* which is almost equivalent (it also detects interruption). |
333 |
|
|
* |
334 |
jsr166 |
1.51 |
* @return {@code true} if a permit was acquired and {@code false} |
335 |
|
|
* otherwise |
336 |
tim |
1.1 |
*/ |
337 |
|
|
public boolean tryAcquire() { |
338 |
dl |
1.34 |
return sync.nonfairTryAcquireShared(1) >= 0; |
339 |
tim |
1.1 |
} |
340 |
|
|
|
341 |
|
|
/** |
342 |
jsr166 |
1.45 |
* Acquires a permit from this semaphore, if one becomes available |
343 |
jsr166 |
1.51 |
* within the given waiting time and the current thread has not |
344 |
|
|
* been {@linkplain Thread#interrupt interrupted}. |
345 |
|
|
* |
346 |
tim |
1.1 |
* <p>Acquires a permit, if one is available and returns immediately, |
347 |
jsr166 |
1.51 |
* with the value {@code true}, |
348 |
tim |
1.1 |
* reducing the number of available permits by one. |
349 |
jsr166 |
1.51 |
* |
350 |
|
|
* <p>If no permit is available then the current thread becomes |
351 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
352 |
|
|
* one of three things happens: |
353 |
tim |
1.1 |
* <ul> |
354 |
|
|
* <li>Some other thread invokes the {@link #release} method for this |
355 |
dl |
1.16 |
* semaphore and the current thread is next to be assigned a permit; or |
356 |
jsr166 |
1.51 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} |
357 |
|
|
* the current thread; or |
358 |
tim |
1.1 |
* <li>The specified waiting time elapses. |
359 |
|
|
* </ul> |
360 |
jsr166 |
1.51 |
* |
361 |
|
|
* <p>If a permit is acquired then the value {@code true} is returned. |
362 |
|
|
* |
363 |
tim |
1.1 |
* <p>If the current thread: |
364 |
|
|
* <ul> |
365 |
|
|
* <li>has its interrupted status set on entry to this method; or |
366 |
jsr166 |
1.51 |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting |
367 |
|
|
* to acquire a permit, |
368 |
tim |
1.1 |
* </ul> |
369 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
370 |
|
|
* interrupted status is cleared. |
371 |
jsr166 |
1.51 |
* |
372 |
|
|
* <p>If the specified waiting time elapses then the value {@code false} |
373 |
|
|
* is returned. If the time is less than or equal to zero, the method |
374 |
|
|
* will not wait at all. |
375 |
tim |
1.1 |
* |
376 |
|
|
* @param timeout the maximum time to wait for a permit |
377 |
jsr166 |
1.51 |
* @param unit the time unit of the {@code timeout} argument |
378 |
|
|
* @return {@code true} if a permit was acquired and {@code false} |
379 |
|
|
* if the waiting time elapsed before a permit was acquired |
380 |
tim |
1.1 |
* @throws InterruptedException if the current thread is interrupted |
381 |
|
|
*/ |
382 |
jsr166 |
1.45 |
public boolean tryAcquire(long timeout, TimeUnit unit) |
383 |
tim |
1.1 |
throws InterruptedException { |
384 |
dl |
1.39 |
return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); |
385 |
tim |
1.1 |
} |
386 |
|
|
|
387 |
|
|
/** |
388 |
|
|
* Releases a permit, returning it to the semaphore. |
389 |
jsr166 |
1.51 |
* |
390 |
|
|
* <p>Releases a permit, increasing the number of available permits by |
391 |
|
|
* one. If any threads are trying to acquire a permit, then one is |
392 |
|
|
* selected and given the permit that was just released. That thread |
393 |
|
|
* is (re)enabled for thread scheduling purposes. |
394 |
|
|
* |
395 |
tim |
1.1 |
* <p>There is no requirement that a thread that releases a permit must |
396 |
|
|
* have acquired that permit by calling {@link #acquire}. |
397 |
|
|
* Correct usage of a semaphore is established by programming convention |
398 |
|
|
* in the application. |
399 |
|
|
*/ |
400 |
dl |
1.2 |
public void release() { |
401 |
dl |
1.23 |
sync.releaseShared(1); |
402 |
dl |
1.16 |
} |
403 |
jsr166 |
1.45 |
|
404 |
dl |
1.16 |
/** |
405 |
jsr166 |
1.45 |
* Acquires the given number of permits from this semaphore, |
406 |
|
|
* blocking until all are available, |
407 |
jsr166 |
1.51 |
* or the thread is {@linkplain Thread#interrupt interrupted}. |
408 |
dl |
1.16 |
* |
409 |
|
|
* <p>Acquires the given number of permits, if they are available, |
410 |
jsr166 |
1.51 |
* and returns immediately, reducing the number of available permits |
411 |
|
|
* by the given amount. |
412 |
dl |
1.16 |
* |
413 |
|
|
* <p>If insufficient permits are available then the current thread becomes |
414 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
415 |
|
|
* one of two things happens: |
416 |
|
|
* <ul> |
417 |
jsr166 |
1.45 |
* <li>Some other thread invokes one of the {@link #release() release} |
418 |
dl |
1.16 |
* methods for this semaphore, the current thread is next to be assigned |
419 |
|
|
* permits and the number of available permits satisfies this request; or |
420 |
jsr166 |
1.51 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} |
421 |
|
|
* the current thread. |
422 |
dl |
1.16 |
* </ul> |
423 |
|
|
* |
424 |
|
|
* <p>If the current thread: |
425 |
|
|
* <ul> |
426 |
|
|
* <li>has its interrupted status set on entry to this method; or |
427 |
jsr166 |
1.51 |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting |
428 |
dl |
1.16 |
* for a permit, |
429 |
|
|
* </ul> |
430 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
431 |
jsr166 |
1.45 |
* interrupted status is cleared. |
432 |
|
|
* Any permits that were to be assigned to this thread are instead |
433 |
dl |
1.43 |
* assigned to other threads trying to acquire permits, as if |
434 |
|
|
* permits had been made available by a call to {@link #release()}. |
435 |
dl |
1.16 |
* |
436 |
|
|
* @param permits the number of permits to acquire |
437 |
|
|
* @throws InterruptedException if the current thread is interrupted |
438 |
jsr166 |
1.51 |
* @throws IllegalArgumentException if {@code permits} is negative |
439 |
dl |
1.16 |
*/ |
440 |
|
|
public void acquire(int permits) throws InterruptedException { |
441 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
442 |
dl |
1.23 |
sync.acquireSharedInterruptibly(permits); |
443 |
dl |
1.16 |
} |
444 |
|
|
|
445 |
|
|
/** |
446 |
jsr166 |
1.45 |
* Acquires the given number of permits from this semaphore, |
447 |
dl |
1.16 |
* blocking until all are available. |
448 |
|
|
* |
449 |
|
|
* <p>Acquires the given number of permits, if they are available, |
450 |
jsr166 |
1.51 |
* and returns immediately, reducing the number of available permits |
451 |
|
|
* by the given amount. |
452 |
dl |
1.16 |
* |
453 |
|
|
* <p>If insufficient permits are available then the current thread becomes |
454 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
455 |
jsr166 |
1.45 |
* some other thread invokes one of the {@link #release() release} |
456 |
dl |
1.16 |
* methods for this semaphore, the current thread is next to be assigned |
457 |
|
|
* permits and the number of available permits satisfies this request. |
458 |
|
|
* |
459 |
jsr166 |
1.51 |
* <p>If the current thread is {@linkplain Thread#interrupt interrupted} |
460 |
|
|
* while waiting for permits then it will continue to wait and its |
461 |
|
|
* position in the queue is not affected. When the thread does return |
462 |
|
|
* from this method its interrupt status will be set. |
463 |
dl |
1.16 |
* |
464 |
|
|
* @param permits the number of permits to acquire |
465 |
jsr166 |
1.51 |
* @throws IllegalArgumentException if {@code permits} is negative |
466 |
dl |
1.16 |
* |
467 |
|
|
*/ |
468 |
|
|
public void acquireUninterruptibly(int permits) { |
469 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
470 |
dl |
1.39 |
sync.acquireShared(permits); |
471 |
dl |
1.16 |
} |
472 |
|
|
|
473 |
|
|
/** |
474 |
dl |
1.40 |
* Acquires the given number of permits from this semaphore, only |
475 |
|
|
* if all are available at the time of invocation. |
476 |
dl |
1.41 |
* |
477 |
jsr166 |
1.45 |
* <p>Acquires the given number of permits, if they are available, and |
478 |
jsr166 |
1.51 |
* returns immediately, with the value {@code true}, |
479 |
dl |
1.16 |
* reducing the number of available permits by the given amount. |
480 |
|
|
* |
481 |
|
|
* <p>If insufficient permits are available then this method will return |
482 |
jsr166 |
1.51 |
* immediately with the value {@code false} and the number of available |
483 |
dl |
1.16 |
* permits is unchanged. |
484 |
|
|
* |
485 |
dl |
1.27 |
* <p>Even when this semaphore has been set to use a fair ordering |
486 |
jsr166 |
1.51 |
* policy, a call to {@code tryAcquire} <em>will</em> |
487 |
dl |
1.27 |
* immediately acquire a permit if one is available, whether or |
488 |
|
|
* not other threads are currently waiting. This |
489 |
|
|
* "barging" behavior can be useful in certain |
490 |
|
|
* circumstances, even though it breaks fairness. If you want to |
491 |
|
|
* honor the fairness setting, then use {@link #tryAcquire(int, |
492 |
|
|
* long, TimeUnit) tryAcquire(permits, 0, TimeUnit.SECONDS) } |
493 |
|
|
* which is almost equivalent (it also detects interruption). |
494 |
|
|
* |
495 |
dl |
1.16 |
* @param permits the number of permits to acquire |
496 |
jsr166 |
1.51 |
* @return {@code true} if the permits were acquired and |
497 |
|
|
* {@code false} otherwise |
498 |
|
|
* @throws IllegalArgumentException if {@code permits} is negative |
499 |
dl |
1.16 |
*/ |
500 |
|
|
public boolean tryAcquire(int permits) { |
501 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
502 |
dl |
1.34 |
return sync.nonfairTryAcquireShared(permits) >= 0; |
503 |
dl |
1.16 |
} |
504 |
|
|
|
505 |
|
|
/** |
506 |
jsr166 |
1.45 |
* Acquires the given number of permits from this semaphore, if all |
507 |
jsr166 |
1.51 |
* become available within the given waiting time and the current |
508 |
|
|
* thread has not been {@linkplain Thread#interrupt interrupted}. |
509 |
|
|
* |
510 |
jsr166 |
1.45 |
* <p>Acquires the given number of permits, if they are available and |
511 |
jsr166 |
1.51 |
* returns immediately, with the value {@code true}, |
512 |
dl |
1.16 |
* reducing the number of available permits by the given amount. |
513 |
jsr166 |
1.51 |
* |
514 |
dl |
1.16 |
* <p>If insufficient permits are available then |
515 |
|
|
* the current thread becomes disabled for thread scheduling |
516 |
|
|
* purposes and lies dormant until one of three things happens: |
517 |
|
|
* <ul> |
518 |
jsr166 |
1.45 |
* <li>Some other thread invokes one of the {@link #release() release} |
519 |
dl |
1.16 |
* methods for this semaphore, the current thread is next to be assigned |
520 |
|
|
* permits and the number of available permits satisfies this request; or |
521 |
jsr166 |
1.51 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} |
522 |
|
|
* the current thread; or |
523 |
dl |
1.16 |
* <li>The specified waiting time elapses. |
524 |
|
|
* </ul> |
525 |
jsr166 |
1.51 |
* |
526 |
|
|
* <p>If the permits are acquired then the value {@code true} is returned. |
527 |
|
|
* |
528 |
dl |
1.16 |
* <p>If the current thread: |
529 |
|
|
* <ul> |
530 |
|
|
* <li>has its interrupted status set on entry to this method; or |
531 |
jsr166 |
1.51 |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting |
532 |
|
|
* to acquire the permits, |
533 |
dl |
1.16 |
* </ul> |
534 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
535 |
|
|
* interrupted status is cleared. |
536 |
jsr166 |
1.45 |
* Any permits that were to be assigned to this thread, are instead |
537 |
dl |
1.43 |
* assigned to other threads trying to acquire permits, as if |
538 |
|
|
* the permits had been made available by a call to {@link #release()}. |
539 |
dl |
1.16 |
* |
540 |
jsr166 |
1.51 |
* <p>If the specified waiting time elapses then the value {@code false} |
541 |
|
|
* is returned. If the time is less than or equal to zero, the method |
542 |
|
|
* will not wait at all. Any permits that were to be assigned to this |
543 |
|
|
* thread, are instead assigned to other threads trying to acquire |
544 |
|
|
* permits, as if the permits had been made available by a call to |
545 |
|
|
* {@link #release()}. |
546 |
dl |
1.16 |
* |
547 |
|
|
* @param permits the number of permits to acquire |
548 |
|
|
* @param timeout the maximum time to wait for the permits |
549 |
jsr166 |
1.51 |
* @param unit the time unit of the {@code timeout} argument |
550 |
|
|
* @return {@code true} if all permits were acquired and {@code false} |
551 |
|
|
* if the waiting time elapsed before all permits were acquired |
552 |
dl |
1.16 |
* @throws InterruptedException if the current thread is interrupted |
553 |
jsr166 |
1.51 |
* @throws IllegalArgumentException if {@code permits} is negative |
554 |
dl |
1.16 |
*/ |
555 |
|
|
public boolean tryAcquire(int permits, long timeout, TimeUnit unit) |
556 |
|
|
throws InterruptedException { |
557 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
558 |
dl |
1.39 |
return sync.tryAcquireSharedNanos(permits, unit.toNanos(timeout)); |
559 |
dl |
1.16 |
} |
560 |
|
|
|
561 |
|
|
/** |
562 |
|
|
* Releases the given number of permits, returning them to the semaphore. |
563 |
jsr166 |
1.51 |
* |
564 |
jsr166 |
1.45 |
* <p>Releases the given number of permits, increasing the number of |
565 |
dl |
1.16 |
* available permits by that amount. |
566 |
dl |
1.43 |
* If any threads are trying to acquire permits, then one |
567 |
dl |
1.16 |
* is selected and given the permits that were just released. |
568 |
|
|
* If the number of available permits satisfies that thread's request |
569 |
dl |
1.43 |
* then that thread is (re)enabled for thread scheduling purposes; |
570 |
|
|
* otherwise the thread will wait until sufficient permits are available. |
571 |
|
|
* If there are still permits available |
572 |
|
|
* after this thread's request has been satisfied, then those permits |
573 |
|
|
* are assigned in turn to other threads trying to acquire permits. |
574 |
dl |
1.16 |
* |
575 |
|
|
* <p>There is no requirement that a thread that releases a permit must |
576 |
|
|
* have acquired that permit by calling {@link Semaphore#acquire acquire}. |
577 |
|
|
* Correct usage of a semaphore is established by programming convention |
578 |
|
|
* in the application. |
579 |
|
|
* |
580 |
|
|
* @param permits the number of permits to release |
581 |
jsr166 |
1.51 |
* @throws IllegalArgumentException if {@code permits} is negative |
582 |
dl |
1.16 |
*/ |
583 |
|
|
public void release(int permits) { |
584 |
|
|
if (permits < 0) throw new IllegalArgumentException(); |
585 |
dl |
1.23 |
sync.releaseShared(permits); |
586 |
dl |
1.2 |
} |
587 |
tim |
1.1 |
|
588 |
|
|
/** |
589 |
dl |
1.41 |
* Returns the current number of permits available in this semaphore. |
590 |
jsr166 |
1.51 |
* |
591 |
tim |
1.1 |
* <p>This method is typically used for debugging and testing purposes. |
592 |
jsr166 |
1.51 |
* |
593 |
|
|
* @return the number of permits available in this semaphore |
594 |
tim |
1.1 |
*/ |
595 |
dl |
1.16 |
public int availablePermits() { |
596 |
dl |
1.29 |
return sync.getPermits(); |
597 |
tim |
1.1 |
} |
598 |
dl |
1.15 |
|
599 |
|
|
/** |
600 |
jsr166 |
1.45 |
* Acquires and returns all permits that are immediately available. |
601 |
jsr166 |
1.51 |
* |
602 |
|
|
* @return the number of permits acquired |
603 |
dl |
1.31 |
*/ |
604 |
|
|
public int drainPermits() { |
605 |
|
|
return sync.drainPermits(); |
606 |
|
|
} |
607 |
|
|
|
608 |
|
|
/** |
609 |
dl |
1.41 |
* Shrinks the number of available permits by the indicated |
610 |
dl |
1.40 |
* reduction. This method can be useful in subclasses that use |
611 |
|
|
* semaphores to track resources that become unavailable. This |
612 |
jsr166 |
1.51 |
* method differs from {@code acquire} in that it does not block |
613 |
dl |
1.40 |
* waiting for permits to become available. |
614 |
jsr166 |
1.51 |
* |
615 |
dl |
1.15 |
* @param reduction the number of permits to remove |
616 |
jsr166 |
1.51 |
* @throws IllegalArgumentException if {@code reduction} is negative |
617 |
dl |
1.15 |
*/ |
618 |
dl |
1.16 |
protected void reducePermits(int reduction) { |
619 |
jsr166 |
1.53 |
if (reduction < 0) throw new IllegalArgumentException(); |
620 |
dl |
1.29 |
sync.reducePermits(reduction); |
621 |
dl |
1.16 |
} |
622 |
|
|
|
623 |
|
|
/** |
624 |
jsr166 |
1.51 |
* Returns {@code true} if this semaphore has fairness set true. |
625 |
|
|
* |
626 |
|
|
* @return {@code true} if this semaphore has fairness set true |
627 |
dl |
1.16 |
*/ |
628 |
|
|
public boolean isFair() { |
629 |
dl |
1.34 |
return sync instanceof FairSync; |
630 |
dl |
1.22 |
} |
631 |
|
|
|
632 |
|
|
/** |
633 |
dl |
1.23 |
* Queries whether any threads are waiting to acquire. Note that |
634 |
jsr166 |
1.51 |
* because cancellations may occur at any time, a {@code true} |
635 |
dl |
1.23 |
* return does not guarantee that any other thread will ever |
636 |
|
|
* acquire. This method is designed primarily for use in |
637 |
|
|
* monitoring of the system state. |
638 |
|
|
* |
639 |
jsr166 |
1.51 |
* @return {@code true} if there may be other threads waiting to |
640 |
|
|
* acquire the lock |
641 |
dl |
1.22 |
*/ |
642 |
jsr166 |
1.45 |
public final boolean hasQueuedThreads() { |
643 |
dl |
1.25 |
return sync.hasQueuedThreads(); |
644 |
dl |
1.22 |
} |
645 |
|
|
|
646 |
|
|
/** |
647 |
jsr166 |
1.51 |
* Returns an estimate of the number of threads waiting to acquire. |
648 |
|
|
* The value is only an estimate because the number of threads may |
649 |
|
|
* change dynamically while this method traverses internal data |
650 |
|
|
* structures. This method is designed for use in monitoring of the |
651 |
|
|
* system state, not for synchronization control. |
652 |
|
|
* |
653 |
dl |
1.23 |
* @return the estimated number of threads waiting for this lock |
654 |
dl |
1.22 |
*/ |
655 |
dl |
1.23 |
public final int getQueueLength() { |
656 |
|
|
return sync.getQueueLength(); |
657 |
dl |
1.22 |
} |
658 |
|
|
|
659 |
|
|
/** |
660 |
jsr166 |
1.51 |
* Returns a collection containing threads that may be waiting to acquire. |
661 |
|
|
* Because the actual set of threads may change dynamically while |
662 |
|
|
* constructing this result, the returned collection is only a best-effort |
663 |
|
|
* estimate. The elements of the returned collection are in no particular |
664 |
|
|
* order. This method is designed to facilitate construction of |
665 |
|
|
* subclasses that provide more extensive monitoring facilities. |
666 |
|
|
* |
667 |
dl |
1.23 |
* @return the collection of threads |
668 |
dl |
1.22 |
*/ |
669 |
dl |
1.23 |
protected Collection<Thread> getQueuedThreads() { |
670 |
|
|
return sync.getQueuedThreads(); |
671 |
dl |
1.16 |
} |
672 |
dl |
1.37 |
|
673 |
|
|
/** |
674 |
dl |
1.40 |
* Returns a string identifying this semaphore, as well as its state. |
675 |
jsr166 |
1.51 |
* The state, in brackets, includes the String {@code "Permits ="} |
676 |
|
|
* followed by the number of permits. |
677 |
|
|
* |
678 |
|
|
* @return a string identifying this semaphore, as well as its state |
679 |
dl |
1.37 |
*/ |
680 |
|
|
public String toString() { |
681 |
|
|
return super.toString() + "[Permits = " + sync.getPermits() + "]"; |
682 |
|
|
} |
683 |
tim |
1.1 |
} |