You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gn...@apache.org on 2018/11/08 15:00:15 UTC

[camel] 01/08: Improve DefaultNodeIdFactory concurrency

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch sandbox/camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit e1068829f28e71f29121e194d247515171684bc9
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Wed Nov 7 16:11:15 2018 +0100

    Improve DefaultNodeIdFactory concurrency
---
 .../java/org/apache/camel/impl/DefaultNodeIdFactory.java  | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultNodeIdFactory.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultNodeIdFactory.java
index cfc8d40..19cb6ab 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultNodeIdFactory.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultNodeIdFactory.java
@@ -16,8 +16,8 @@
  */
 package org.apache.camel.impl;
 
-import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.camel.NamedNode;
@@ -28,7 +28,7 @@ import org.apache.camel.spi.NodeIdFactory;
  */
 public class DefaultNodeIdFactory implements NodeIdFactory {
 
-    protected static Map<String, AtomicInteger> nodeCounters = new HashMap<>();
+    protected static Map<String, AtomicInteger> nodeCounters = new ConcurrentHashMap<>();
 
     public String createId(NamedNode definition) {
         String key = definition.getShortName();
@@ -38,13 +38,8 @@ public class DefaultNodeIdFactory implements NodeIdFactory {
     /**
      * Returns the counter for the given node key, lazily creating one if necessary
      */
-    protected static synchronized AtomicInteger getNodeCounter(String key) {
-        AtomicInteger answer = nodeCounters.get(key);
-        if (answer == null) {
-            answer = new AtomicInteger(0);
-            nodeCounters.put(key, answer);
-        }
-        return answer;
+    protected static AtomicInteger getNodeCounter(String key) {
+        return nodeCounters.computeIfAbsent(key, k -> new AtomicInteger(0));
     }
 
 
@@ -52,7 +47,7 @@ public class DefaultNodeIdFactory implements NodeIdFactory {
      * Helper method for test purposes that allows tests to start clean (made protected 
      *  to ensure that it is not called accidentally)
      */
-    protected static synchronized void resetAllCounters() {
+    protected static void resetAllCounters() {
         for (AtomicInteger counter : nodeCounters.values()) {
             counter.set(0);
         }