import java.io.*; import java.net.*; import java.util.*; public class Work implements Runnable{ List users=new ArrayList<>(); boolean running=true; public void writeToAll(String text){ for(User userToWrite: users){ userToWrite.pw.println(text); } } public void add(User user){ synchronized(users){ users.add(user); writeToAll(user.toString()); } } public void run(){ while(running){ try{ Thread.sleep(500); synchronized(users){ for(User user: users){ if(user.br.ready()){ while(user.br.ready()){ char c=(char)user.br.read(); if(c=='\n'){ user.execute(user.linepart); writeToAll(user.toString()); user.linepart=""; } else { user.linepart+=c; } } } } } } catch(Exception e){e.printStackTrace();} } } }