All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fireflysource.common.concurrent.Locker Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
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 { } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy