diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java index 4e8b971328d..017e4a93df3 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java @@ -65,6 +65,7 @@ public class DubboProtocol extends AbstractProtocol { private final Map serverMap = new ConcurrentHashMap(); // private final Map referenceClientMap = new ConcurrentHashMap(); // private final ConcurrentMap ghostClientMap = new ConcurrentHashMap(); + private final ConcurrentMap locks = new ConcurrentHashMap(); private final Set optimizers = new ConcurrentHashSet(); //consumer side export a stub service for dispatching event //servicekey-stubmethods @@ -373,11 +374,18 @@ private ExchangeClient getSharedClient(URL url) { referenceClientMap.remove(key); } } - synchronized (key.intern()) { + + locks.putIfAbsent(key, new Object()); + synchronized (locks.get(key)) { + if (referenceClientMap.containsKey(key)) { + return referenceClientMap.get(key); + } + ExchangeClient exchangeClient = initClient(url); client = new ReferenceCountExchangeClient(exchangeClient, ghostClientMap); referenceClientMap.put(key, client); ghostClientMap.remove(key); + locks.remove(key); return client; } }