code: private static boolean isCreatingPlatformMBeanServer(int threshold) {
          int count = 0;
          for (final StackTraceElement ste : Thread.currentThread().getStackTrace()) {
              if ("java.lang.management.ManagementFactory".equals(ste.getClassName())
                  && "getPlatformMBeanServer".equals(ste.getMethodName())) {
                  count++;
                  if (count >= threshold) {
                      return true;
                  }
              }
          }
          return false;
      }
docs: Returns true if at least 'threshold' of the ancestor calls on the stack are
  java.lang.management.ManagementFactory.getPlatformMBeanServer().