com.hazelcast.core
Interface ICountDownLatch

All Superinterfaces:
DistributedObject

public interface ICountDownLatch
extends DistributedObject

ICountDownLatch is a backed-up distributed alternative to the java.util.concurrent.CountDownLatch.

ICountDownLatch is a cluster-wide synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

There are a few differences compared to the ICountDownLatch:

  1. the ICountDownLatch count can be re-set using trySetCount(int) after a countdown has finished but not during an active count. This allows the same latch instance to be reused.
  2. there is no await() method to do an unbound wait since this is undesirable in a distributed application: it can happen that for example a cluster is split or that the master and replica's all die. So in most cases it is best to configure an explicit timeout so have the ability to deal with these situations.


Method Summary
 boolean await(long timeout, TimeUnit unit)
          Causes the current thread to wait until the latch has counted down to zero, an exception is thrown, or the specified waiting time elapses.
 void countDown()
          Decrements the count of the latch, releasing all waiting threads if the count reaches zero.
 int getCount()
          Returns the current count.
 boolean trySetCount(int count)
          Sets the count to the given value if the current count is zero.
 
Methods inherited from interface com.hazelcast.core.DistributedObject
destroy, getId, getName, getPartitionKey, getServiceName
 

Method Detail

await

boolean await(long timeout,
              TimeUnit unit)
              throws InterruptedException
Causes the current thread to wait until the latch has counted down to zero, an exception is thrown, or the specified waiting time elapses.

If the current count is zero then this method returns immediately with the value true.

If the current count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of five things happen:

If the count reaches zero then the method returns with the value true.

If the current thread:

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

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.

Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
true if the count reached zero and false if the waiting time elapsed before the count reached zero
Throws:
InterruptedException - if the current thread is interrupted
IllegalStateException - if hazelcast instance is shutdown while waiting

countDown

void countDown()
Decrements the count of the latch, releasing all waiting threads if the count reaches zero.

If the current count is greater than zero then it is decremented. If the new count is zero:

If the current count equals zero then nothing happens.


getCount

int getCount()
Returns the current count.

Returns:
current count

trySetCount

boolean trySetCount(int count)
Sets the count to the given value if the current count is zero.

If count is not zero then this method does nothing and returns false.

Parameters:
count - the number of times countDown() must be invoked before threads can pass through await(long, java.util.concurrent.TimeUnit)
Returns:
true if the new count was set or false if the current count is not zero
Throws:
IllegalArgumentException - if count is negative


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