Class ReentryGuard.ReentryGuardImpl

  • All Implemented Interfaces:
    ReentryGuard
    Enclosing interface:
    ReentryGuard

    public static class ReentryGuard.ReentryGuardImpl
    extends Object
    implements ReentryGuard
    Default per-thread implementation backed by a ThreadLocal.

    Semantics: a value of Boolean.TRUE indicates the current thread is inside a guarded region. If the ThreadLocal has no value (null), isLocked() treats this as unlocked (returns false).

    Note: this implementation intentionally uses ThreadLocal<Boolean> to avoid global synchronization. The initial state is unlocked.

    Typical usage:
     if (!guard.isLocked()) {
       guard.lock();
       try {
         // guarded work
       } finally {
         guard.unlock();
       }
     }
     
    • Method Detail

      • isLocked

        public boolean isLocked()
        Description copied from interface: ReentryGuard
        Return true if the current thread holds the guard (i.e. is inside a guarded region).

        Implementations typically return false if the current thread has not previously called ReentryGuard.lock() or if the stored value is null.

        Specified by:
        isLocked in interface ReentryGuard
        Returns:
        true if the guard is locked for the current thread, false otherwise
      • lock

        public void lock()
        Description copied from interface: ReentryGuard
        Mark the guard as locked for the current thread.

        Callers must ensure ReentryGuard.unlock() is invoked in a finally block to avoid leaving the guard permanently locked for the thread.

        Specified by:
        lock in interface ReentryGuard
      • unlock

        public void unlock()
        Description copied from interface: ReentryGuard
        Release the guard for the current thread.

        After calling unlock() the ReentryGuard.isLocked() should return false for the current thread (unless lock() is called again).

        Specified by:
        unlock in interface ReentryGuard