com.fireflysource.common.concurrent.Locker Maven / Gradle / Ivy
package com.fireflysource.common.concurrent;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* Convenience auto closeable {@link ReentrantLock} wrapper.
*
*
* try (Locker.Lock lock = locker.lock())
* {
* // something
* }
*
*/
public class Locker {
private final ReentrantLock _lock = new ReentrantLock();
private final Lock _unlock = new Lock();
/**
* Acquires the lock.
*
* @return the lock to unlock
*/
public Lock lock() {
_lock.lock();
return _unlock;
}
/**
* @return the lock to unlock
* @deprecated use {@link #lock()} instead
*/
@Deprecated
public Lock lockIfNotHeld() {
return lock();
}
/**
* @return whether this lock has been acquired
*/
public boolean isLocked() {
return _lock.isLocked();
}
/**
* @return a {@link Condition} associated with this lock
*/
public Condition newCondition() {
return _lock.newCondition();
}
/**
* The unlocker object that unlocks when it is closed.
*/
public class Lock implements AutoCloseable {
@Override
public void close() {
_lock.unlock();
}
}
@Deprecated
public class UnLock extends Lock {
}
}