Say an object, an ArrayList<String>
is shared by multiple threads running inside an outer class (the runnable classes are inner classes of the larger class - a server). These threads represent seperate clients which respectively have a thread dedicated to handle their outputs and inputs to the server. Now, as a thread is created, within the thread, adds/removes a value in this ArrayList<String>
which stores their username when they join/exit the client program.
However, when I send this ArrayList<String>
across to each of the client sockets, each client gets the arraylist they got before (when they first joined) rather than a new updated list (when new people join). E.g. if John joins first then his list will always comprise of only John, and if Cath joins next then she only gets a list of John, Cath and no more.
My server side program has this method which sends the list.
public void sendOnlineList() {
try{
for(ObjectOutputStream os: clientStreams) { //ObjectOutputStream for each client socket
System.out.println("sending: " + printAll(currentOnline));
os.writeObject(currentOnline);
}
}catch(IOException e) {e.printStackTrace();}
}
The client side program has a thread to constantly receive messages when needed.
public class ServReader implements Runnable {
@SuppressWarnings("unchecked")
public void run() {
try {
Object line = null;
while((line=is.readObject())!=null) {
synchronized(this) {
if(line instanceof String) { //normal chat message
line = (String) line;
System.out.println(line);
text.append(line + "\n"); //irrelevant stuff using the string
} else if(line instanceof ArrayList<?>) { //list of users
System.out.println("found list");
printAll((ArrayList<String>) line);
setOnline((ArrayList<String>) line); //irrelevant stuff using the list
}
}
}
/*String line = null;
while((line=is.readLine())!=null) {
text.append(line+ "\n");
System.out.println(line);
}*/
} catch(Exception e) {
e.printStackTrace();
}
}
}
the server side program outputs to the console
sending: John, Cath
sending: John, Cath
however the surprising part is that the client side programs outputs to the console
John:
found list
John,
Cath:
found list
John, Cath,
I want to send the same list across to all clients but somehow, the same list is sent to both clients from the server, but each client receives a different list; surely the object isn't changing as it gets sent... what in this code is wrong?