public class RaftSessionAwareSemaphoreProxy extends SessionAwareProxy implements ISemaphore
ISemaphore
Modifier and Type | Field and Description |
---|---|
static int |
DRAIN_SESSION_ACQ_COUNT
Since a proxy does not know how many permits will be drained on
the Raft group, it uses this constant to increment its local session
acquire count.
|
groupId
Constructor and Description |
---|
RaftSessionAwareSemaphoreProxy(NodeEngine nodeEngine,
RaftGroupId groupId,
String proxyName,
String objectName) |
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.
|
void |
destroy()
Destroys this object cluster-wide.
|
int |
drainPermits()
Acquires and returns all permits that are available at invocation time.
|
String |
getName()
Returns the name of this ISemaphore instance.
|
String |
getPartitionKey()
Returns the key of the partition that this DistributedObject is assigned to.
|
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.
|
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. |
acquireSession, acquireSession, getGroupId, getOrCreateUniqueThreadId, getSession, invalidateSession, releaseSession, releaseSession
public static final int DRAIN_SESSION_ACQ_COUNT
public RaftSessionAwareSemaphoreProxy(NodeEngine nodeEngine, RaftGroupId groupId, String proxyName, String objectName)
public boolean init(int permits)
ISemaphore
init
in interface ISemaphore
permits
- the given permit countpublic void acquire()
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
public void acquire(int permits)
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 acquirepublic 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)
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 acquiredpublic boolean tryAcquire(int permits, long timeout, TimeUnit unit)
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 acquiredpublic 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 thread can only release a permit which
it has acquired before. In other words, a thread 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 thread that releases a permit must have acquired that permit by
calling one of the ISemaphore.acquire()
methods. A thread 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 thread can only release a permit which
it has acquired before. In other words, a thread 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 thread that releases a permit must have acquired that permit by
calling one of the ISemaphore.acquire()
methods. A thread 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 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 String getName()
ISemaphore
getName
in interface DistributedObject
getName
in interface ISemaphore
public String getPartitionKey()
DistributedObject
IAtomicLong
. For a partitioned data structure like an IMap
,
the returned value will not be null, but otherwise undefined.getPartitionKey
in interface DistributedObject
public String getServiceName()
DistributedObject
getServiceName
in interface DistributedObject
public void destroy()
DistributedObject
destroy
in interface DistributedObject
Copyright © 2019 Hazelcast, Inc.. All Rights Reserved.