diff --git a/clients/java/signalr/src/main/java/com/microsoft/signalr/CallbackMap.java b/clients/java/signalr/src/main/java/com/microsoft/signalr/CallbackMap.java index 134ab2ed097..a6298aed52c 100644 --- a/clients/java/signalr/src/main/java/com/microsoft/signalr/CallbackMap.java +++ b/clients/java/signalr/src/main/java/com/microsoft/signalr/CallbackMap.java @@ -3,29 +3,45 @@ package com.microsoft.signalr; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.locks.ReentrantLock; class CallbackMap { - private final Map> handlers = new ConcurrentHashMap<>(); + private final Map> handlers = new HashMap<>(); + private final ReentrantLock lock = new ReentrantLock(); public InvocationHandler put(String target, ActionBase action, Class... classes) { - InvocationHandler handler = new InvocationHandler(action, classes); - handlers.compute(target, (key, value) -> { - if (value == null) { - value = new ArrayList<>(); + try { + lock.lock(); + InvocationHandler handler = new InvocationHandler(action, classes); + if (!handlers.containsKey(target)) { + handlers.put(target, new ArrayList<>()); } - value.add(handler); - return value; - }); - return handler; + handlers.get(target).add(handler); + return handler; + } finally { + lock.unlock(); + } } public List get(String key) { - return handlers.get(key); + try { + lock.lock(); + return handlers.get(key); + } finally { + lock.unlock(); + } } public void remove(String key) { - handlers.remove(key); + try { + lock.lock(); + handlers.remove(key); + } finally { + lock.unlock(); + } } }