From c29d2b1be4be10c5069099ea47499398cf40fbc5 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 4 Nov 2013 13:31:31 +0000 Subject: [PATCH] Remove a small race condition --- .../metrics/InMemoryMetricRepository.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/InMemoryMetricRepository.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/InMemoryMetricRepository.java index 8a62abc539..9c3d638e4b 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/InMemoryMetricRepository.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/InMemoryMetricRepository.java @@ -35,22 +35,23 @@ public class InMemoryMetricRepository implements MetricRepository { @Override public void increment(String metricName, int amount, Date timestamp) { - Measurement current = this.metrics.get(metricName); - if (current != null) { - Object lock = this.locks.putIfAbsent(metricName, new Object()); - if (lock == null) { - lock = this.locks.get(metricName); - } - synchronized (lock) { - current = this.metrics.get(metricName); + Object lock = this.locks.putIfAbsent(metricName, new Object()); + if (lock == null) { + lock = this.locks.get(metricName); + } + synchronized (lock) { + Measurement current = this.metrics.get(metricName); + if (current != null) { Metric metric = current.getMetric(); this.metrics.replace(metricName, current, new Measurement(timestamp, metric.increment(amount))); return; } + else { + this.metrics.putIfAbsent(metricName, new Measurement(timestamp, + new Metric(metricName, amount))); + } } - this.metrics.putIfAbsent(metricName, new Measurement(timestamp, new Metric( - metricName, amount))); } @Override