|
CoherenceTM v3.3 Copyright© 2000-2007 by Oracle Corporation |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.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 |
close(long cMillis)
Close the thread 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 thread 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 calls. |
protected 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 |
isActiveThread()
Determine if the current thread has entered and not exited. |
void |
open()
After the ThreadGate is closed by a call to close(), it can be re-opened by calling this method. |
protected void |
setCloseCount(int cClose)
Specify the number of unmatched completed close calls. |
protected void |
setClosingThread(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. |
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 close(long cMillis)
cMillis
- maximum number of milliseconds to wait;
pass -1 for forever or 0 for no waitpublic void destroy()
public boolean enter(long cMillis)
cMillis
- maximum number of milliseconds to wait;
pass -1 for forever or 0 for no waitpublic void exit()
public void open()
IllegalStateException
- if the gate is not closed or if open()
is not called by the thread that called close()protected long doWait(long cMillis)
public int getActiveCount()
public boolean isActiveThread()
protected long getVersion()
protected void setVersion(long cVersion)
public int getCloseCount()
protected void setCloseCount(int cClose)
protected Thread getClosingThread()
protected void setClosingThread(Thread thread)
public int getStatus()
protected int updateStatus(int nStatus)
nStatus
- the new status
public String toString()
|
CoherenceTM v3.3 Copyright© 2000-2007 by Oracle Corporation |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |