Class EntryExitManager


  • public class EntryExitManager
    extends java.lang.Object
    Manages threads entering and exiting a notional region. Entry is controlled by a gate, and exit is signalled by Completion. Can block threads entering, abort waiting threads and wait for threads which have entered to exit.

    When the gate is open, threads are not prevented from entering. When the gate is closed, threads will block on enter(...), until the gate is opened (by some other thread).

    Threads which enter the region must leave it by calling exit(). This will signal the exit of the thread. The manager offers a method waitToClear(...) which will block until all the currently entered threads have exited the region. Threads which enter during waitToClear(long, java.util.concurrent.TimeUnit) are not detected.

    closeGate()
    will close the gate to all threads,
    openGate()
    will open the gate, unblocking all waiting threads,
    enter(...)
    will allow the calling thread to enter the region, or block if the gate is closed,
    exit()
    will signal the calling thread to exit the region,
    waitToClear(...)
    will block until all the threads currently in the region have exited,
    abortWaiters()
    will reject all waiting threads with an AbortedException.
    • Constructor Summary

      Constructors 
      Constructor Description
      EntryExitManager()
      Create an EntryExitManager, initially closed.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void abortWaiters()
      Abort all threads waiting to enter with an AbortedException.
      boolean closeGate()
      Close the gate, if allowed, so subsequent enter()ing threads will block.
      boolean enter​(long timeout, java.util.concurrent.TimeUnit unit)
      Returns true immediately if the gate is open.
      boolean enter​(TimeTracker tt)
      Returns true immediately if the gate is open.
      void exit()
      This thread is exiting the region.
      boolean isClosed()
      Is the gate closed?
      boolean openGate()
      Opens the gate and wakes up all waiting threads.
      boolean waitToClear​(long timeout, java.util.concurrent.TimeUnit unit)
      Wait for current threads to exit region.
      boolean waitToClear​(TimeTracker tt)
      Wait for current threads to exit region.
      • Methods inherited from class java.lang.Object

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

      • EntryExitManager

        public EntryExitManager()
        Create an EntryExitManager, initially closed.
    • Method Detail

      • isClosed

        public boolean isClosed()
        Is the gate closed?
        Returns:
        true if the gate is closed, false otherwise.
      • closeGate

        public boolean closeGate()
        Close the gate, if allowed, so subsequent enter()ing threads will block.
        Returns:
        true in all cases.
      • openGate

        public boolean openGate()
        Opens the gate and wakes up all waiting threads. Does not block.
        Returns:
        true in all cases.
      • enter

        public boolean enter​(long timeout,
                             java.util.concurrent.TimeUnit unit)
                      throws java.lang.InterruptedException,
                             AbortedException
        Returns true immediately if the gate is open. Otherwise if the gate is closed the thread blocks until one of the following occurs:
        gate is opened (by another thread);
        timeout expires before gate is opened.
        Parameters:
        timeout - the time to wait for the gate to open.
        unit - the time unit of the timeout argument.
        Returns:
        false if timeout was reached before gate opens; true if gate is open or opens while we are waiting.
        Throws:
        java.lang.InterruptedException - if the callers thread is interrupted while waiting.
        AbortedException - if this thread is aborted by a stop() or close() while waiting.
      • enter

        public boolean enter​(TimeTracker tt)
                      throws java.lang.InterruptedException,
                             AbortedException
        Returns true immediately if the gate is open. Otherwise if the gate is closed the thread blocks until one of the following occurs:
        gate is opened (by another thread);
        timeout expires before gate is opened.
        Parameters:
        tt - the time tracker used to wait for the gate to open.
        Returns:
        false if timeout was reached before gate opens; true if gate is open or opens while we are waiting.
        Throws:
        java.lang.InterruptedException - if the callers thread is interrupted while waiting.
        AbortedException - if this thread is aborted by a stop() or close() while waiting.
      • exit

        public void exit()
        This thread is exiting the region. Must be called eventually by the thread that entered the region.
      • waitToClear

        public boolean waitToClear​(long timeout,
                                   java.util.concurrent.TimeUnit unit)
                            throws java.lang.InterruptedException
        Wait for current threads to exit region.
        Parameters:
        timeout - max time to wait in units.
        unit - of time measurement for timeout.
        Returns:
        true if they all exited in time, false otherwise.
        Throws:
        java.lang.InterruptedException - if thread is interrupted and is waiting.
      • waitToClear

        public boolean waitToClear​(TimeTracker tt)
                            throws java.lang.InterruptedException
        Wait for current threads to exit region.
        Parameters:
        tt - timeout tracker.
        Returns:
        true if they all exited in time, false otherwise.
        Throws:
        java.lang.InterruptedException - if thread is interrupted and is waiting.
      • abortWaiters

        public void abortWaiters()
        Abort all threads waiting to enter with an AbortedException.