Class LifecycleLock


  • public class LifecycleLock
    extends Object
    A synchronization tool for lifecycled objects (see 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 Detail

      • LifecycleLock

        public LifecycleLock()
    • Method Detail

      • canStart

        public 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.
      • exitStart

        public void exitStart()
        Must be called before exit from start() on the lifecycled object, usually in a finally block.
        Throws:
        IllegalMonitorStateException - if canStart() is not yet called on this LifecycleLock
      • isStarted

        public boolean isStarted()
        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.
      • awaitStarted

        public boolean awaitStarted()
        Awaits until 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.
      • awaitStarted

        public 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. 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.
      • canStop

        public boolean canStop()
        Stop latch, only one canStop() call in any thread on this LifecycleLock object could return true. If started() wasn't called on this LifecycleLock object, always returns false.
        Throws:
        IllegalMonitorStateException - if exitStart() 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, use exitStopAndReset() instead.
        Throws:
        IllegalMonitorStateException - if canStop() is not yet called on this LifecycleLock
      • exitStopAndReset

        public void exitStopAndReset()
        Finalizes stopping the LifecycleLock and resets it, so that 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.
        Throws:
        IllegalMonitorStateException - if canStop() is not yet called on this LifecycleLock