Class ReusableLatch


  • public class ReusableLatch
    extends java.lang.Object
    This class is similar to CountDownLatch and Semaphore. Many threads can await() the the call of release() (blocking until it is invoked.) After this all calls to await() will not block until reset is called.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void await()
      Block until another thread calls release, or the thread is interrupted.
      void await​(long timeoutMillis)
      Block until another thread calls release, or the thread is interrupted.
      void awaitUninterruptibly()
      Block until release is called by another thread.
      int getQueueLength()
      Gets the number of threads waiting.
      boolean isReleased()
      Returns whether or not release has been called and threads can call await without blocking.
      void release()
      Allow all waiting threads to go through, and all future threads to proceed without blocking.
      void reset()
      Resets the latch to an un-release state.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ReusableLatch

        public ReusableLatch()
      • ReusableLatch

        public ReusableLatch​(boolean startReleased)
    • Method Detail

      • await

        public void await()
                   throws java.lang.InterruptedException
        Block until another thread calls release, or the thread is interrupted.
        Throws:
        java.lang.InterruptedException - If the operation was interrupted while waiting.
      • await

        public void await​(long timeoutMillis)
                   throws java.lang.InterruptedException,
                          java.util.concurrent.TimeoutException
        Block until another thread calls release, or the thread is interrupted.
        Parameters:
        timeoutMillis - Timeout, in milliseconds, to wait for the release.
        Throws:
        java.lang.InterruptedException - If the operation was interrupted while waiting.
        java.util.concurrent.TimeoutException - If the timeout expired prior to being able to await the release.
      • awaitUninterruptibly

        public void awaitUninterruptibly()
        Block until release is called by another thread.
      • release

        public void release()
        Allow all waiting threads to go through, and all future threads to proceed without blocking.
      • isReleased

        public boolean isReleased()
        Returns whether or not release has been called and threads can call await without blocking.
        Returns:
        True if the latch is set to release state.
      • reset

        public void reset()
        Resets the latch to an un-release state.
      • getQueueLength

        public int getQueueLength()
        Gets the number of threads waiting.
        Returns:
        The number of threads waiting.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object