Class VariableLatch


  • @ThreadSafe
    public class VariableLatch
    extends Object
    A latch that works similarly to CountDownLatch except that it can also increase the count dynamically.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  VariableLatch.Sync
      Synchronization control For CountDownLatch.
    • Constructor Summary

      Constructors 
      Constructor Description
      VariableLatch​(int count)
      Constructs a CountDownLatch initialized with the given count.
    • Constructor Detail

      • VariableLatch

        public VariableLatch​(int count)
        Constructs a CountDownLatch initialized with the given count.
        Parameters:
        count - the number of times countDown() must be invoked before threads can pass through await()
        Throws:
        IllegalArgumentException - if count is negative
    • Method Detail

      • create

        public static VariableLatch create()
        Create a new variable latch.
        Returns:
        the variable latch; never null
      • create

        public static VariableLatch create​(int initialValue)
        Create a new variable latch.
        Parameters:
        initialValue - the initial number of latches
        Returns:
        the variable latch; never null
      • await

        public void await()
                   throws InterruptedException
        Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted.

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

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

        • The count reaches zero due to invocations of the countDown() method; or
        • Some other thread interrupts the current thread.

        If the current thread:

        • has its interrupted status set on entry to this method; or
        • is interrupted while waiting,
        then InterruptedException is thrown and the current thread's interrupted status is cleared.
        Throws:
        InterruptedException - if the current thread is interrupted while waiting
      • await

        public boolean await​(long timeout,
                             TimeUnit unit)
                      throws InterruptedException
        Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted, 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 three things happen:

        • The count reaches zero due to invocations of the countDown() method; or
        • Some other thread interrupts the current thread; or
        • The specified waiting time elapses.

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

        If the current thread:

        • has its interrupted status set on entry to this method; or
        • is interrupted while waiting,
        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 while waiting
      • countDown

        public 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 then all waiting threads are re-enabled for thread scheduling purposes.

        If the current count equals zero then nothing happens.

      • countDown

        public void countDown​(int count)
        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 then all waiting threads are re-enabled for thread scheduling purposes.

        If the current count equals zero then nothing happens.

        Parameters:
        count - the number of counts to decrease
      • countUp

        public void countUp()
        Increments the count of the latch by one.
      • countUp

        public void countUp​(int count)
        Increments the count of the latch by a positive number.
        Parameters:
        count - the number of counts to increase
      • getCount

        public long getCount()
        Returns the current count.

        This method is typically used for debugging and testing purposes.

        Returns:
        the current count
      • toString

        public String toString()
        Returns a string identifying this latch, as well as its state. The state, in brackets, includes the String "Count =" followed by the current count.
        Overrides:
        toString in class Object
        Returns:
        a string identifying this latch, as well as its state