JAVA 客户端到客户端通过服务器的并发修改异常
Concurrent modification exception in JAVA client to client through server
我正在尝试通过服务器实现客户端到客户端的聊天。
但它给了我并发执行异常。
任何建议将不胜感激。
我写的源代码:
- 下面是我的服务器程序:
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);
}
}
}
}
- 而客户端代码如下:
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");
}
}
}
请帮我修改代码...
我试图修复它几个小时但无济于事。
您正在一个线程 (while (it.hasNext())
) 中迭代客户列表,而另一个线程正在向其中添加项目 (clients.add(dos);
)。
例如,您可以通过传递列表的副本来教授新线程来避免异常:
ServerThread runnable= new ServerThread(socket,new Arraylist<>(clients),this);
我正在尝试通过服务器实现客户端到客户端的聊天。
但它给了我并发执行异常。
任何建议将不胜感激。
我写的源代码:
- 下面是我的服务器程序:
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);
}
}
}
}
- 而客户端代码如下:
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");
}
}
}
请帮我修改代码...
我试图修复它几个小时但无济于事。
您正在一个线程 (while (it.hasNext())
) 中迭代客户列表,而另一个线程正在向其中添加项目 (clients.add(dos);
)。
例如,您可以通过传递列表的副本来教授新线程来避免异常:
ServerThread runnable= new ServerThread(socket,new Arraylist<>(clients),this);