I am writing a simple game from top to bottom, and producing it to allow playing on the network with several players. I participated in a little reading, but this is the first time I have done this, and I would appreciate advice on choosing a reasonable design.
My GUI is written using Swing. The timer is triggered 30 times per second, and it changes my graphical interface in accordance with the data in the gameWorld object in memory (in essence, a list of ships and shells with positions, etc.). GameWorld physics updates are also performed using this timer. Thus, to implement one player, everything happens on the EDT, and it works great.
Now I have a separate thread related to incoming packages from other players. I would like to update the data in the gameWorld object based on what these packages contain. My question is: should I use invokeLater to make these changes or use locks to avoid concurrency issues?
To illustrate what I mean:
runMethodOfInputThread() { while(takingInput) { data = receiveAndInterpretIncomingPacket(); // blocks SwingUtilities.invokeLater(new Runnable() { public void run() { gameWorld.updateWithNewGameInfo(data); } }); } }
against
runMethodOfInputThread() { while(takingInput) { data = receiveAndInterpretIncomingPacket();
The latter will also require the use of similar synchronization blocks wherever the EDT accesses gameWorld, so it seems to me that using invokeLater would be easier to implement. But do I think that both approaches will work? Are there any other important pros and cons to keep in mind?
Thanks,
Jeremiah
source share