private void samplingLoop() {
    try {
      for (int sampleNo = 0;
           !Thread.currentThread().isInterrupted();
           sampleNo++) {
        long timeTakenNs = runCpuAndMemoryTask();

        taskDurationHisto.recordValue(MathUtil.clamp(timeTakenNs, 0, Long.MAX_VALUE));

        try {
          //noinspection BusyWait
          Thread.sleep(SAMPLING_PERIOD_MS);
        }
        catch (InterruptedException e) {
          LOG.info("Sampling thread interrupted -> stop sampling");
          return;
        }

        if (sampleNo % REPORTING_EACH_N_SAMPLES == REPORTING_EACH_N_SAMPLES - 1) {
          reportAccumulatedStats(taskDurationHisto);
          taskDurationHisto.reset();
        }
      }
    }
    catch (Throwable e) {
      LOG.error("Sampling thread exiting because of error", e);
    }
    finally {
      LOG.info("Sampling thread exiting normally");
    }
  }
private void samplingLoop()