title: Netty ChannelGroup over ConcurrentHashMap
body: <p>I'm working on a multiplayer game server based on netty.
  Most of the messages transmitted to the clients are specific to a single client
  but sometimes i need to broadcast the same messages to all clients.</p>
  
  <p>I'm not sure if there's a good reason to user a ChannelGroup over my own Map.
  So right now i have:</p>
  
  <pre><code>public class GameSession {
    /* a map of all the players part of this game session */
    private ConcurrentHashMap&lt;String, PlayerHandler&gt; players = new ConcurrentHashMap&lt;String, PlayerHandler&gt;();
    private final ChannelGroup playersChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
  </code></pre>
  
  <p>Where PlayerHandler is:</p>
  
  <pre><code>public class PlayerHandler extends SimpleChannelInboundHandler&lt;IncomingMessage&gt;
  </code></pre>
  
  <p>And contains stateful member variables.</p>
  
  <p>When a new player joins (after going through some logic) he's being added to the GameSession:</p>
  
  <pre><code>public void addPlayer(PlayerHandler p) {
  
      if (p.getGameSessionID().equals(this.gameId)) {
          players.put(p.getPlayerID(), p); //add this player to our game session
          playersChannels.add(p.getChannelFromCtx()); //get channel and add to ChannelGroup
      }
  }
  </code></pre>
  
  <p>Let's say i want to broadcast a message since a player requested to leave (or closed connection)</p>
  
  <pre><code>public void notifyPlayerLeft(String exPlayer) {
      //Broadcast message with the id of the player that left
      for (Map.Entry&lt;String, PlayerHandler&gt; entry : players.entrySet()) {
          PlayerHandler player = entry.getValue();
          player.sendPlayerLeft(exPlayer);
  
      }
  }
  </code></pre>
  
  <p>Where sendPlayerLeft() is a simple method that does something like:</p>
  
  <pre><code>ctx.writeAndFlush(outgoingMsg)
  </code></pre>
  
  <p>If I use a <strong>ChannelGroup</strong> I can do something like this:</p>
  
  <pre><code>playersChannels.writeAndFlush(outgoingMsg, matcher)
  </code></pre>
  
  <p>but I'm not sure why that's a better idea. Netty states that it happens asynchronously
  but since PlayerHandler does not have its own thread, wouldn't iterating over the objects 
  like I did in NotifyPlayerLeft() will also be async? Please note that this entire scenario will be triggered by one Channel/User/Thread.   </p>
  
  <p>I hope that my questions are clear enough.
  Thanks!</p>