|
Oracle® Coherence Java API Reference Release 12.1.2.0.3 E26043-02 |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object
com.tangosol.util.Base
com.tangosol.util.ThreadGate
public class ThreadGate
Use this class in cases that large numbers of threads can operate concurrently with an additional requirement that all threads be blocked for certain operations. The algorithm is based on a gate concept, allowing threads in (enter) and out (exit), but occasionally shutting the gate (close) such that other threads cannot enter and exit. However, since threads may "be inside", the gate cannot fully close until they leave (exit). Once all threads are out, the gate is closed, and can be re-opened (open) or permanently closed (destroy).
Each call to enter requires a corresponding call to exit, similar to the JVM implementation of the "synchronized" keyword that places a monitorenter op that the beginning of the synchronized portion and protects the synchronized portion with a try..finally construct that ensures the execution of a monitorexit op. For example, the following would ensure proper clean-up using a ThreadGate:
gate.enter(); try { ... } finally { gate.exit(); }
Similarly, each call to close() should be matched with a call to open(), unless the gate is being destroyed:
gate.close(); try { ... } finally { gate.open(); }
or:
gate.close(); gate.destroy();
The enter/exit calls can be nested; the same thread can invoke enter multiple times as long as exit is invoked a corresponding number of times. The close/open calls work in the same manner. Lastly, the thread that closes the gate may continue to enter/exit the gate even when it is closed since that thread has exclusive control of the gate.
Nested Class Summary | |
---|---|
static class |
ThreadGate.ThreadLocalCounter Specialization of ThreadLocalObject that can be used for efficient thread local long counters. |
Field Summary | |
---|---|
static int |
GATE_CLOSED GATE_CLOSED: A single thread is inside the gates; other threads cannot enter. |
static int |
GATE_CLOSING GATE_CLOSING: A thread is waiting to be the only thread inside the gates; other threads can only exit. |
static int |
GATE_DESTROYED GATE_DESTROYED: Life-cycle is complete; the object is no longer usable. |
static int |
GATE_OPEN GATE_OPEN: Threads may enter and exit the gates. |
Constructor Summary | |
---|---|
ThreadGate() Default constructor. |
Method Summary | |
---|---|
boolean |
barEntry(long cMillis) Bar entry of the thread gate by other threads, but do not wait for the gate to close. |
boolean |
close(long cMillis) Close the gate. |
void |
destroy() Destroy the thread gate. |
protected long |
doWait(long cMillis) Wait up to the specified number of milliseconds for notification. |
boolean |
enter(long cMillis) Enter the gate. |
void |
exit() Exit the gate. |
int |
getActiveCount() Return the number of unmatched completed enter calls. |
int |
getCloseCount() Return the number of unmatched completed close/barEntry calls. |
protected java.lang.Thread |
getClosingThread() Return the thread that is closing the gates. |
int |
getStatus() Return the current thread gate status. |
protected long |
getVersion() Return the total number of times the gate has been fully opened. |
boolean |
isClosed() Determine if any thread has closed the gate. |
boolean |
isClosedByCurrentThread() Determine if the current thread has closed the gate. |
boolean |
isEnteredByCurrentThread() Determines if the current thread has entered the gate and not yet exited. |
void |
open() Re-open the closed gate. |
protected void |
setCloseCount(int cClose) Specify the number of unmatched completed close/barEntry calls. |
protected void |
setClosingThread(java.lang.Thread thread) Specify the thread that is closing the gates. |
protected void |
setVersion(long cVersion) Specify the total number of times the gate has been fully opened. |
java.lang.String |
toString() Provide a human-readable representation of this ThreadGate. |
protected int |
updateStatus(int nStatus) Update the current thread gate status, without changing the active count. |
Field Detail |
---|
public static final int GATE_OPEN
public static final int GATE_CLOSING
public static final int GATE_CLOSED
public static final int GATE_DESTROYED
Constructor Detail |
---|
public ThreadGate()
Method Detail |
---|
public boolean barEntry(long cMillis)
gate.barEntry(-1);
try
{
// processing that does not require the gate to be closed
// ...
}
finally
{
gate.close(-1);
try
{
// processing that does require the gate to be closed
// ...
}
finally
{
gate.open(); // matches gate.close()
}
gate.open(); // matches gate.barEntry()
}
cMillis
- maximum number of milliseconds to wait; pass -1 for forever or 0 for no waitpublic boolean close(long cMillis)
Gate.open()
method.close
in interface Gate
cMillis
- maximum number of milliseconds to wait; pass -1 to wait indefinitely or 0 to return immediatelypublic void destroy()
public boolean enter(long cMillis)
Gate.exit()
method.enter
in interface Gate
cMillis
- maximum number of milliseconds to wait; pass -1 to wait indefinitely or 0 to return immediatelypublic void exit()
Gate.enter(long)
method.exit
in interface Gate
public void open()
open
in interface Gate
public boolean isClosedByCurrentThread()
isClosedByCurrentThread
in interface Gate
public boolean isEnteredByCurrentThread()
isEnteredByCurrentThread
in interface Gate
public boolean isClosed()
isClosed
in interface Gate
protected long doWait(long cMillis)
public int getActiveCount()
protected long getVersion()
protected void setVersion(long cVersion)
The caller must have the gate closed.
public int getCloseCount()
protected void setCloseCount(int cClose)
The caller must have the gate closed/closing.
protected java.lang.Thread getClosingThread()
protected void setClosingThread(java.lang.Thread thread)
The caller must be synchronized on the ThreadGate.
public int getStatus()
protected int updateStatus(int nStatus)
The caller must hold synchronization on the ThreadGate.
nStatus
- the new statuspublic java.lang.String toString()
|
Oracle® Coherence Java API Reference Release 12.1.2.0.3 E26043-02 |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |