Class SpinLock

java.lang.Object
org.wildfly.common.lock.SpinLock
All Implemented Interfaces:
Lock, ExtendedLock

public class SpinLock extends Object implements ExtendedLock
A spin lock. Such locks are designed to only be held for a very short time - for example, long enough to compare and swap two fields. The lock may degrade to yielding the thread after a certain number of spins if it is held for too long.

Spin locks do not support conditions, and they do not support timed waiting. Normally only the uninterruptible lock, tryLock, and unlock methods should be used to control the lock.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a new instance.
    SpinLock(int spinLimit)
    Construct a new instance with the given spin limit.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Determine if this lock is fair.
    boolean
    Determine if this spin lock is held by the calling thread.
    boolean
    Determine if this spin lock is held.
    void
    Acquire the lock by spinning until it is held.
    void
    Acquire the lock by spinning until it is held or the thread is interrupted.
    Unsupported.
    boolean
    Try to acquire the lock, returning immediately whether or not the lock was acquired.
    boolean
    tryLock(long time, TimeUnit unit)
    Unsupported.
    void
    Release the lock.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SpinLock

      public SpinLock()
      Construct a new instance.
    • SpinLock

      public SpinLock(int spinLimit)
      Construct a new instance with the given spin limit.
      Parameters:
      spinLimit - the spin limit to use for this instance
  • Method Details

    • isLocked

      public boolean isLocked()
      Determine if this spin lock is held. Useful for assertions.
      Specified by:
      isLocked in interface ExtendedLock
      Returns:
      true if the lock is held by any thread, false otherwise
    • isHeldByCurrentThread

      public boolean isHeldByCurrentThread()
      Determine if this spin lock is held by the calling thread. Useful for assertions.
      Specified by:
      isHeldByCurrentThread in interface ExtendedLock
      Returns:
      true if the lock is held by the calling thread, false otherwise
    • isFair

      public boolean isFair()
      Determine if this lock is fair.
      Specified by:
      isFair in interface ExtendedLock
      Returns:
      true; the lock is fair
    • lock

      public void lock()
      Acquire the lock by spinning until it is held.
      Specified by:
      lock in interface Lock
    • lockInterruptibly

      public void lockInterruptibly() throws InterruptedException
      Acquire the lock by spinning until it is held or the thread is interrupted.
      Specified by:
      lockInterruptibly in interface Lock
      Throws:
      InterruptedException - if the thread is interrupted before the lock can be acquired
    • tryLock

      public boolean tryLock()
      Try to acquire the lock, returning immediately whether or not the lock was acquired.
      Specified by:
      tryLock in interface Lock
      Returns:
      true if the lock was acquired, false otherwise
    • unlock

      public void unlock()
      Release the lock.
      Specified by:
      unlock in interface Lock
      Throws:
      IllegalMonitorStateException - if the lock is not held by the current thread
    • tryLock

      public boolean tryLock(long time, TimeUnit unit) throws UnsupportedOperationException
      Unsupported.
      Specified by:
      tryLock in interface Lock
      Parameters:
      time - ignored
      unit - ignored
      Returns:
      nothing
      Throws:
      UnsupportedOperationException - always
    • newCondition

      public Condition newCondition() throws UnsupportedOperationException
      Unsupported.
      Specified by:
      newCondition in interface Lock
      Returns:
      nothing
      Throws:
      UnsupportedOperationException - always