Package org.wildfly.common.lock
Class SpinLock
- java.lang.Object
-
- org.wildfly.common.lock.SpinLock
-
- All Implemented Interfaces:
java.util.concurrent.locks.Lock,ExtendedLock
public class SpinLock extends java.lang.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, andunlockmethods should be used to control the lock.
-
-
Constructor Summary
Constructors Constructor Description SpinLock()Construct a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanisFair()Determine if this lock is fair.booleanisHeldByCurrentThread()Determine if this spin lock is held by the calling thread.booleanisLocked()Determine if this spin lock is held.voidlock()Acquire the lock by spinning until it is held.voidlockInterruptibly()Acquire the lock by spinning until it is held or the thread is interrupted.java.util.concurrent.locks.ConditionnewCondition()Unsupported.booleantryLock()Try to acquire the lock, returning immediately whether or not the lock was acquired.booleantryLock(long time, java.util.concurrent.TimeUnit unit)Unsupported.voidunlock()Release the lock.
-
-
-
Method Detail
-
isLocked
public boolean isLocked()
Determine if this spin lock is held. Useful for assertions.- Specified by:
isLockedin interfaceExtendedLock- Returns:
trueif the lock is held by any thread,falseotherwise
-
isHeldByCurrentThread
public boolean isHeldByCurrentThread()
Determine if this spin lock is held by the calling thread. Useful for assertions.- Specified by:
isHeldByCurrentThreadin interfaceExtendedLock- Returns:
trueif the lock is held by the calling thread,falseotherwise
-
isFair
public boolean isFair()
Determine if this lock is fair.- Specified by:
isFairin interfaceExtendedLock- Returns:
true; the lock is fair
-
lock
public void lock()
Acquire the lock by spinning until it is held.- Specified by:
lockin interfacejava.util.concurrent.locks.Lock
-
lockInterruptibly
public void lockInterruptibly() throws java.lang.InterruptedExceptionAcquire the lock by spinning until it is held or the thread is interrupted.- Specified by:
lockInterruptiblyin interfacejava.util.concurrent.locks.Lock- Throws:
java.lang.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:
tryLockin interfacejava.util.concurrent.locks.Lock- Returns:
trueif the lock was acquired,falseotherwise
-
unlock
public void unlock()
Release the lock.- Specified by:
unlockin interfacejava.util.concurrent.locks.Lock- Throws:
java.lang.IllegalMonitorStateException- if the lock is not held by the current thread
-
tryLock
public boolean tryLock(long time, java.util.concurrent.TimeUnit unit) throws java.lang.UnsupportedOperationExceptionUnsupported.- Specified by:
tryLockin interfacejava.util.concurrent.locks.Lock- Parameters:
time- ignoredunit- ignored- Returns:
- nothing
- Throws:
java.lang.UnsupportedOperationException- always
-
newCondition
public java.util.concurrent.locks.Condition newCondition() throws java.lang.UnsupportedOperationExceptionUnsupported.- Specified by:
newConditionin interfacejava.util.concurrent.locks.Lock- Returns:
- nothing
- Throws:
java.lang.UnsupportedOperationException- always
-
-