code: private static void lockBoth(Lock sourceWriteLock, Lock destWriteLock) {
      while (true) {
        sourceWriteLock.lock();
        if (destWriteLock.tryLock()) {
          return;
        } else {
          sourceWriteLock.unlock();
        }
  
        destWriteLock.lock();
        if (sourceWriteLock.tryLock()) {
          return;
        } else {
          destWriteLock.unlock();
        }
      }
    }
docs: Acquires both write locks in a way that attempts to avoid the possibility of deadlock. Note
  that typically (when only one file system instance is involved), both locks will be the same
  lock and there will be no issue at all.