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();
      }
    }
  }
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.
jimfs/src/main/java/com/google/common/jimfs/FileSystemView.java