public class LifecycleLock extends Object
Lifecycle,
that need happens-before between start() and other methods and/or to check that the object was successfully started
in other methods.
Guarantees in terms of JMM: happens-before between exitStart() and awaitStarted(),
exitStart() and canStop(), if it returns true.
Example:
class ExampleLifecycledClass
{
final LifecycleLock lifecycleLock = new LifecycleLock();
void start()
{
if (!lifecycleLock.canStart()) {
.. return or throw exception
}
try {
.. do start
lifecycleLock.started();
}
finally {
lifecycleLock.exitStart();
}
}
void otherMethod()
{
Preconditions.checkState(lifecycleLock.awaitStarted());
// all actions done in start() are visible here
.. do stuff
}
void stop()
{
if (!lifecycleLock.canStop()) {
.. return or throw exception
}
// all actions done in start() are visible here
.. do stop
}
}| Constructor and Description |
|---|
LifecycleLock() |
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitStarted()
Awaits until
exitStart() is called, if needed, and returns true if started() was called
before that. |
boolean |
awaitStarted(long timeout,
TimeUnit unit)
Awaits until
exitStart() is called for at most the specified timeout, and returns true if started() was called before that. |
boolean |
canStart()
Start latch, only one canStart() call in any thread on this LifecycleLock object could return true, if
exitStopAndReset() is not called in between. |
boolean |
canStop()
Stop latch, only one canStop() call in any thread on this LifecycleLock object could return
true. |
void |
exitStart()
Must be called before exit from start() on the lifecycled object, usually in a finally block.
|
void |
exitStop()
Finalizes stopping the LifecycleLock.
|
void |
exitStopAndReset()
Finalizes stopping the LifecycleLock and resets it, so that
canStart() could be called again. |
boolean |
isStarted()
Returns
true if started() was called before that. |
void |
started()
Announce the start was successful.
|
public boolean canStart()
exitStopAndReset() is not called in between.public void started()
IllegalMonitorStateException - if canStart() is not yet called or if exitStart() is already
called on this LifecycleLockpublic void exitStart()
IllegalMonitorStateException - if canStart() is not yet called on this LifecycleLockpublic boolean isStarted()
true if started() was called before that. Returns false if started() is
not called before exitStart(), or if canStop() is already called on this LifecycleLock.public boolean awaitStarted()
exitStart() is called, if needed, and returns true if started() was called
before that. Returns false if started() is not called before exitStart(), or if canStop() is already called on this LifecycleLock.public boolean awaitStarted(long timeout,
TimeUnit unit)
exitStart() is called for at most the specified timeout, and returns true if started() was called before that. Returns false if started() wasn't called before exitStart(), or if exitStart() isn't called on this LifecycleLock until the specified timeout expires, or
if canStop() is already called on this LifecycleLock.public boolean canStop()
true. If
started() wasn't called on this LifecycleLock object, always returns false.IllegalMonitorStateException - if exitStart() are not yet called on this LifecycleLockpublic void exitStop()
exitStopAndReset() instead.IllegalMonitorStateException - if canStop() is not yet called on this LifecycleLockpublic void exitStopAndReset()
canStart() could be called again. If this
LifecycleLock is used in a restartable object, this method must be called before exit from stop() on this object,
usually in a finally block.IllegalMonitorStateException - if canStop() is not yet called on this LifecycleLockCopyright © 2011–2023 The Apache Software Foundation. All rights reserved.