|
locks.putIfAbsent(key, new Object()); |
|
synchronized (locks.get(key)) { |
|
clients = referenceClientMap.get(key); |
|
// dubbo check |
|
if (checkClientCanUse(clients)) { |
|
batchClientRefIncr(clients); |
|
return clients; |
|
} |
|
|
|
// connectNum must be greater than or equal to 1 |
|
connectNum = Math.max(connectNum, 1); |
|
|
|
// If the clients is empty, then the first initialization is |
|
if (CollectionUtils.isEmpty(clients)) { |
|
clients = buildReferenceCountExchangeClientList(url, connectNum); |
|
referenceClientMap.put(key, clients); |
|
|
|
} else { |
|
for (int i = 0; i < clients.size(); i++) { |
|
ReferenceCountExchangeClient referenceCountExchangeClient = clients.get(i); |
|
// If there is a client in the list that is no longer available, create a new one to replace him. |
|
if (referenceCountExchangeClient == null || referenceCountExchangeClient.isClosed()) { |
|
clients.set(i, buildReferenceCountExchangeClient(url)); |
|
continue; |
|
} |
|
|
|
referenceCountExchangeClient.incrementAndGetCount(); |
|
} |
|
} |
|
|
|
/* |
|
* I understand that the purpose of the remove operation here is to avoid the expired url key |
|
* always occupying this memory space. |
|
*/ |
|
locks.remove(key); |
|
|
|
return clients; |
Why not use the key to lock
synchronized (key.intern()) {
// ...... content
}
dubbo/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java
Lines 458 to 494 in 317e62f
Why not use the key to lock