code: private static long getCurrentPID() {
      try {
        String n = ManagementFactory.getRuntimeMXBean().getName();
        int i = n.indexOf('@');
        if(i != -1) {
          return Long.parseLong(n.substring(0, i));
        } else {
          return -1L;
        }
      } catch(Throwable ignore) {
        return -1L;
      }
    }
docs: Find PID of the current process, use -1 if we can't find the value.