@Test(timeout = 1000) public void willUseDifferentLocksForDifferentPartsOfTheSameClass() { final AtomicBoolean lockAcquired = new AtomicBoolean(false); final AtomicInteger callCount = new AtomicInteger(0); thread1 = new Thread(() -> { try (final AutoCloseableReentrantLock.AutoCloseableLock ignored = new AutoCloseableReentrantLock(AutoCloseableReentrantLockTest.class, "user1").lock()) { lockAcquired.set(true); callCount.incrementAndGet(); Thread.sleep(1000); } catch (final InterruptedException e) { LOGGER.info("Interrupted whilst sleeping", e); } }); thread1.start(); thread2 = new Thread(() -> { try (final AutoCloseableReentrantLock.AutoCloseableLock ignored = new AutoCloseableReentrantLock(AutoCloseableReentrantLockTest.class, "user2").lock()) { lockAcquired.set(true); callCount.incrementAndGet(); Thread.sleep(1000); } catch (final InterruptedException e) { LOGGER.info("Interrupted whilst sleeping", e); } }); thread2.start(); await().untilTrue(lockAcquired); assertThat(callCount.get(), is(2)); }
@SuppressWarnings( "LockAcquiredButNotSafelyReleased" ) public AutoCloseableLock lock() throws IllegalStateException { checkNotReleased(); lock.lock(); return autoCloseable; }
AutoCloseableReentrantLock { @SuppressWarnings( "LockAcquiredButNotSafelyReleased" ) public AutoCloseableLock lock() throws IllegalStateException { checkNotReleased(); lock.lock(); return autoCloseable; } }
AutoCloseableReentrantLock { @SuppressWarnings( "LockAcquiredButNotSafelyReleased" ) public AutoCloseableLock lock() throws IllegalStateException { checkNotReleased(); lock.lock(); return autoCloseable; } AutoCloseableReentrantLock(final Class clazz, final String resource); }
AutoCloseableReentrantLock { @SuppressWarnings( "LockAcquiredButNotSafelyReleased" ) public AutoCloseableLock lock() throws IllegalStateException { checkNotReleased(); lock.lock(); return autoCloseable; } AutoCloseableReentrantLock(final Class clazz, final String resource); @SuppressWarnings( "LockAcquiredButNotSafelyReleased" ) AutoCloseableLock lock(); Optional<AutoCloseableLock> tryLock(); @SuppressWarnings( "LockAcquiredButNotSafelyReleased" ) AutoCloseableLock lockInterruptibly(); boolean isHeldByCurrentThread(); boolean isLocked(); }
AutoCloseableReentrantLock { @SuppressWarnings( "LockAcquiredButNotSafelyReleased" ) public AutoCloseableLock lock() throws IllegalStateException { checkNotReleased(); lock.lock(); return autoCloseable; } AutoCloseableReentrantLock(final Class clazz, final String resource); @SuppressWarnings( "LockAcquiredButNotSafelyReleased" ) AutoCloseableLock lock(); Optional<AutoCloseableLock> tryLock(); @SuppressWarnings( "LockAcquiredButNotSafelyReleased" ) AutoCloseableLock lockInterruptibly(); boolean isHeldByCurrentThread(); boolean isLocked();  }