Many assumptions have been made, for example, I assume that you have a disconnect() method in your Client class. Please provide more information if you want a more accurate solution.
One way to do this could be by passing a callback to your client so that it will be removed from the ArrayList when it has been done. Very crude implementation:
public class Callback { private ArrayList<Client> clients; public Callback(ArrayList<Client> clients) { this.clients = clients; } public void remove(Client client) { clients.remove(client); } }
Then pass the Callback when you instantiate the Client :
Callback callback = new Callback(list); list.add(new Client(socket, callback));
Then call the remove() method of the callback. If Client has a disconnect() method, then inside it you can do
public void disconnect() {
Thus, Client can clean up after itself when it needs to :)
async source share