public static <T extends Throwable>void lockAndThen(ReentrantLock lock,ExceptionalRunnable<T> code) throws T {
  if (lock.isHeldByCurrentThread()) {
    code.run();
  }
 else {
    lock.lock();
    try {
      code.run();
    }
  finally {
      lock.unlock();
    }
  }
}

public static < T extends Throwable > void lockAndThen ( ReentrantLock lock , ExceptionalRunnable < T > code ) throws T { if ( lock . isHeldByCurrentThread ( ) ) { code . run ( ) ; } else { lock . lock ( ) ; try { code . run ( ) ; } finally { lock . unlock ( ) ; } } }
acquires lock if the current thread does not hold it already , executes code and returns . the passed lambda may throw an exception , which is then thrown by this method .
