Code example for ReentrantLock

Methods: isHeldByCurrentThread, isLocked, lock

0
      assertEquals("key value not found: (" + key + ", " + value + ")", true, exists);
   } 
 
   public static class DelayTransportInterceptor extends CommandInterceptor { 
 
      private final ReentrantLock lock = new ReentrantLock();
 
      public DelayTransportInterceptor(boolean lock) {
         if (lock)
            block(); 
      } 
 
      @Override 
      protected Object handleDefault(InvocationContext ctx, VisitableCommand command) throws Throwable {
         log.trace("Acquiring lock. " + lockInfo());
         lock.lock();
         try { 
            return super.handleDefault(ctx, command);
         } finally { 
            log.trace("Done operation, releasing lock" + lockInfo());
            lock.unlock();
         } 
      } 
 
      private String lockInfo() {
         return " Is locked? " + lock.isLocked() + ". Lock held by me? " + lock.isHeldByCurrentThread();
      } 
 
      public void block() { 
         log.trace("block. " + lockInfo());
         lock.lock();
      } 
 
      public void allow() { 
         log.trace("allow." + lockInfo());
         lock.unlock();