Use (Concurrent)Map.computeIfAbsent for lazy nested collection creation

This commit is contained in:
Juergen Hoeller
2018-03-23 17:36:23 +01:00
parent 8d8e218b52
commit f00afe3247
5 changed files with 14 additions and 35 deletions

View File

@@ -26,6 +26,8 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
@@ -86,7 +88,7 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
private volatile boolean active = true;
private final Map<Integer, LinkedList<Session>> cachedSessions = new HashMap<>();
private final ConcurrentMap<Integer, LinkedList<Session>> cachedSessions = new ConcurrentHashMap<>();
/**
@@ -208,14 +210,7 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
return null;
}
LinkedList<Session> sessionList;
synchronized (this.cachedSessions) {
sessionList = this.cachedSessions.get(mode);
if (sessionList == null) {
sessionList = new LinkedList<>();
this.cachedSessions.put(mode, sessionList);
}
}
LinkedList<Session> sessionList = this.cachedSessions.computeIfAbsent(mode, k -> new LinkedList<>());
Session session = null;
synchronized (sessionList) {
if (!sessionList.isEmpty()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -139,11 +139,7 @@ public class JmsResourceHolder extends ResourceHolderSupport {
if (!this.sessions.contains(session)) {
this.sessions.add(session);
if (connection != null) {
List<Session> sessions = this.sessionsPerConnection.get(connection);
if (sessions == null) {
sessions = new LinkedList<>();
this.sessionsPerConnection.put(connection, sessions);
}
List<Session> sessions = this.sessionsPerConnection.computeIfAbsent(connection, k -> new LinkedList<>());
sessions.add(session);
}
}