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

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

Introduction

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

Prototype

boolean tryLock(long time, TimeUnit unit) throws InterruptedException;

Source Link

Document

Acquires the lock if it is free within the given waiting time and the current thread has not been Thread#interrupt interrupted .

Usage

From source file:org.rhq.core.pc.PluginContainer.java

private Lock obtainReadLock() {
    // try to obtain the lock, but if we can't get the lock in 60 seconds,
    // keep going. The PC is usually fine within seconds after its initializes,
    // so not getting this lock within 60 seconds probably isn't detrimental.
    // But if there is a deadlock, blocking forever here would be detrimental,
    // so we do not do it. We'll just log a warning and let the thread keep going.
    Lock readLock = rwLock.readLock();
    try {//from w  w  w .j  a v  a 2  s .co  m
        if (!readLock.tryLock(60L, TimeUnit.SECONDS)) {
            String msg = "There may be a deadlock in the plugin container.";
            //noinspection ThrowableInstanceNeverThrown
            log.warn(msg, new Throwable(msg));
            readLock = null;
        }
    } catch (InterruptedException e) {
        readLock = null;
    }
    return readLock;
}

From source file:org.rhq.core.pc.PluginContainer.java

private Lock obtainWriteLock() {
    // try to obtain the lock, but if we can't get the lock in 60 seconds,
    // keep going. The PC is usually fine within seconds after its initializes,
    // so not getting this lock within 60 seconds probably isn't detrimental.
    // But if there is a deadlock, blocking forever here would be detrimental,
    // so we do not do it. We'll just log a warning and let the thread keep going.
    Lock writeLock = rwLock.writeLock();
    try {//from  w ww .  j ava  2 s .  co m
        if (!writeLock.tryLock(60L, TimeUnit.SECONDS)) {
            String msg = "There may be a deadlock in the plugin container.";
            //noinspection ThrowableInstanceNeverThrown
            log.warn(msg, new Throwable(msg));
            writeLock = null;
        }
    } catch (InterruptedException e) {
        writeLock = null;
    }
    return writeLock;
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable/*from   w  w  w. j  av a2 s .co m*/
public void testTwoThreadsSecondFailsToGetLock() throws Exception {
    final RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(),
            this.registryKey);
    final Lock lock1 = registry.obtain("foo");
    lock1.lockInterruptibly();
    final AtomicBoolean locked = new AtomicBoolean();
    final CountDownLatch latch = new CountDownLatch(1);
    Future<Object> result = Executors.newSingleThreadExecutor().submit(() -> {
        Lock lock2 = registry.obtain("foo");
        locked.set(lock2.tryLock(200, TimeUnit.MILLISECONDS));
        latch.countDown();
        try {
            lock2.unlock();
        } catch (IllegalStateException ise) {
            return ise;
        }
        return null;
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertFalse(locked.get());
    lock1.unlock();
    Object ise = result.get(10, TimeUnit.SECONDS);
    assertThat(ise, instanceOf(IllegalStateException.class));
    assertThat(((Exception) ise).getMessage(), containsString("Lock is not locked"));
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}