Example usage for java.util.concurrent.locks ReentrantLock tryLock

List of usage examples for java.util.concurrent.locks ReentrantLock tryLock

Introduction

In this page you can find the example usage for java.util.concurrent.locks ReentrantLock tryLock.

Prototype

public boolean tryLock() 

Source Link

Document

Acquires the lock only if it is not held by another thread at the time of invocation.

Usage

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;
    }
}