public class SemaphoreClientProxy extends Object implements ISemaphore
Instance.InstanceType
Constructor and Description |
---|
SemaphoreClientProxy(HazelcastClient hazelcastClient,
String name) |
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 |
destroy()
Destroys this instance cluster-wide.
|
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.
|
Object |
getId()
Returns the unique id for this instance.
|
Instance.InstanceType |
getInstanceType()
Returns instance type such as map, set, list, lock, topic, multimap, id generator
|
LocalSemaphoreStats |
getLocalSemaphoreStats() |
String |
getName()
Returns the name of this ISemaphore instance.
|
void |
reducePermits(int permits)
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.
|
public SemaphoreClientProxy(HazelcastClient hazelcastClient, String name)
public void acquire() throws InstanceDestroyedException, InterruptedException
ISemaphore
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:
ISemaphore.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.acquire
in interface ISemaphore
InstanceDestroyedException
- if the instance is destroyed while waitingInterruptedException
- if the current thread is interruptedpublic void acquire(int permits) throws InstanceDestroyedException, InterruptedException
ISemaphore
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.acquire
in interface ISemaphore
permits
- the number of permits to acquireInstanceDestroyedException
- if the instance is destroyed while waitingInterruptedException
- if the current thread is interruptedpublic Future acquireAsync()
ISemaphore
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.
acquireAsync
in interface ISemaphore
Future
public Future acquireAsync(int permits)
ISemaphore
ISemaphore.acquireAsync()
.acquireAsync
in interface ISemaphore
permits
- the number of permits to acquirepublic void acquireAttach() throws InstanceDestroyedException, InterruptedException
ISemaphore
ISemaphore.acquire()
and ISemaphore.attach()
.acquireAttach
in interface ISemaphore
InstanceDestroyedException
InterruptedException
public void acquireAttach(int permits) throws InstanceDestroyedException, InterruptedException
ISemaphore
ISemaphore.acquire()
and ISemaphore.attach()
.acquireAttach
in interface ISemaphore
permits
- the number of permits to acquire and attachInstanceDestroyedException
InterruptedException
public Future acquireAttachAsync()
ISemaphore
ISemaphore.acquireAsync()
and ISemaphore.attach()
.acquireAttachAsync
in interface ISemaphore
public Future acquireAttachAsync(int permits)
ISemaphore
ISemaphore.acquireAsync()
and ISemaphore.attach()
.acquireAttachAsync
in interface ISemaphore
permits
- the number of permits to acquire and attachpublic void attach()
ISemaphore
ISemaphore.attachedPermits()
.attach
in interface ISemaphore
public void attach(int permits)
ISemaphore
ISemaphore.attachedPermits()
.attach
in interface ISemaphore
permits
- the number of permits to attachpublic int attachedPermits()
ISemaphore
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.
attachedPermits
in interface ISemaphore
public int availablePermits()
ISemaphore
availablePermits
in interface ISemaphore
public void detach()
ISemaphore
ISemaphore.attachedPermits()
.detach
in interface ISemaphore
public void detach(int permits)
ISemaphore
ISemaphore.attachedPermits()
.detach
in interface ISemaphore
permits
- the number of permits to detachpublic int drainPermits()
ISemaphore
drainPermits
in interface ISemaphore
public void reducePermits(int permits)
ISemaphore
acquire
in that it does not
block waiting for permits to become available.reducePermits
in interface ISemaphore
permits
- the number of permits to removepublic void release()
ISemaphore
acquire
methods.
Correct usage of a semaphore is established by programming convention
in the application.release
in interface ISemaphore
public void release(int permits)
ISemaphore
acquire
methods.
Correct usage of a semaphore is established by programming convention
in the application.release
in interface ISemaphore
permits
- the number of permits to releasepublic void releaseDetach()
ISemaphore
ISemaphore.release()
and ISemaphore.detach()
.releaseDetach
in interface ISemaphore
public void releaseDetach(int permits)
ISemaphore
ISemaphore.release(int)
and ISemaphore.detach()
.releaseDetach
in interface ISemaphore
permits
- the number of permits to release and detachpublic boolean tryAcquire()
ISemaphore
true
,
reducing the number of available permits by one.
If no permit is available then this method will return
immediately with the value false
.tryAcquire
in interface ISemaphore
true
if a permit was acquired and false
otherwisepublic boolean tryAcquire(int permits)
ISemaphore
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.
tryAcquire
in interface ISemaphore
permits
- the number of permits to acquiretrue
if the permits were acquired and
false
otherwisepublic boolean tryAcquire(long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException
ISemaphore
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:
ISemaphore.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.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 acquiredInstanceDestroyedException
- if the instance is destroyed while waitingInterruptedException
- if the current thread is interruptedpublic boolean tryAcquire(int permits, long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException
ISemaphore
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.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 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 interruptedpublic boolean tryAcquireAttach()
ISemaphore
ISemaphore.tryAcquire()
and ISemaphore.attach()
.tryAcquireAttach
in interface ISemaphore
true
if permit was acquired and attached to
the caller's addresspublic boolean tryAcquireAttach(int permits)
ISemaphore
ISemaphore.tryAcquire(int permits)
and ISemaphore.attach(int)
.tryAcquireAttach
in interface ISemaphore
permits
- the number of permits to try to acquire and attachtrue
if permit(s) were acquired and attached to
the caller's addresspublic boolean tryAcquireAttach(long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException
ISemaphore
InstanceDestroyedException
will be thrown.
See ISemaphore.tryAcquire(long timeout, TimeUnit unit)
and ISemaphore.attach()
.tryAcquireAttach
in interface ISemaphore
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 interruptedpublic boolean tryAcquireAttach(int permits, long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException
ISemaphore
ISemaphore.tryAcquire(int permits, long timeout, TimeUnit unit)
and ISemaphore.attach(int)
.tryAcquireAttach
in interface ISemaphore
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 interruptedpublic Instance.InstanceType getInstanceType()
Instance
getInstanceType
in interface Instance
public void destroy()
Instance
public Object getId()
Instance
public String getName()
ISemaphore
getName
in interface ISemaphore
public LocalSemaphoreStats getLocalSemaphoreStats()
getLocalSemaphoreStats
in interface ISemaphore
Copyright © 2013 Hazelcast, Inc.. All rights reserved.