Why is this code not thread safe? 2) What issues could arise if it is not fixed? 3) How you would fix it using what you learned about synchronization? You must write lines of code or fix lines of code that already exist in the file for full credit. mport java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.function.Consumer; import javafx.application.Platform; import javafx.scene.control.ListView; public class Server{ int count = 1; ArrayList clients = new ArrayList(); TheServer server; private Consumer callback; Server(Consumer call){ callback = call; server = new TheServer(); server.start(); } public class TheServer extends Thread{ public void run() { try(ServerSocket mysocket = new ServerSocket(5555);){ System.out.println("Server is waiting for a client!"); while(true) { ClientThread c = new ClientThread(mysocket.accept(), count); callback.accept("client has connected to server: " + "client #" + count); clients.add(c); c.start(); count++; } }//end of try catch(Exception e) { callback.accept("Server socket did not launch"); } }//end of while } class ClientThread extends Thread{ Socket connection; int count; ObjectInputStream in; ObjectOutputStream out; ClientThread(Socket s, int count){ this.connection = s; this.count = count; } public void updateClients(String message) { for(int i = 0; i < clients.size(); i++) { ClientThread t = clients.get(i); try { t.out.writeObject(message); } catch(Exception e) {} } } public void run(){ try { in = new ObjectInputStream(connection.getInputStream()); out = new ObjectOutputStream(connection.getOutputStream()); connection.setTcpNoDelay(true); } catch(Exception e) { System.out.println("Streams not open"); } updateClients("new client on server: client #"+count); while(true) { try { String data = in.readObject().toString(); callback.accept("client: " + count + " sent: " + data); updateClients("client #"+count+" said: "+data); } catch(Exception e) { callback.accept("OOOOPPs...Something wrong with the socket from client: " + count + "....closing down!"); updateClients("Client #"+count+" has left the server!"); clients.remove(this); break; } } }//end of run }//end of client thread }
1.)Why is this code not thread safe?
2) What issues could arise if it is not fixed?
3) How you would fix it using what you learned about synchronization? You must write lines of code or fix lines of code that already exist in the file for full credit.
mport java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.function.Consumer; import javafx.application.Platform; import javafx.scene.control.ListView; public class Server{ int count = 1; ArrayList clients = new ArrayList(); TheServer server; private Consumer callback; Server(Consumer call){ callback = call; server = new TheServer(); server.start(); } public class TheServer extends Thread{ public void run() { try(ServerSocket mysocket = new ServerSocket(5555);){ System.out.println("Server is waiting for a client!"); while(true) { ClientThread c = new ClientThread(mysocket.accept(), count); callback.accept("client has connected to server: " + "client #" + count); clients.add(c); c.start(); count++; } }//end of try catch(Exception e) { callback.accept("Server socket did not launch"); } }//end of while } class ClientThread extends Thread{ Socket connection; int count; ObjectInputStream in; ObjectOutputStream out; ClientThread(Socket s, int count){ this.connection = s; this.count = count; } public void updateClients(String message) { for(int i = 0; i < clients.size(); i++) { ClientThread t = clients.get(i); try { t.out.writeObject(message); } catch(Exception e) {} } } public void run(){ try { in = new ObjectInputStream(connection.getInputStream()); out = new ObjectOutputStream(connection.getOutputStream()); connection.setTcpNoDelay(true); } catch(Exception e) { System.out.println("Streams not open"); } updateClients("new client on server: client #"+count); while(true) { try { String data = in.readObject().toString(); callback.accept("client: " + count + " sent: " + data); updateClients("client #"+count+" said: "+data); } catch(Exception e) { callback.accept("OOOOPPs...Something wrong with the socket from client: " + count + "....closing down!"); updateClients("Client #"+count+" has left the server!"); clients.remove(this); break; } } }//end of run }//end of client thread }
Trending now
This is a popular solution!
Step by step
Solved in 2 steps