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 |
< |
* order in which threads acquire permits. In particular, <em>barging</em> is |
94 |
< |
* 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. |
91 |
> |
* <p> The constructor for this class optionally accepts a |
92 |
> |
* <em>fairness</em> parameter. When set false, this class makes no |
93 |
> |
* guarantees about the order in which threads acquire permits. In |
94 |
> |
* particular, <em>barging</em> is permitted, that is, a thread |
95 |
> |
* invoking {@link #acquire} can be allocated a permit ahead of a |
96 |
> |
* thread that has been waiting. When fairness is set true, the |
97 |
> |
* semaphore guarantees that threads invoking any of the {@link |
98 |
> |
* #acquire() acquire} methods are allocated permits in the order in |
99 |
> |
* 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 |
> |
* <tt>acquire</tt> before another, but reach the ordering point after |
104 |
> |
* the other, and similarly upon return from the method. |
105 |
|
* |
106 |
|
* <p>Generally, semaphores used to control resource access should be |
107 |
|
* initialized as fair, to ensure that no thread is starved out from |
212 |
|
|
213 |
|
/** |
214 |
|
* Construct a <tt>Semaphore</tt> with the given number of |
215 |
+ |
* permits and nonfair fairness setting. |
216 |
+ |
* @param permits the initial number of permits available. This |
217 |
+ |
* value may be negative, in which case releases must |
218 |
+ |
* occur before any acquires will be granted. |
219 |
+ |
*/ |
220 |
+ |
public Semaphore(int permits) { |
221 |
+ |
sync = new NonfairSync(permits); |
222 |
+ |
} |
223 |
+ |
|
224 |
+ |
/** |
225 |
+ |
* Construct a <tt>Semaphore</tt> with the given number of |
226 |
|
* permits and the given fairness setting. |
227 |
|
* @param permits the initial number of permits available. This |
228 |
|
* value may be negative, in which case releases must |
360 |
|
*/ |
361 |
|
public boolean tryAcquire(long timeout, TimeUnit unit) |
362 |
|
throws InterruptedException { |
363 |
< |
return sync.acquireSharedTimed(1, unit.toNanos(timeout)); |
363 |
> |
return sync.acquireSharedNanos(1, unit.toNanos(timeout)); |
364 |
|
} |
365 |
|
|
366 |
|
/** |
538 |
|
public boolean tryAcquire(int permits, long timeout, TimeUnit unit) |
539 |
|
throws InterruptedException { |
540 |
|
if (permits < 0) throw new IllegalArgumentException(); |
541 |
< |
return sync.acquireSharedTimed(permits, unit.toNanos(timeout)); |
541 |
> |
return sync.acquireSharedNanos(permits, unit.toNanos(timeout)); |
542 |
|
} |
543 |
|
|
544 |
|
/** |