public class SemaphoreProxy extends AbstractDistributedObject<SemaphoreService> implements ISemaphore
PARTITIONING_STRATEGY
Constructor and Description |
---|
SemaphoreProxy(String name,
SemaphoreService service,
NodeEngine nodeEngine) |
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.
|
int |
availablePermits()
Returns the current number of permits currently available in this semaphore.
|
int |
drainPermits()
Acquires and returns all permits that are available at invocation time.
|
String |
getName()
Returns the name of this ISemaphore instance.
|
String |
getServiceName()
Returns the service name for this object.
|
void |
increasePermits(int increase)
Increases the number of available permits by the indicated amount.
|
boolean |
init(int permits)
Tries to initialize this ISemaphore instance with the given permit count
|
void |
reducePermits(int reduction)
Reduces the number of available permits by the indicated amount.
|
void |
release()
Releases a permit and increases the number of available permits by one.
|
void |
release(int permits)
Releases the given number of permits and increases the number of
available permits by that amount.
|
String |
toString() |
boolean |
tryAcquire()
Acquires a permit if one is available, and returns
true
immediately. |
boolean |
tryAcquire(int permits)
Acquires the given number of permits if they are available, and
returns
true immediately. |
boolean |
tryAcquire(int permits,
long timeout,
TimeUnit unit)
Acquires the given number of permits and returns
true , if they
become available during the given waiting time. |
boolean |
tryAcquire(long timeout,
TimeUnit unit)
Acquires a permit and returns
true , if one becomes available
during the given waiting time and the current thread has not been
interrupted. |
destroy, equals, getDistributedObjectName, getNameAsPartitionAwareData, getNodeEngine, getOperationService, getPartitionId, getPartitionKey, getService, hashCode, invalidate, invokeOnPartition, isClusterVersionEqualTo, isClusterVersionGreaterOrEqual, isClusterVersionGreaterThan, isClusterVersionLessOrEqual, isClusterVersionLessThan, isClusterVersionUnknown, isClusterVersionUnknownOrGreaterOrEqual, isClusterVersionUnknownOrGreaterThan, isClusterVersionUnknownOrLessOrEqual, isClusterVersionUnknownOrLessThan, postDestroy, preDestroy, throwNotActiveException, toData
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
destroy, getPartitionKey
public SemaphoreProxy(String name, SemaphoreService service, NodeEngine nodeEngine)
public String getName()
ISemaphore
getName
in interface DistributedObject
getName
in interface ISemaphore
public boolean init(int permits)
ISemaphore
init
in interface ISemaphore
permits
- the given permit countpublic void acquire() throws InterruptedException
ISemaphore
If no permit is available, then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:
ISemaphore.release()
methods for this
semaphore and the current thread is next to be assigned a permit,If the current thread:
InterruptedException
is thrown and the current thread's
interrupted status is cleared.acquire
in interface ISemaphore
InterruptedException
- if the current thread is interruptedpublic void acquire(int permits) throws InterruptedException
ISemaphore
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,InterruptedException
is thrown and the current thread's
interrupted status is cleared.acquire
in interface ISemaphore
permits
- the number of permits to acquireInterruptedException
- if the current thread is interruptedpublic int availablePermits()
ISemaphore
This method is typically used for debugging and testing purposes.
availablePermits
in interface ISemaphore
public int drainPermits()
ISemaphore
drainPermits
in interface ISemaphore
public void reducePermits(int reduction)
ISemaphore
acquire
as it does not block until permits
become available. Similarly, if the caller has acquired some permits,
they are not released with this call.reducePermits
in interface ISemaphore
reduction
- the number of permits to reducepublic void increasePermits(int increase)
ISemaphore
increasePermits
in interface ISemaphore
increase
- the number of permits to increasepublic void release()
ISemaphore
If the underlying ISemaphore
impl is the non-JDK compatible
CP impl that is configured via CPSemaphoreConfig
and fetched
via CPSubsystem
, then a HazelcastInstance can only release a permit which
it has acquired before. In other words, a HazelcastInstance cannot release a permit
without acquiring it first.
Otherwise, which means the underlying impl is either the JDK compatible
CP impl configured via CPSemaphoreConfig
and fetched
via CPSubsystem
, or it is the old impl fetched via
HazelcastInstance.getSemaphore(String)
, there is no requirement
that a HazelcastInstance that releases a permit must have acquired that permit by
calling one of the ISemaphore.acquire()
methods. A HazelcastInstance can freely
release a permit without acquiring it first. In this case, correct usage
of a semaphore is established by programming convention in the application.
release
in interface ISemaphore
public void release(int permits)
ISemaphore
If the underlying ISemaphore
impl is the non-JDK compatible
CP impl that is configured via CPSemaphoreConfig
and fetched
via CPSubsystem
, then a HazelcastInstance can only release a permit which
it has acquired before. In other words, a HazelcastInstance cannot release a permit
without acquiring it first.
Otherwise, which means the underlying impl is either the JDK compatible
CP impl configured via CPSemaphoreConfig
and fetched
via CPSubsystem
, or it is the old impl fetched via
HazelcastInstance.getSemaphore(String)
, there is no requirement
that a HazelcastInstance that releases a permit must have acquired that permit by
calling one of the ISemaphore.acquire()
methods. A HazelcastInstance can freely
release a permit without acquiring it first. In this case, correct usage
of a semaphore is established by programming convention in the application.
release
in interface ISemaphore
permits
- the number of permits to releasepublic boolean tryAcquire()
ISemaphore
true
immediately. If no permit is available, returns false
immediately.tryAcquire
in interface ISemaphore
true
if a permit was acquired, false
otherwisepublic boolean tryAcquire(int permits)
ISemaphore
true
immediately. If the requested number of permits are
not available, returns false
immediately.tryAcquire
in interface ISemaphore
permits
- the number of permits to acquiretrue
if the permits were acquired, false
otherwisepublic boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException
ISemaphore
true
, if one becomes available
during the given waiting time and the current thread has not been
interrupted. If a permit is acquired,
the number of available permits in the ISemaphore
instance is
also reduced 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:
Returns true
if a permit is successfully acquired.
Returns false
if the specified waiting time elapses without
acquiring a permit. If the time is less than or equal to zero,
the method will not wait at all.
If the current thread:
InterruptedException
is thrown and the current thread's
interrupted status is cleared.tryAcquire
in interface ISemaphore
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 acquiredInterruptedException
- if the current thread is interruptedpublic boolean tryAcquire(int permits, long timeout, TimeUnit unit) throws InterruptedException
ISemaphore
true
, if they
become available during the given waiting time. If permits are acquired,
the number of available permits in the ISemaphore
instance is
also reduced by the given amount.
If no sufficient permits are available, then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:
true
if requested permits are successfully acquired.
Returns false
if the specified waiting time elapses without
acquiring permits. If the time is less than or equal to zero,
the method will not wait at all.
If the current thread:
InterruptedException
is thrown and the current thread's
interrupted status is cleared.tryAcquire
in interface ISemaphore
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,
false
if the waiting time elapsed before all permits could be acquiredInterruptedException
- if the current thread is interruptedpublic String getServiceName()
DistributedObject
getServiceName
in interface DistributedObject
getServiceName
in class AbstractDistributedObject<SemaphoreService>
public String toString()
toString
in class AbstractDistributedObject<SemaphoreService>
Copyright © 2022 Hazelcast, Inc.. All Rights Reserved.