Package org.apache.druid.concurrent
Class LifecycleLock
- java.lang.Object
-
- org.apache.druid.concurrent.LifecycleLock
-
public class LifecycleLock extends Object
A synchronization tool for lifecycled objects (seeLifecycle
, 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 betweenexitStart()
andawaitStarted()
, exitStart() andcanStop()
, if it returnstrue
. 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 Summary
Constructors Constructor Description LifecycleLock()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
awaitStarted()
Awaits untilexitStart()
is called, if needed, and returnstrue
ifstarted()
was called before that.boolean
awaitStarted(long timeout, TimeUnit unit)
Awaits untilexitStart()
is called for at most the specified timeout, and returnstrue
ifstarted()
was called before that.boolean
canStart()
Start latch, only one canStart() call in any thread on this LifecycleLock object could return true, ifexitStopAndReset()
is not called in between.boolean
canStop()
Stop latch, only one canStop() call in any thread on this LifecycleLock object could returntrue
.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 thatcanStart()
could be called again.boolean
isStarted()
Returnstrue
ifstarted()
was called before that.void
started()
Announce the start was successful.
-
-
-
Method Detail
-
canStart
public boolean canStart()
Start latch, only one canStart() call in any thread on this LifecycleLock object could return true, ifexitStopAndReset()
is not called in between.
-
started
public void started()
Announce the start was successful.- Throws:
IllegalMonitorStateException
- ifcanStart()
is not yet called or ifexitStart()
is already called on this LifecycleLock
-
exitStart
public void exitStart()
Must be called before exit from start() on the lifecycled object, usually in a finally block.- Throws:
IllegalMonitorStateException
- ifcanStart()
is not yet called on this LifecycleLock
-
isStarted
public boolean isStarted()
Returnstrue
ifstarted()
was called before that. Returnsfalse
ifstarted()
is not called beforeexitStart()
, or ifcanStop()
is already called on this LifecycleLock.
-
awaitStarted
public boolean awaitStarted()
Awaits untilexitStart()
is called, if needed, and returnstrue
ifstarted()
was called before that. Returnsfalse
ifstarted()
is not called beforeexitStart()
, or ifcanStop()
is already called on this LifecycleLock.
-
awaitStarted
public boolean awaitStarted(long timeout, TimeUnit unit)
Awaits untilexitStart()
is called for at most the specified timeout, and returnstrue
ifstarted()
was called before that. Returnsfalse
ifstarted()
wasn't called beforeexitStart()
, or ifexitStart()
isn't called on this LifecycleLock until the specified timeout expires, or ifcanStop()
is already called on this LifecycleLock.
-
canStop
public boolean canStop()
Stop latch, only one canStop() call in any thread on this LifecycleLock object could returntrue
. Ifstarted()
wasn't called on this LifecycleLock object, always returnsfalse
.- Throws:
IllegalMonitorStateException
- ifexitStart()
are not yet called on this LifecycleLock
-
exitStop
public void exitStop()
Finalizes stopping the LifecycleLock. This method must be called before exit from stop() on this object, usually in a finally block. If you're using a restartable object, useexitStopAndReset()
instead.- Throws:
IllegalMonitorStateException
- ifcanStop()
is not yet called on this LifecycleLock
-
exitStopAndReset
public void exitStopAndReset()
Finalizes stopping the LifecycleLock and resets it, so thatcanStart()
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.- Throws:
IllegalMonitorStateException
- ifcanStop()
is not yet called on this LifecycleLock
-
-