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