1
votes

I am writting a Netty Server for a multiplayer game and I am not sure if I need to synchronize in some way a variable that lives in the server but is accessed by the ChannelHandler.

At the server level I am using an ArrayList to store the different matches the server will be serving.

Each match will be referencing 2 channels (I store for the match the ChannelHandlerContetx for each one).

When I create the ChannelHandler that extends from SimpleChannelInboundHandler I pass an instance of the server to the constructor and I store the server as an instance variable in the handler.

When channelActive is fired, the ChannerlHandler will search for a match in the ArrayList (that lives in the server instance) in "Waiting" state. If it finds one it bounds to it and changes the match status. If not a new Match is created and then the channel bounds to it leaving it in Waiting status.

I know that channels are Thread Safe. But here the different channels are accessing the same server's ArrayList instance.

In this case should I take care of synchronizing the access to the ArrayList?

Note in case it adds to my question: As I am going to have a database on the backend, I am passing a DefaultEventExecutor in the .addLast() method when the handler is being created.

1

1 Answers

0
votes

If you have more than one eventLoop you are going to have concurrent access to the static ArrayList across multiple eventLoops. So YES you should take care of synchronizing the access to the ArrayList.