From dfb1315ff5f188ca1d896f1b084099487ceb178f Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sun, 16 Feb 2020 10:43:35 +0100 Subject: [PATCH] [core] Use concurrent hash map in ParameterizedMetricKey --- .../pmd/lang/metrics/ParameterizedMetricKey.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/ParameterizedMetricKey.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/ParameterizedMetricKey.java index 474852c8ec..fd863681d9 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/ParameterizedMetricKey.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/ParameterizedMetricKey.java @@ -4,8 +4,8 @@ package net.sourceforge.pmd.lang.metrics; -import java.util.HashMap; -import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import net.sourceforge.pmd.annotation.InternalApi; import net.sourceforge.pmd.lang.ast.Node; @@ -24,7 +24,7 @@ import net.sourceforge.pmd.util.DataMap.DataKey; @Deprecated public final class ParameterizedMetricKey implements DataKey, Double> { - private static final Map, ParameterizedMetricKey> POOL = new HashMap<>(); + private static final ConcurrentMap, ParameterizedMetricKey> POOL = new ConcurrentHashMap<>(); /** The metric key. */ public final MetricKey key; @@ -72,9 +72,7 @@ public final class ParameterizedMetricKey implements DataKey ParameterizedMetricKey getInstance(MetricKey key, MetricOptions options) { // sharing instances allows using DataMap, which uses reference identity ParameterizedMetricKey tmp = new ParameterizedMetricKey<>(key, options); - if (!POOL.containsKey(tmp)) { - POOL.put(tmp, tmp); - } + POOL.putIfAbsent(tmp, tmp); @SuppressWarnings("unchecked") ParameterizedMetricKey result = (ParameterizedMetricKey) POOL.get(tmp);