List of usage examples for java.util.concurrent.locks ReentrantLock tryLock
public boolean tryLock()
From source file:io.nuclei.splice.internal.FileManager.java
public static ReentrantLock lock(FileRef ref) { ReentrantLock lock; boolean locked; synchronized (sLocks) { lock = sLocks.get(ref);/*from w w w .j av a 2 s . c o m*/ if (lock == null) { lock = new ReentrantLock(); sLocks.put(ref, lock); } locked = lock.tryLock(); } if (!locked) { while (true) { synchronized (sLocks) { try { sLocks.wait(); } catch (InterruptedException ignore) { } if (lock.tryLock()) { ReentrantLock newLock = sLocks.get(ref); if (newLock == null) { sLocks.put(ref, lock); break; } else if (newLock != lock) { lock.unlock(); lock = newLock; if (lock.tryLock()) { break; } } else { break; } } } } } return lock; }
From source file:org.broadleafcommerce.core.web.order.SessionOrderLockManager.java
@Override public Object acquireLockIfAvailable(Order order) { ReentrantLock lockObject = getSessionLock(); boolean locked = lockObject.tryLock(); return locked ? lockObject : null; }
From source file:io.tilt.minka.business.impl.SemaphoreImpl.java
private boolean lock(final Action action, final boolean blockThread, final Action cause) { if (logger.isDebugEnabled()) { if (cause == null) { logger.debug("{}: {} is {}", getClass().getSimpleName(), action, blockThread ? "Locking" : "TryLocking"); } else {/*from w ww . j a v a 2 s.c o m*/ logger.debug("{}: {} is {}: {}", getClass().getSimpleName(), cause, blockThread ? "Locking" : "TryLocking", action); } } switch (action.getScope()) { case GLOBAL: return locks.acquireDistributedLock(nameForDistributedLock(action, null), true, DISTRIBUTED_LOCK_WAIT_MS, TimeUnit.MILLISECONDS); case LOCAL: ReentrantLock lock = g(action); if (blockThread) { lock.lock(); return true; } else { return lock.tryLock(); } default: logger.error("{}: what the heck ? --> {}", getClass().getSimpleName(), action.getScope()); return false; } }