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

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

Introduction

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

Prototype

void lockInterruptibly() throws InterruptedException;

Source Link

Document

Acquires the lock unless the current thread is Thread#interrupt interrupted .

Usage

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

@Test
@RedisAvailable//from ww  w . j a v  a2 s  .  c  o m
public void testReentrantLockInterruptibly() throws Exception {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
    for (int i = 0; i < 10; i++) {
        Lock lock1 = registry.obtain("foo");
        lock1.lockInterruptibly();
        try {
            Lock lock2 = registry.obtain("foo");
            assertSame(lock1, lock2);
            lock2.lockInterruptibly();
            try {
                // just get the lock
            } finally {
                lock2.unlock();
            }
        } finally {
            lock1.unlock();
        }
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

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

@Test
@RedisAvailable// w w w. j  a v a 2  s .com
public void testTwoLocks() throws Exception {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
    for (int i = 0; i < 10; i++) {
        Lock lock1 = registry.obtain("foo");
        lock1.lockInterruptibly();
        try {
            Lock lock2 = registry.obtain("bar");
            assertNotSame(lock1, lock2);
            lock2.lockInterruptibly();
            try {
                // just get the lock
            } finally {
                lock2.unlock();
            }
        } finally {
            lock1.unlock();
        }
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

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

@Test
@RedisAvailable/*  w w w . ja v a 2 s .c o 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());
}

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

@Test
@RedisAvailable/*from  w  ww.j  a  v  a  2s .c om*/
public void testTwoThreads() throws Exception {
    final RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(),
            this.registryKey);
    final Lock lock1 = registry.obtain("foo");
    final AtomicBoolean locked = new AtomicBoolean();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final CountDownLatch latch3 = new CountDownLatch(1);
    lock1.lockInterruptibly();
    assertNotNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
    Executors.newSingleThreadExecutor().execute(() -> {
        Lock lock2 = registry.obtain("foo");
        try {
            latch1.countDown();
            lock2.lockInterruptibly();
            assertNotNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
            latch2.await(10, TimeUnit.SECONDS);
            locked.set(true);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            lock2.unlock();
            latch3.countDown();
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertFalse(locked.get());
    lock1.unlock();
    latch2.countDown();
    assertTrue(latch3.await(10, TimeUnit.SECONDS));
    assertTrue(locked.get());
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

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

@Test
@RedisAvailable//from   w ww.  ja v a 2s.co m
public void testTwoThreadsDifferentRegistries() throws Exception {
    final RedisLockRegistry registry1 = new RedisLockRegistry(this.getConnectionFactoryForTest(),
            this.registryKey);
    final RedisLockRegistry registry2 = new RedisLockRegistry(this.getConnectionFactoryForTest(),
            this.registryKey);
    final Lock lock1 = registry1.obtain("foo");
    final AtomicBoolean locked = new AtomicBoolean();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final CountDownLatch latch3 = new CountDownLatch(1);
    lock1.lockInterruptibly();
    assertNotNull(TestUtils.getPropertyValue(registry1, "hardThreadLocks", ThreadLocal.class).get());
    Executors.newSingleThreadExecutor().execute(() -> {
        Lock lock2 = registry2.obtain("foo");
        try {
            latch1.countDown();
            lock2.lockInterruptibly();
            assertNotNull(TestUtils.getPropertyValue(registry2, "hardThreadLocks", ThreadLocal.class).get());
            latch2.await(10, TimeUnit.SECONDS);
            locked.set(true);
        } catch (InterruptedException e1) {
            Thread.currentThread().interrupt();
            logger.error("Interrupted while locking: " + lock2, e1);
        } finally {
            try {
                lock2.unlock();
                latch3.countDown();
            } catch (IllegalStateException e2) {
                logger.error("Failed to unlock: " + lock2, e2);
            }
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertFalse(locked.get());
    lock1.unlock();
    latch2.countDown();
    assertTrue(latch3.await(10, TimeUnit.SECONDS));
    assertTrue(locked.get());
    assertNull(TestUtils.getPropertyValue(registry1, "hardThreadLocks", ThreadLocal.class).get());
    assertNull(TestUtils.getPropertyValue(registry2, "hardThreadLocks", ThreadLocal.class).get());
}

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

@Test
@RedisAvailable/*ww w .j  a  v a2  s.  com*/
public void testTwoThreadsWrongOneUnlocks() throws Exception {
    final RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(),
            this.registryKey);
    final Lock lock = registry.obtain("foo");
    lock.lockInterruptibly();
    final AtomicBoolean locked = new AtomicBoolean();
    final CountDownLatch latch = new CountDownLatch(1);
    Future<Object> result = Executors.newSingleThreadExecutor().submit(() -> {
        try {
            lock.unlock();
        } catch (IllegalStateException ise) {
            latch.countDown();
            return ise;
        }
        return null;
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertFalse(locked.get());
    lock.unlock();
    Object ise = result.get(10, TimeUnit.SECONDS);
    assertThat(ise, instanceOf(IllegalStateException.class));
    assertThat(((Exception) ise).getMessage(), containsString("Lock is owned by"));
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

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

@Test
@RedisAvailable//  w w  w.  j a v a2 s . c  om
public void testList() throws Exception {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
    Lock foo = registry.obtain("foo");
    foo.lockInterruptibly();
    Lock bar = registry.obtain("bar");
    bar.lockInterruptibly();
    Lock baz = registry.obtain("baz");
    baz.lockInterruptibly();
    Collection<Lock> locks = registry.listLocks();
    assertEquals(3, locks.size());
    foo.unlock();
    bar.unlock();
    baz.unlock();
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

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

@Test
@RedisAvailable//from ww  w.  j  a  v  a  2s  .  co  m
public void testExpireNoLockInStore() throws Exception {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey,
            100);
    Lock foo = registry.obtain("foo");
    foo.lockInterruptibly();
    waitForExpire("foo");
    try {
        foo.unlock();
        fail("Expected exception");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), containsString("Lock was released due to expiration"));
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

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

@Test
@RedisAvailable/*  w  ww.  j av a 2s  .  com*/
public void testExpireDuringSecondObtain() throws Exception {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey,
            100);
    registry.setUseWeakReferences(true);
    Lock foo = registry.obtain("foo");
    foo.lockInterruptibly();
    waitForExpire("foo");
    Lock foo1 = registry.obtain("foo");
    assertNotSame(foo, foo1);

    try {
        foo.unlock();
        fail("IllegalStateException");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), containsString("Lock is not locked"));
    }
}

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

@Test
@RedisAvailable/*from   ww w  .ja  va2 s. co  m*/
public void testExpireNewLockInStore() throws Exception {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey,
            100);
    Lock foo1 = registry.obtain("foo");
    foo1.lockInterruptibly();
    waitForExpire("foo");
    Lock foo2 = registry.obtain("foo");
    assertNotSame(foo1, foo2);
    foo2.lockInterruptibly();
    try {
        foo1.unlock();
        fail("Expected exception");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), containsString("Lock is not locked"));
    }
    foo2.unlock();
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}