Show / Hide Table of Contents

Interface Gate

Use a Gate in cases that large numbers of threads can operate concurrently with an additional requirement that all threads be blocked for certain operations.

Namespace: Tangosol.Util
Assembly: Coherence.dll
Syntax
public interface Gate
Remarks

The algorithm is based on a gate concept, allowing threads in Enter(long) and out Exit(), but occasionally shutting the gate Close(long) 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().

Each call to Enter requires a corresponding call to Exit. For example, the following would ensure proper clean-up using a Gate:

gate.Enter();
try
{
    ...
}
finally
{
    gate.Exit();
}

Similarly, each call to Close should be matched with a call to Open:

gate.Close();
try
{
    ...
}
finally
{
    gate.Open();
}

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.

Properties

IsClosedByCurrentThread

Determines if the current thread has Closed the Gate but not yet Opened the Gate.

Declaration
bool IsClosedByCurrentThread { get; }
Property Value
Type Description
bool

IsEnteredByCurrentThread

Determines if the current thread has Entered, but not yet Exited the Gate.

Declaration
bool IsEnteredByCurrentThread { get; }
Property Value
Type Description
bool

Methods

Close(long)

Tries to acquire the exclusive lock, the attempt is willing to wait up the specified millis or -1 to wait infinite.

Declaration
bool Close(long millis)
Parameters
Type Name Description
long millis

If the value is positive the caller will block for this long until either the lock has been acquired. If 0 an immediate attempt is made. If -1 wait an infinite time for the lock to be acquired.

Returns
Type Description
bool

true if the lock has been acquired.

Enter(long)

Tries to acquire the non-exclusive lock, the attempt is willing to wait up the specified millis or -1 to wait infinite.

Declaration
bool Enter(long millis)
Parameters
Type Name Description
long millis

If the value is positive the caller will block for this long until either the lock has been acquired. If 0 an immediate attempt is made. If -1 wait an infinite time for the lock to be acquired.

Returns
Type Description
bool

true if the lock has been acquired.

Exit()

Releases the non-exclusive lock. If the non-exclusive lock has no more matching Enter(millis) from any thread the exclusive lock is released so that threads wanting to Close the Gate can proceed.

Declaration
void Exit()

Open()

Releases the exclusive lock. If the exclusive lock has no more matching Close(millis) the exclusive lock is released. Any Threads waiting to Enter(millis) or Close(millis) will continue executing.

If the thread currently is also holding the non-exclusive lock, the thread will immediately Enter the lock. The thread will have to match all calls to Enter(millis) with the corresponding count of Exit().

Declaration
void Open()
In this article
Back to top Copyright © 2000, 2024, Oracle and/or its affiliates.