public interface ISemaphore extends Instance
java.util.concurrent.Semaphore
.
Hazelcast's ISemaphore is a cluster-wide counting semaphore. Conceptually,
it maintains a set of permits. Each acquire()
blocks if necessary until
a permit is available, and then takes it. Each release()
adds a permit,
potentially releasing a blocking acquirer. However, no actual permit objects are
used; the semaphore just keeps a count of the number available and acts accordingly.
The Hazelcast distributed semaphore implementation guarantees that
threads invoking any of the acquire
methods are selected
to obtain permits in the order in which their invocation of those methods
was processed(first-in-first-out; FIFO). Note that FIFO ordering necessarily
applies to specific internal points of execution within the cluster. So,
it is possible for one member to invoke acquire
before another, but reach
the ordering point after the other, and similarly upon return from the method.
The Hazelcast semaphore also allows you to attach()
/detach()
permits to the caller address. This provides a safety mechanism in case
that address becomes disconnected from the cluster. Attached permits will
automatically be released
to the semaphore if a disconnection
occurs. An address can also have an excess number of detached permits and is
represented by a negative attached permit count. This is the number of permits
that the semaphore will automatically be reduced
by
if a disconnection occurs.
This class also provides convenience methods to acquire
and release
multiple
permits at a time. Beware of the increased risk of indefinite
postponement when using the multiple acquire. If a single permit is
released to a semaphore that is currently blocking, a thread waiting
for one permit will acquire it before a thread waiting for multiple
permits regardless of the call order.
Instance.InstanceType
Modifier and Type | Method and Description |
---|---|
void |
acquire()
Acquires a permit, if one is available and returns immediately,
reducing the number of available permits by one.
|
void |
acquire(int permits)
Acquires the given number of permits, if they are available,
and returns immediately, reducing the number of available permits
by the given amount.
|
Future |
acquireAsync()
Asynchronously acquires a permit.
|
Future |
acquireAsync(int permits)
Asynchronously acquires a given number of permits.
|
void |
acquireAttach()
Acquires and attaches a permit to the caller's address.
|
void |
acquireAttach(int permits)
Acquires and attaches the given number of permits to the caller's
address.
|
Future |
acquireAttachAsync()
Asynchronously acquires and attaches a permit to the caller's address.
|
Future |
acquireAttachAsync(int permits)
Asynchronously acquires and attaches the given number of permits
to the caller's address.
|
void |
attach()
Attaches a permit to the caller's address.
|
void |
attach(int permits)
Attaches the given number of permits to the caller's address.
|
int |
attachedPermits()
Returns the current number of permits attached to the caller's address.
|
int |
availablePermits()
Returns the current number of permits currently available in this semaphore.
|
void |
detach()
Detaches a permit from the caller's address.
|
void |
detach(int permits)
Detaches the given number of permits from the caller's address.
|
int |
drainPermits()
Acquires and returns all permits that are immediately available.
|
LocalSemaphoreStats |
getLocalSemaphoreStats() |
String |
getName()
Returns the name of this ISemaphore instance.
|
void |
reducePermits(int reduction)
Shrinks the number of available permits by the indicated
reduction.
|
void |
release()
Releases a permit, increasing the number of available permits by
one.
|
void |
release(int permits)
Releases the given number of permits, increasing the number of
available permits by that amount.
|
void |
releaseDetach()
Detaches a permit from the caller's address and returns it to the semaphore.
|
void |
releaseDetach(int permits)
Detaches the given number of permits from the caller's address and returns
them to the semaphore.
|
boolean |
tryAcquire()
Acquires a permit, if one is available and returns immediately,
with the value
true ,
reducing the number of available permits by one. |
boolean |
tryAcquire(int permits)
Acquires the given number of permits, if they are available, and
returns immediately, with the value
true ,
reducing the number of available permits by the given amount. |
boolean |
tryAcquire(int permits,
long timeout,
TimeUnit unit)
Acquires the given number of permits, if they are available and
returns immediately, with the value
true ,
reducing the number of available permits by the given amount. |
boolean |
tryAcquire(long timeout,
TimeUnit unit)
Acquires a permit from this semaphore, if one becomes available
within the given waiting time and the current thread has not
been interrupted.
|
boolean |
tryAcquireAttach()
Acquires a permit from this semaphore and attaches it to the
calling member, only if one is available at the time of invocation.
|
boolean |
tryAcquireAttach(int permits)
Acquires the given number of permits from this semaphore and
attaches them to the calling member, only if all are available
at the time of invocation.
|
boolean |
tryAcquireAttach(int permits,
long timeout,
TimeUnit unit)
Acquires the given number of permits from this semaphore and
attaches them to the calling member, only if all become available
within the given waiting time and the current thread has not
been interrupted or the instance
destroyed.
|
boolean |
tryAcquireAttach(long timeout,
TimeUnit unit)
Acquires a permit from this semaphore and attaches it to the calling
member, only if one becomes available within the given waiting
time and the current thread has not been interrupted
or the instance destroyed.
|
destroy, getId, getInstanceType
String getName()
void acquire() throws InstanceDestroyedException, InterruptedException
Acquires a permit, if one is available and returns immediately, reducing the number of available permits by one.
If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:
release()
methods for this
semaphore and the current thread is next to be assigned a permit;
If the ISemaphore instance is destroyed while the thread is waiting
then InstanceDestroyedException
will be thrown.
If the current thread:
InterruptedException
is thrown and the current thread's
interrupted status is cleared.InstanceDestroyedException
- if the instance is destroyed while waitingInterruptedException
- if the current thread is interruptedIllegalStateException
- if hazelcast instance is shutdown while waitingvoid acquire(int permits) throws InstanceDestroyedException, InterruptedException
Acquires the given number of permits, if they are available, and returns immediately, reducing the number of available permits by the given amount.
If insufficient permits are available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:
release
methods for this semaphore, the current thread is next to be assigned
permits and the number of available permits satisfies this request;
InstanceDestroyedException
will be thrown.
If the current thread:
InterruptedException
is thrown and the current thread's
interrupted status is cleared.permits
- the number of permits to acquireInstanceDestroyedException
- if the instance is destroyed while waitingInterruptedException
- if the current thread is interruptedIllegalArgumentException
- if permits
is negativeIllegalStateException
- if hazelcast instance is shutdown while waitingFuture acquireAsync()
Future future = semaphore.acquireAsync(); // do some other stuff, when ready get the result Object value = future.get();Future.get() will block until the actual acquire() completes. If the application requires timely response, then Future.get(timeout, timeunit) can be used.
Future future = semaphore.acquireAsync(); try{ Object value = future.get(40, TimeUnit.MILLISECOND); }catch (TimeoutException t) { // time wasn't enough }This method itself does not throw any exceptions. Exceptions occur during the
future.get()
operation.
If insufficient permits are available when calling
future.get()
then the current
thread becomes disabled for thread scheduling purposes and lies dormant until
one of four things happens:
release
methods for this semaphore, the current thread is next to be assigned
permits and the number of available permits satisfies this request;
future.get()
is waiting then
ExecutionException
will be thrown with
InstanceDestroyedException
set as it's cause.
If the Hazelcast instance is shutdows while the
future.get()
is waiting then
IllegalStateException
will be thrown.
If when calling future.get()
a timeout is specified and the a permit cannot be acquired within the
timeout period TimeoutException
will be thrown.
If when calling future.get()
the current thread:
InterruptedException
is thrown and the current thread's
interrupted status is cleared.
If the thread future.get()
throws an
InterruptedException, the acquire request is still outstanding and
future.get()
may be called again. To cancel the actual acquire,
future.cancel()
must be called. If the cancel method returns false
then
it was too late to cancel the request and the permit was acquired.
The future.cancel()
mayInterruptIfRunning argument is ignored.
Future
Future acquireAsync(int permits)
acquireAsync()
.permits
- the number of permits to acquireIllegalArgumentException
- if permits
is negativevoid acquireAttach() throws InstanceDestroyedException, InterruptedException
IllegalStateException
- if hazelcast instance is shutdown while waitingInstanceDestroyedException
InterruptedException
void acquireAttach(int permits) throws InstanceDestroyedException, InterruptedException
acquire()
and attach()
.permits
- the number of permits to acquire and attachIllegalArgumentException
- if permits
is negativeIllegalStateException
- if hazelcast instance is shutdown while waitingInstanceDestroyedException
InterruptedException
Future acquireAttachAsync()
acquireAsync()
and attach()
.Future acquireAttachAsync(int permits)
acquireAsync()
and attach()
.permits
- the number of permits to acquire and attachIllegalArgumentException
- if permits
is negativevoid attach()
attachedPermits()
.void attach(int permits)
attachedPermits()
.permits
- the number of permits to attachIllegalArgumentException
- if permits
is negativeint attachedPermits()
released
to the semaphore if the caller address becomes
disconnected from the cluster.
Negative value represents the number of permits that the semaphore will
automatically be reduced
by if the caller address
becomes disconnected from the cluster.
int availablePermits()
void detach()
attachedPermits()
.void detach(int permits)
attachedPermits()
.permits
- the number of permits to detachint drainPermits()
void reducePermits(int reduction)
acquire
in that it does not
block waiting for permits to become available.reduction
- the number of permits to removeIllegalArgumentException
- if reduction
is negativevoid release()
acquire
methods.
Correct usage of a semaphore is established by programming convention
in the application.void release(int permits)
acquire
methods.
Correct usage of a semaphore is established by programming convention
in the application.permits
- the number of permits to releaseIllegalArgumentException
- if permits
is negativevoid releaseDetach()
void releaseDetach(int permits)
release(int)
and detach()
.permits
- the number of permits to release and detachIllegalArgumentException
- if permits
is negativeboolean tryAcquire()
true
,
reducing the number of available permits by one.
If no permit is available then this method will return
immediately with the value false
.true
if a permit was acquired and false
otherwiseboolean tryAcquire(int permits)
true
,
reducing the number of available permits by the given amount.
If insufficient permits are available then this method will return
immediately with the value false
and the number of available
permits is unchanged.
permits
- the number of permits to acquiretrue
if the permits were acquired and
false
otherwiseIllegalArgumentException
- if permits
is negativeboolean tryAcquire(long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException
true
,
reducing the number of available permits by one.
If no permit is available then the current thread becomes
disabled for thread scheduling purposes and lies dormant until
one of three things happens:
release()
method for this
semaphore and the current thread is next to be assigned a permit; or
true
is returned.
If the specified waiting time elapses then the value false
is returned. If the time is less than or equal to zero, the method
will not wait at all.
If the ISemaphore instance is destroyed while the thread is waiting
then InstanceDestroyedException
will be thrown.
If the current thread:
InterruptedException
is thrown and the current thread's
interrupted status is cleared.timeout
- the maximum time to wait for a permitunit
- the time unit of the timeout
argumenttrue
if a permit was acquired and false
if the waiting time elapsed before a permit was acquiredInstanceDestroyedException
- if the instance is destroyed while waitingInterruptedException
- if the current thread is interruptedIllegalStateException
- if hazelcast instance is shutdown while waitingboolean tryAcquire(int permits, long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException
true
,
reducing the number of available permits by the given amount.
If insufficient permits are available then
the current thread becomes disabled for thread scheduling
purposes and lies dormant until one of three things happens:
release
methods for this semaphore, the current thread is next to be assigned
permits and the number of available permits satisfies this request; or
true
is returned.
If the specified waiting time elapses then the value false
is returned. If the time is less than or equal to zero, the method
will not wait at all.
If the ISemaphore instance is destroyed while the thread is waiting
then InstanceDestroyedException
will be thrown.
If the current thread:
InterruptedException
is thrown and the current thread's
interrupted status is cleared.permits
- the number of permits to acquiretimeout
- the maximum time to wait for the permitsunit
- the time unit of the timeout
argumenttrue
if all permits were acquired and false
if the waiting time elapsed before all permits could be acquiredInstanceDestroyedException
- if the instance is destroyed while waitingInterruptedException
- if the current thread is interruptedIllegalArgumentException
- if permits
is negativeIllegalStateException
- if hazelcast instance is shutdown while waitingboolean tryAcquireAttach()
tryAcquire()
and attach()
.true
if permit was acquired and attached to
the caller's addressIllegalArgumentException
- if permits
is negativeboolean tryAcquireAttach(int permits)
tryAcquire(int permits)
and attach(int)
.permits
- the number of permits to try to acquire and attachtrue
if permit(s) were acquired and attached to
the caller's addressIllegalArgumentException
- if permits
is negativeboolean tryAcquireAttach(long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException
InstanceDestroyedException
will be thrown.
See tryAcquire(long timeout, TimeUnit unit)
and attach()
.timeout
- the maximum time to wait for a permitunit
- unit the time unit of the timeout
argumenttrue
if a permit was acquired and false
if the waiting time elapsed before a permit could be acquiredInstanceDestroyedException
- if the instance is destroyed while waitingInterruptedException
- if the current thread is interruptedIllegalStateException
- if hazelcast instance is shutdown while waitingboolean tryAcquireAttach(int permits, long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException
tryAcquire(int permits, long timeout, TimeUnit unit)
and attach(int)
.permits
- the number of permits to try to acquire and attachtimeout
- the maximum time to wait for the permitsunit
- unit the time unit of the timeout
argumenttrue
if permit(s) were acquired and attached to
the caller's address and false
if the waiting
time elapsed before the permits could be acquiredInstanceDestroyedException
- if the instance is destroyed while waitingInterruptedException
- if the current thread is interruptedIllegalArgumentException
- if permits
is negativeIllegalStateException
- if hazelcast instance is shutdown while waitingLocalSemaphoreStats getLocalSemaphoreStats()
Copyright © 2008-2013 Hazelcast, Inc. All Rights Reserved.