Sweet trick to avoid locking in global tab list, basically reimplement HashSet based on a ConcurrentHashMap.

This commit is contained in:
md_5 2013-01-31 17:55:46 +11:00
parent e5bb5e5168
commit 0721e3cc75

View File

@ -1,8 +1,7 @@
package net.md_5.bungee.tablist;
import java.util.HashSet;
import java.util.Set;
import lombok.Synchronized;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.ProxyServer;
@ -13,7 +12,9 @@ import net.md_5.bungee.packet.PacketC9PlayerListItem;
public class GlobalTabList implements TabListHandler
{
private final Set<ProxiedPlayer> sentPings = new HashSet<>();
// Sweet trick to avoid locking, basically reimplement HashSet based on a ConcurrentHashMap
private static final Object PRESENT = new Object();
private final Map<ProxiedPlayer, Object> sentPings = new ConcurrentHashMap<>();
@Override
public void onConnect(ProxiedPlayer player)
@ -27,18 +28,16 @@ public class GlobalTabList implements TabListHandler
}
@Override
@Synchronized(value = "sentPings")
public void onPingChange(ProxiedPlayer player, int ping)
{
if (!sentPings.contains(player))
if (!sentPings.containsKey(player))
{
BungeeCord.getInstance().broadcast(new PacketC9PlayerListItem(player.getDisplayName(), true, player.getPing()));
sentPings.add(player);
sentPings.put(player, PRESENT);
}
}
@Override
@Synchronized(value = "sentPings")
public void onDisconnect(ProxiedPlayer player)
{
BungeeCord.getInstance().broadcast(new PacketC9PlayerListItem(player.getDisplayName(), false, 9999));