@Test
    public void testCheckQuotasInMultiThreads() throws InterruptedException, ExecutionException {
        final Metrics metrics = new Metrics(new MetricConfig().quota(Quota.upperBound(Double.MAX_VALUE))
            // decreasing the value of time window make SampledStat always record the given value
            .timeWindow(1, TimeUnit.MILLISECONDS)
            // increasing the value of samples make SampledStat store more samples
            .samples(100));
        final Sensor sensor = metrics.sensor("sensor");

        assertTrue(sensor.add(metrics.metricName("test-metric", "test-group"), new Rate()));
        final int threadCount = 10;
        final CountDownLatch latch = new CountDownLatch(1);
        ExecutorService service = Executors.newFixedThreadPool(threadCount);
        List<Future<Throwable>> workers = new ArrayList<>(threadCount);
        boolean needShutdown = true;
        try {
            for (int i = 0; i != threadCount; ++i) {
                final int index = i;
                workers.add(service.submit(() -> {
                    try {
                        assertTrue(latch.await(5, TimeUnit.SECONDS));
                        for (int j = 0; j != 20; ++j) {
                            sensor.record(j * index, System.currentTimeMillis() + j, false);
                            sensor.checkQuotas();
                        }
                        return null;
                    } catch (Throwable e) {
                        return e;
                    }
                }));
            }
            latch.countDown();
            service.shutdown();
            assertTrue(service.awaitTermination(10, TimeUnit.SECONDS));
            needShutdown = false;
            for (Future<Throwable> callable : workers) {
                assertTrue(callable.isDone(), "If this failure happen frequently, we can try to increase the wait time");
                assertNull(callable.get(), "Sensor#checkQuotas SHOULD be thread-safe!");
            }
        } finally {
            if (needShutdown) {
                service.shutdownNow();
            }
        }
    }
The Sensor#checkQuotas should be thread-safe since the method may be used by many ReplicaFetcherThreads.
clients/src/test/java/org/apache/kafka/common/metrics/SensorTest.java