diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrSessionTracker.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrSessionTracker.java index 5f291d19693..6af4830c59b 100644 --- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrSessionTracker.java +++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrSessionTracker.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.jsr356; import java.util.Collections; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import javax.websocket.Session; import org.eclipse.jetty.util.component.AbstractLifeCycle; @@ -28,38 +29,29 @@ import org.eclipse.jetty.util.component.LifeCycle; public class JsrSessionTracker extends AbstractLifeCycle implements JsrSessionListener { - private final Set sessions = new HashSet<>(); + private final Set sessions = Collections.newSetFromMap(new ConcurrentHashMap<>()); public Set getSessions() { - synchronized (this) - { - return Collections.unmodifiableSet(new HashSet<>(sessions)); - } + return Collections.unmodifiableSet(new HashSet<>(sessions)); } @Override public void onSessionOpened(JsrSession session) { - synchronized (this) - { - sessions.add(session); - } + sessions.add(session); } @Override public void onSessionClosed(JsrSession session) { - synchronized (this) - { - sessions.remove(session); - } + sessions.remove(session); } @Override protected void doStop() throws Exception { - for (Session session : getSessions()) + for (Session session : sessions) { LifeCycle.stop(session); } diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/SessionTracker.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/SessionTracker.java index d0342bbf708..635f1a9d2ce 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/SessionTracker.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/SessionTracker.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.util.Collections; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import org.eclipse.jetty.util.component.AbstractLifeCycle; import org.eclipse.jetty.util.component.Dumpable; @@ -29,40 +30,31 @@ import org.eclipse.jetty.util.component.LifeCycle; public class SessionTracker extends AbstractLifeCycle implements WebSocketSessionListener, Dumpable { - private final Set sessions = new HashSet<>(); + private final Set sessions = Collections.newSetFromMap(new ConcurrentHashMap<>()); public Set getSessions() { - synchronized (this) - { - return Collections.unmodifiableSet(new HashSet<>(sessions)); - } + return Collections.unmodifiableSet(new HashSet<>(sessions)); } @Override public void onSessionCreated(WebSocketSession session) { LifeCycle.start(session); - synchronized (this) - { - sessions.add(session); - } + sessions.add(session); } @Override public void onSessionClosed(WebSocketSession session) { - synchronized (this) - { - sessions.remove(session); - } + sessions.remove(session); LifeCycle.stop(session); } @Override protected void doStop() throws Exception { - for (WebSocketSession session : getSessions()) + for (WebSocketSession session : sessions) { LifeCycle.stop(session); } @@ -72,6 +64,6 @@ public class SessionTracker extends AbstractLifeCycle implements WebSocketSessio @Override public void dump(Appendable out, String indent) throws IOException { - Dumpable.dumpObjects(out, indent, this, getSessions()); + Dumpable.dumpObjects(out, indent, this, sessions); } }