JAVA客户端中通过服务器的并发修改异常

时间:2018-08-18 19:44:38

标签: java client-server java.util.concurrent

我正在尝试通过服务器实现客户端到客户端的聊天。
但这给了我并发执行异常。
任何建议将不胜感激。

我设法编写的源代码:

  1. 下面是我的服务器程序:
public class Server {

    int port;
    ServerSocket server=null;
    Socket socket=null;
    ExecutorService exec = null;
    ArrayList clients = new ArrayList();
    DataOutputStream dos=null;

    public static void main(String[] args) throws IOException {
        Server serverobj=new Server(2000);
        serverobj.startServer();
    }

    Server(int port){
        this.port=port;
        exec = Executors.newFixedThreadPool(2);
    }

    public void startServer() throws IOException {

        server=new ServerSocket(2000);
        System.out.println("Server running");

        while(true){
            socket=server.accept();
            dos = new DataOutputStream(socket.getOutputStream());
            clients.add(dos);
            ServerThread runnable= new ServerThread(socket,clients,this);
            exec.execute(runnable);
        }
    }

    private static class ServerThread implements Runnable {

        Server server=null;
        Socket socket=null;
        BufferedReader brin;
        Iterator it=null;
        Scanner sc=new Scanner(System.in);
        String str;

        ServerThread(Socket socket, ArrayList clients ,Server server ) throws IOException {
            this.socket=socket;
            this.server=server;
            System.out.println("Connection successful with "+socket);

            brin=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            it = clients.iterator();
        }

        @Override
        public void run() {      
            try
            {
                while ((str = brin.readLine()) != null) 
                {
                    {
                        try
                        {
                            DataOutputStream oss=it.next();
                            oss.writeChars(str);          
                        }
                        catch(Exception ex){
                            System.out.println("Error 1 "+ex);
                        }
                    }
                }
                brin.close();
                socket.close();             
            } 
            catch(IOException ex){
                System.out.println("Error 2 "+ex);
            }
        }
    }

}
  1. 客户端代码如下:
public class Client1 {
    public static void main(String args[]) throws IOException{

        String str;
        Socket socket=new Socket("127.0.0.1",2000);

        BufferedReader brin=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintStream prout=new PrintStream(socket.getOutputStream());
        BufferedReader bread=new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Send to Server:");
            str=bread.readLine();
            prout.println(str);

        while(true){
            str=brin.readLine();
            System.out.print("Server:"+str+"\n");
        }
    }  
}

请帮助我修复代码...
我试图修复它几个小时,但无济于事。

1 个答案:

答案 0 :(得分:1)

您正在一个线程(while (it.hasNext()))上迭代客户端列表,而另一个正在向其中添加项目(clients.add(dos);)。 例如,可以通过传递列表的副本来教授新线程来避免该异常: ServerThread runnable= new ServerThread(socket,new Arraylist<>(clients),this);