code: public boolean lock() throws Exception {
          synchronized (this) {
              if (fileLock != null) {
                  throw new Exception("The lock is already held");
              }
              if (log.isDebugEnabled()) {
                  log.debug("Acquiring lock on " + lockFile.getAbsolutePath());
              }
              try {
                  fileChannel = new RandomAccessFile(lockFile, "rw").getChannel();
                  fileLock = fileChannel.tryLock();
              } catch (OverlappingFileLockException | IOException e) {
                  throw new Exception("Exception occurred while trying to get a lock on file: " +
                          lockFile.getAbsolutePath(), e);
              }
              if (fileLock == null) {
                  if (fileChannel != null) {
                      try {
                          fileChannel.close();
                      } catch (IOException ie) {
                          // ignore
                      }
                      fileChannel = null;
                  }
                  return false;
              } else {
                  return true;
              }
          }
      }
docs: Try to lock the file and return true if the locking succeeds.
  
  @return true if the locking succeeds; false if the lock is already held
  @throws Exception if the lock could not be obtained for any reason