Interface Monitor
- All Superinterfaces:
AutoCloseable
Convenience wrapper to automatically manage the state of an underlying lock, series of locks, or semaphores. This
can be used to ensure that multiple locks or semaphores are acquired in consistent order (avoiding deadlock) and
are automatically released when the critical section of code executes.
Note, per the
AutoCloseable.close()
documentation Monitor
instances are not idempotent unless
explicitly stated, such as in the return of idempotent()
.-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
static Monitor
empty()
Returns an empty monitor.static Monitor
static Monitor
static Monitor
default Monitor
Per the definition ofAutoCloseable.close()
,Monitor
instances are not idempotent.default Monitor
Acquires a monitor which will release the lock after this monitor closes.default Monitor
Acquires a monitor which will release the lock after this monitor closes.default Monitor
Acquires a monitor which will release the semaphore after this monitor closes.default Monitor
Acquires a monitor which will release the lock after this monitor closes.
-
Method Details
-
close
void close()- Specified by:
close
in interfaceAutoCloseable
-
then
Acquires a monitor which will release the lock after this monitor closes.enter(Lock)
- Parameters:
lock
- the lock- Returns:
- a new Monitor
-
then
Acquires a monitor which will release the semaphore after this monitor closes.enter(Semaphore)
- Parameters:
semaphore
- the semaphore- Returns:
- a new Monitor
-
then
Acquires a monitor which will release the lock after this monitor closes.enter(Semaphore,int)
- Parameters:
semaphore
- the semaphorepermits
- the permits to acquire- Returns:
- a new Monitor
-
then
Acquires a monitor which will release the lock after this monitor closes. This ensures that when multiple monitors are chained together then they are released in the reverse order.enter(Semaphore,int)
- Parameters:
other
- the other monitor- Returns:
- a new Monitor
-
empty
Returns an empty monitor. The rationale is that this can be the start of a chain of monitors within a critical section of code ensuring that all get unwound properly as the chain is built.- Returns:
- an empty monitor
-
idempotent
Per the definition ofAutoCloseable.close()
,Monitor
instances are not idempotent. This method returns aMonitor
which is idempotent. Callingclose()
multiple times will ensure that all previously chained calls are only performed once. More complicatedMonitor
instances may be naturally idempotent which means that this may not be necessary. -
enter
- Parameters:
lock
- theLock
instance- Returns:
- a new Monitor
-
enter
- Parameters:
semaphore
- theSemaphore
instance- Returns:
- a new Monitor
-
enter
-