@Override public void run() { lock2.lock(); try { lockAcquired.countDown(); } finally { lock2.unlock(); } } };
@Override public void run() { lock.lock(); try { acquired.countDown(); } finally { lock.unlock(); } } };
@Override public void run() { try { lock2.lockInterruptibly(); try { lockAcquired.countDown(); } finally { lock2.unlock(); } } catch (InterruptedException e) { lockInterrupted.countDown(); } } };
@Override public void run() { try { lock.lockInterruptibly(); try { lockAcquired.countDown(); } finally { lock.unlock(); } } catch (InterruptedException e) { lockInterrupted.countDown(); } } };
@Override public void run() { try { if (!lock.tryLock()) { lockFailed.countDown(); } if (!lock.tryLock(1, TimeUnit.SECONDS)) { lockFailed.countDown(); } if (lock.tryLock(5, TimeUnit.SECONDS)) { // Test the reentrant park as well if (lock.tryLock(1, TimeUnit.SECONDS)) { if (lock.tryLock()) { lockAcquired.countDown(); lock.unlock(); } lock.unlock(); } lock.unlock(); } } catch (InterruptedException e) { // Shouldn't happen LOG.error(e.getMessage(), e); } } };
@Override public void run() { try { if (!lock2.tryLock()) { lockFailed.countDown(); } if (!lock2.tryLock(1, TimeUnit.SECONDS)) { lockFailed.countDown(); } if (lock2.tryLock(5000, TimeUnit.SECONDS)) { // Test the reentrant park as well if (lock2.tryLock(1, TimeUnit.SECONDS)) { if (lock2.tryLock()) { lockAcquired.countDown(); lock2.unlock(); } lock2.unlock(); } lock2.unlock(); } } catch (InterruptedException e) { // Shouldn't happen LOG.error(e.getMessage(), e); } } };
@Test (timeout = 20000) public void testReentrant() { // Test the lock is reentrant from the same thread ZKClientService zkClient = createZKClient(); try { ReentrantDistributedLock lock = new ReentrantDistributedLock(zkClient, "reentrant"); lock.lock(); try { try { lock.lock(); } finally { lock.unlock(); } } finally { lock.unlock(); } } finally { zkClient.stopAndWait(); } }
Assert.assertTrue(lockInterrupted.await(2, TimeUnit.SECONDS)); } finally { lock.unlock();
lock.unlock();