    01: /**
    02:    This gate makes a thread wait until another
    03:    thread opens the gate.
    04: */
    05: public class Gate
    06: {
    07:    /**
    08:       Checks whether the gate is active. An inactive
    09:       gate does not cause a thread to wait.
    10:       @return true if this gate is active
    11:    */
    12:    public boolean isActive()
    13:    {
    14:       return active;
    15:    }
    16:
    17:    /**
    18:       Activates or deactivates this gate.
    19:       @param newValue true to activate, false to deactivate
    20:    */
    21:    public synchronized void setActive(boolean newValue)
    22:    {
    23:       active = newValue;
    24:       notifyAll();
    25:    }
    26:
    27:    /**
    28:       Opens or closes this gate. An open gate allows
    29:       waiting threads to proceed.
    30:       @param newValue true to open, false to close
    31:    */
    32:    public synchronized void setOpen(boolean newValue)
    33:    {
    34:       active = true;
    35:       opened = newValue;
    36:       notifyAll();
    37:    }
    38:
    39:    /**
    40:       Waits for this gate to open, and closes it again
    41:       after the gate has been opened.
    42:    */
    43:    public synchronized void waitForOpen()
    44:       throws InterruptedException
    45:    {
    46:       while (active && !opened)
    47:          wait();
    48:       opened = false;
    49:    }
    50:
    51:    private boolean active;
    52:    private boolean opened;
    53: }