com.hazelcast.concurrent.semaphore
Class SemaphoreProxy

java.lang.Object
  extended by com.hazelcast.spi.AbstractDistributedObject<SemaphoreService>
      extended by com.hazelcast.concurrent.semaphore.SemaphoreProxy
All Implemented Interfaces:
DistributedObject, ISemaphore

public class SemaphoreProxy
extends AbstractDistributedObject<SemaphoreService>
implements ISemaphore


Field Summary
 
Fields inherited from class com.hazelcast.spi.AbstractDistributedObject
PARTITIONING_STRATEGY
 
Constructor Summary
SemaphoreProxy(String name, SemaphoreService service, NodeEngine nodeEngine)
           
 
Method Summary
 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 immediately available.
 String getName()
          Returns the name of this ISemaphore instance.
 String getServiceName()
          Returns the service name for this object.
 boolean init(int permits)
          Try to initialize this ISemaphore instance with the given permit count
 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.
 String toString()
           
 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.
 
Methods inherited from class com.hazelcast.spi.AbstractDistributedObject
destroy, equals, getId, getNameAsPartitionAwareData, getNodeEngine, getPartitionKey, getService, hashCode, invalidate, postDestroy, throwNotActiveException
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.hazelcast.core.DistributedObject
destroy, getId, getPartitionKey
 

Constructor Detail

SemaphoreProxy

public SemaphoreProxy(String name,
                      SemaphoreService service,
                      NodeEngine nodeEngine)
Method Detail

getName

public String getName()
Description copied from interface: ISemaphore
Returns the name of this ISemaphore instance.

Specified by:
getName in interface DistributedObject
Specified by:
getName in interface ISemaphore
Returns:
name of this instance

init

public boolean init(int permits)
Description copied from interface: ISemaphore
Try to initialize this ISemaphore instance with the given permit count

Specified by:
init in interface ISemaphore
Parameters:
permits - the given permit count
Returns:
true if initialization success

acquire

public void acquire()
             throws InterruptedException
Description copied from interface: 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:

If the current thread:

then InterruptedException is thrown and the current thread's interrupted status is cleared.

Specified by:
acquire in interface ISemaphore
Throws:
InterruptedException - if the current thread is interrupted

acquire

public void acquire(int permits)
             throws InterruptedException
Description copied from interface: 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:

If the current thread:

then InterruptedException is thrown and the current thread's interrupted status is cleared.

Specified by:
acquire in interface ISemaphore
Parameters:
permits - the number of permits to acquire
Throws:
InterruptedException - if the current thread is interrupted

availablePermits

public int availablePermits()
Description copied from interface: ISemaphore
Returns the current number of permits currently available in this semaphore.

Specified by:
availablePermits in interface ISemaphore
Returns:
the number of permits available in this semaphore

drainPermits

public int drainPermits()
Description copied from interface: ISemaphore
Acquires and returns all permits that are immediately available.

Specified by:
drainPermits in interface ISemaphore
Returns:
the number of permits drained

reducePermits

public void reducePermits(int reduction)
Description copied from interface: ISemaphore
Shrinks the number of available permits by the indicated reduction. This method differs from acquire in that it does not block waiting for permits to become available.

Specified by:
reducePermits in interface ISemaphore
Parameters:
reduction - the number of permits to remove

release

public void release()
Description copied from interface: ISemaphore
Releases a permit, increasing the number of available permits by one. If any threads in the cluster are trying to acquire a permit, then one is selected and given the permit that was just released.

There is no requirement that a thread that releases a permit must have acquired that permit by calling one of the acquire methods. Correct usage of a semaphore is established by programming convention in the application.

Specified by:
release in interface ISemaphore

release

public void release(int permits)
Description copied from interface: ISemaphore
Releases the given number of permits, increasing the number of available permits by that amount.

There is no requirement that a thread that releases a permit must have acquired that permit by calling one of the acquire methods. Correct usage of a semaphore is established by programming convention in the application.

Specified by:
release in interface ISemaphore
Parameters:
permits - the number of permits to release

tryAcquire

public boolean tryAcquire()
Description copied from interface: ISemaphore
Acquires a permit, if one is available and returns immediately, with the value true, reducing the number of available permits by one.

If no permit is available then this method will return immediately with the value false.

Specified by:
tryAcquire in interface ISemaphore
Returns:
true if a permit was acquired and false otherwise

tryAcquire

public boolean tryAcquire(int permits)
Description copied from interface: ISemaphore
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.

If insufficient permits are available then this method will return immediately with the value false and the number of available permits is unchanged.

Specified by:
tryAcquire in interface ISemaphore
Parameters:
permits - the number of permits to acquire
Returns:
true if the permits were acquired and false otherwise

tryAcquire

public boolean tryAcquire(long timeout,
                          TimeUnit unit)
                   throws InterruptedException
Description copied from interface: ISemaphore
Acquires a permit from this semaphore if one becomes available within the given waiting time and the current thread has not been interrupted.

Acquires a permit if one is available and returns immediately with the value 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:

If a permit is acquired then the value 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 current thread:

then InterruptedException is thrown and the current thread's interrupted status is cleared.

Specified by:
tryAcquire in interface ISemaphore
Parameters:
timeout - the maximum time to wait for a permit
unit - the time unit of the timeout argument
Returns:
true if a permit was acquired and false if the waiting time elapsed before a permit was acquired
Throws:
InterruptedException - if the current thread is interrupted

tryAcquire

public boolean tryAcquire(int permits,
                          long timeout,
                          TimeUnit unit)
                   throws InterruptedException
Description copied from interface: ISemaphore
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.

If insufficient permits are available, then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:

If the permits are acquired then true is returned.

If the specified waiting time elapses then false is returned. If the time is less than or equal to zero, the method will not wait at all.

If the current thread:

then InterruptedException is thrown and the current thread's interrupted status is cleared.

Specified by:
tryAcquire in interface ISemaphore
Parameters:
permits - the number of permits to acquire
timeout - the maximum time to wait for the permits
unit - the time unit of the timeout argument
Returns:
true if all permits were acquired, false if the waiting time elapsed before all permits could be acquired
Throws:
InterruptedException - if the current thread is interrupted

getServiceName

public String getServiceName()
Description copied from interface: DistributedObject
Returns the service name for this object.

Specified by:
getServiceName in interface DistributedObject
Specified by:
getServiceName in class AbstractDistributedObject<SemaphoreService>
Returns:
the service name for this object.

toString

public String toString()
Overrides:
toString in class AbstractDistributedObject<SemaphoreService>


Copyright © 2015 Hazelcast, Inc.. All Rights Reserved.