2 player, turn based game communications

Hi,

I have a 2 player, turn based game which uses DataInputStream and DataOutputStream to send and receive String and int messages to and from the server. I was looking for a way to receive the messages and paint the resulting changes while respecting the rule that all Swing jobs should only be done on the EDT, while receiving data should never be done on the EDT. I have found something that seems to work, but my question is really if it is the most efficient way and if there are other “standard” ways of doing this. This is what I am doing at the moment:


public void processMessage(String message){// Process messages sent by server
    if(message.equals("do something")){
        a = myReadInt();
        b = myReadUTF();
    }
 
Thank you in advance.
    else if(message.equals("do something else")){
        a = myReadInt();
        b = myReadUTF();
    }
 
    String m = message;
    try{
        javax.swing.SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                processMessageEDT(m, a, b);
            }
        });
    }catch(Exception e){System.err.println("error");}
 
}

I favor using a producer-consumer design pattern for problems such as these.

The networking thread reads incoming packets/data/whathaveyou, deserializes them appropriately, and stores them in a queue. This is the ‘producer’. Each game loop, the game logic empties this queue and performs the appropriate game logic. This is the ‘consumer’. The two threads never interact directly.