- java.lang.Object
-
- com.rabbitmq.jms.util.EntryExitManager
-
public class EntryExitManager extends java.lang.ObjectManages threads entering and exiting a notional region. Entry is controlled by a gate, and exit is signalled byCompletion. 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 methodwaitToClear(...)which will block until all the currently entered threads have exited the region. Threads which enter duringwaitToClear(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 anEntryExitManager, initially closed.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabortWaiters()Abort all threads waiting to enter with anAbortedException.booleancloseGate()Close the gate, if allowed, so subsequententer()ing threads will block.booleanenter(long timeout, java.util.concurrent.TimeUnit unit)Returnstrueimmediately if the gate is open.booleanenter(TimeTracker tt)Returnstrueimmediately if the gate is open.voidexit()This thread is exiting the region.booleanisClosed()Is the gate closed?booleanopenGate()Opens the gate and wakes up all waiting threads.booleanwaitToClear(long timeout, java.util.concurrent.TimeUnit unit)Wait for current threads to exit region.booleanwaitToClear(TimeTracker tt)Wait for current threads to exit region.
-
-
-
Method Detail
-
isClosed
public boolean isClosed()
Is the gate closed?- Returns:
trueif the gate is closed,falseotherwise.
-
closeGate
public boolean closeGate()
Close the gate, if allowed, so subsequententer()ing threads will block.- Returns:
truein all cases.
-
openGate
public boolean openGate()
Opens the gate and wakes up all waiting threads. Does not block.- Returns:
truein all cases.
-
enter
public boolean enter(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, AbortedExceptionReturnstrueimmediately 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 thetimeoutargument.- Returns:
falseif timeout was reached before gate opens;trueif 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 astop()orclose()while waiting.
-
enter
public boolean enter(TimeTracker tt) throws java.lang.InterruptedException, AbortedException
Returnstrueimmediately 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:
falseif timeout was reached before gate opens;trueif 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 astop()orclose()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.InterruptedExceptionWait for current threads to exit region.- Parameters:
timeout- max time to wait inunits.unit- of time measurement fortimeout.- Returns:
trueif they all exited in time,falseotherwise.- 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:
trueif they all exited in time,falseotherwise.- Throws:
java.lang.InterruptedException- if thread is interrupted and is waiting.
-
abortWaiters
public void abortWaiters()
Abort all threads waiting to enter with anAbortedException.
-
-