From 93bf68039bd0c114054aaf95a5d274eb4cd6b788 Mon Sep 17 00:00:00 2001 From: shikhman Date: Tue, 6 Sep 2016 03:49:40 -0700 Subject: [PATCH] Add lock.unlock() to finally clause in Counter It is good practice to unlock a lock in a finally clause. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=132302413 --- .../registry/monitoring/metrics/Counter.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/java/google/registry/monitoring/metrics/Counter.java b/java/google/registry/monitoring/metrics/Counter.java index 26ca9a578..6f42e2378 100644 --- a/java/google/registry/monitoring/metrics/Counter.java +++ b/java/google/registry/monitoring/metrics/Counter.java @@ -23,7 +23,6 @@ import com.google.common.util.concurrent.AtomicLongMap; import com.google.common.util.concurrent.Striped; import google.registry.monitoring.metrics.MetricSchema.Kind; import java.util.Map.Entry; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.Lock; import javax.annotation.concurrent.ThreadSafe; @@ -171,18 +170,19 @@ public final class Counter extends AbstractMetric final void reset(Instant startTimestamp) { // Lock the entire set of values so that all existing values will have a consistent timestamp // after this call, without the possibility of interleaving with another reset() call. - Set> keys = values.asMap().keySet(); for (int i = 0; i < valueLocks.size(); i++) { valueLocks.getAt(i).lock(); } - for (ImmutableList labelValues : keys) { - this.values.put(labelValues, 0); - this.valueStartTimestamps.put(labelValues, startTimestamp); - } - - for (int i = 0; i < valueLocks.size(); i++) { - valueLocks.getAt(i).unlock(); + try { + for (ImmutableList labelValues : values.asMap().keySet()) { + this.values.put(labelValues, 0); + this.valueStartTimestamps.put(labelValues, startTimestamp); + } + } finally { + for (int i = 0; i < valueLocks.size(); i++) { + valueLocks.getAt(i).unlock(); + } } }