我有一个类,我liveSocketsByDatacenter在updateLiveSockets()方法中每30秒从一个后台线程填充一个映射,然后我有一个方法getNextSocket(),将由多个读取器线程调用以获得一个可用的实时套接字,它使用相同的映射来获取此信息.
public class SocketManager {
private static final Random random = new Random();
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private final AtomicReference<Map<Datacenters, List<SocketHolder>>> liveSocketsByDatacenter =
new AtomicReference<>(Collections.unmodifiableMap(new HashMap<>()));
private final ZContext ctx = new ZContext();
// Lazy Loaded Singleton Pattern
private static class Holder {
private static final SocketManager instance = new SocketManager();
}
public static SocketManager getInstance() {
return Holder.instance;
}
private SocketManager() {
connectToZMQSockets();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
updateLiveSockets(); …Run Code Online (Sandbox Code Playgroud)