I have two classes in short here they are:
public final class ServerMain
{
static List<Table> s_AvailableGameTables = new Vector<Table>();
static List<Table> s_OccupiedGameTables = new Vector<Table>();
static List<ServerThread> s_PlayersOnServer = new Vector<ServerThread>();
...
}
class ServerThread extends Thread{
...}
ServerMain is the server itself, and it manages the ServerThreads by allocating a new ServerThread for each user who has just connected to the ServerMain.
My questions are simple:
-
When I’m currently running in the specific ServerThread and I want to access some static lists on the serverMain and to update them how can I do that , if I’ve already “left” the area of the ServerMain while being in the specific thread which runs in the background.
Is the only way is to hold a reference from each serverthread to papa ServerMain? -
Maybe it can cause some problems as if at the same time two areas of the code can update the same list the ServerMain itself and the ServerThread which now knows who is the big boss around?
-
General question: does sockets programming means UDP or TCP?
I’d like to hear some good advice. Thanks in advance.
For #1, you wouldn’t need an instance to access static members of ServerMain, assuming they are accessible (e.g. they are
public) you can access them asServerMain.MyVar.For #2, yes, you would need to look into using the
synchronizedstatement to prevent multiple threads for writing to the list at the same time, or use a thread safe list implementation.For #3, the term ‘sockets programming’ can refer to either UDP or TCP. Which kind of socket you use will depend on what kind of app you are implementing.